Handling Approved Requests
Magic xpi's Publish & Subscribe (PSS) system lets one part of a project fire an event and any number of other flows react to it — without the publisher having to know who's listening. This lesson uses PSS to ship approved requests to delivery.
Publish and Subscribe Utilities
Think of PSS like a magazine subscription. The publisher emits an event; every subscriber that has registered for that topic is notified. The publisher doesn't track subscribers, and subscribers don't poll the publisher — the system distributes the event for you.
In Magic xpi, PSS topics are defined in the PSS Topics Repository. The PSS subsystem is shared across every Server hosting the project, so a publication in one Server can fire flows in another.
Defining the Handle Request Topic
- In the Solution Explorer, expand Repositories and double-click PSS Topics.
- Click Add.
- Set Topic Name to
HandleRequest. The repository prefixes the name withP.. - Click Save.
# - & \ / < > { } [ ] ( ) , ; " %, and they cannot
contain ???.
Subscribing the Process Request Flow
The subscribed flow does the actual work when a request is approved:
- Create a flow named Process Request.
- Open Properties » External » Subscribe Name; pick
P.HandleRequestfrom the topic list. - Add a flow variable
F.RequestNum— Numeric, size 5.
C.UserString, a numeric in C.UserCode, and a
BLOB in C.UserBlob.
Now read the request number out of C.UserCode into your variable:
- Drag a Flow Data utility as the first step. Name it Get request number.
- Add a row: Update » Flow »
F.RequestNum» expressionC.UserCode.
Adding the Delivery Header
- Add a Data Mapper as a child of Get request number. Name it Add Delivery Header.
- Source: Database source Get_request_header — Requests table, CustomerID column, WHERE
[Requests].RequestId = <?F.RequestNum?>. - Destination: Database destination Add_To_Delivery — Delivery table, all columns except DateSent.
- Connect
CustomerID → Delivery.CustomerID. - On
Delivery.RequestID, set Calculated Value toF.RequestNum. - On
Delivery.DateEntered, set Calculated Value toDate ().
Adding the Delivery Details
Only valid request lines (Status = 1) get shipped:
- Add a Data Mapper as a child of Add Delivery Header. Name it Add Delivery Details.
- Source: Database source Get_Request_Details — RequestItems table, columns ProductCode and Quantity, WHERE
[RequestItems].RequestID = <?F.RequestNum?> AND [RequestItems].Status = 1. - Destination: Database destination Add_To_Delivery — DeliveryItems table, all columns.
- Connect
ProductCode → DeliveryItems.ProductCodeandQuantity → DeliveryItems.Quantity. - On
DeliveryItems.RequestID, set Calculated Value toF.RequestNum.
Cleaning Up the ODS
Once the request has been delivered, drop the ODS entry to keep storage tidy:
- Drag a Flow Data utility as a child of Add Delivery Details. Name it Remove ODS entry.
- Click Add; set Action to Delete.
- Tick Dynamic.
- Set Name to
'Request_' & Trim (Str (F.RequestNum, '5')).
Publishing the Topic
The publisher side fires the event from the Scan for New Requests flow once a request is fully validated:
- On Scan for New Requests, add a PSS Publish utility as a child of Check Items.
- Name the step Publish the Handle Request.
- Open the configuration. In PSS Name, type
HandleRequest. - Set the Code parameter to
F.RequestNum— this is what subscribers will receive inC.UserCode. - Set the step's Condition to:
Trim (F.CustomerName) <> '' AND C.All_Items_Exist
That condition guarantees the topic only fires when the customer exists and every item is in stock.
Exercise
- In Process Request, also delete the corresponding rows from the local Requests and RequestItems tables — system cleanup.
- In the Add Customer flow you built last lesson, publish
HandleRequestwhen the customer's request is valid. Remember that to ship a request, every item in the request must haveStatus = TRUE.
Summary
You should now be able to:
- Define a PSS topic in the PSS Topics repository.
- Subscribe a flow to a topic and read the auto-passed parameters.
- Publish a topic from a step using the PSS Publish utility, with a condition that gates the publish.
- Coordinate cleanup work (database deletes, ODS removal) inside the subscriber.
