Magic Software
Lesson 16Hands-on · ~25 min

Automatic Item Check

Some requests stay invalid for a while — stock isn't on the shelf, the price is too low, the customer hasn't been added yet. This lesson teaches you to schedule a periodic re-check and introduces the Flow Enablement feature for time-windowed flows.

The Scheduler Utility

You already know several ways to invoke a flow:

  • Auto Start when the project loads.
  • Call Flow from another flow.
  • External event — HTTP, Email, Web Service, etc.
  • Internal event — PSS publish.

When a flow needs to run on a clock, use the Scheduler. At deployment time, the Magic xpi Server reads its scheduler entries — one per timer event — and invokes each flow at the appropriate moment.

To add a scheduled trigger you need a Scheduler service plus a Scheduler trigger:

  1. In Settings » Services, add a Scheduler service named AutomaticCheck.
  2. Set Scheduler Type to Interval; leave Calculate Scheduler Runtime at Startup ticked; set Minutes to 2.

The Requests Auto Check Flow

  1. Insert a flow named Requests Auto Check; move it above Check Item.
  2. Drop a Scheduler utility into the Triggers area; configure it with name Request_Auto_Check and Service AutomaticCheck.
  3. Drop a Data Mapper into the flow; name it Check Request Items.
  4. Source: Database source RequestItemsRequestItems table, columns RequestId, ProductCode, Quantity, RequestPrice, WHERE [RequestItems].Status = 0.

The WHERE clause limits the query to only the lines that are still invalid — the ones worth re-checking.

Check Reason: A Helper Flow

Each invalid line is examined in a small helper flow:

  1. Insert a flow named Check Reason; move it below Check Item.
  2. Add flow variables: F.RequestNum (Numeric 5), F.ItemCode (Alpha 30), F.RequestQuantity (Numeric 9.2), F.RequestPrice (Numeric 9.2).
  3. Add a Data Mapper as the first step; name it Check Item Reason.
  4. Source: Database source FetchItemsProducts table, columns CatalogPrice and StockQuantity, WHERE [Products].ProductCode = '<?F.ItemCode?>'.
  5. Destination: Database destination UpdateItems, DB Operation Update; RequestItems table, columns Quantity and Status, WHERE RequestID = <?F.RequestNum?> AND ProductCode = '<?F.ItemCode?>'.

Connect CatalogPrice and StockQuantity to both destination columns, then set Calculated Values:

QuantityIF (Src.S1/Record/CatalogPrice < F.RequestPrice AND Src.S1/Record/StockQuantity < F.RequestQuantity, Src.S1/Record/StockQuantity, F.RequestQuantity)
StatusIF (Src.S1/Record/CatalogPrice < F.RequestPrice AND Src.S1/Record/StockQuantity < F.RequestQuantity, 'TRUE'LOG, 'FALSE'LOG)
What it means. If the catalog price is below the offered price and the stock is below the requested quantity, ship what we have — mark the line valid with the available quantity. The customer paid more than catalog, so reduced delivery still works.

Wiring the Per-Item Call

Back in Requests Auto Check:

  1. Open the Check Request Items step's configuration.
  2. Add a Call Flow destination named Call_Request_Check; target the Check Reason flow.
  3. Connect RequestId → F.RequestNum, ProductCode → F.ItemCode, Quantity → F.RequestQuantity, RequestPrice → F.RequestPrice.

Now every two minutes the scheduler invokes the flow, the Data Mapper iterates over invalid lines, and the helper flow checks each one and updates it if conditions allow.

Flow Enablement

The Enablement section of a flow's properties decides when the flow is allowed to run. Even if a trigger fires, a disabled flow is silently ignored.

AlwaysFlow is always enabled.
WeeklySpecific days of the week and times of day.
MonthlySpecific months and days — e.g. only the 7th of January, April, July, October.

Weekly and Monthly schedules are configured as Flow Enablement services in the Settings dialog — just like the Scheduler service. Define the service first, then assign it to the flow's Enablement Service property.

Exercise

Schedule a midnight sweep.

The flow you built fixes individual lines but doesn't ship the requests themselves. Define a scheduled flow that runs once a day at midnight and:

  1. Scans every open request.
  2. Publishes the HandleRequest topic for any request whose customer exists and that has at least one valid line in RequestItems.

Summary

You should now be able to:

  • Configure a Scheduler service and trigger to run a flow on an interval or at a specific time.
  • Use Flow Enablement to control the days and hours during which a flow can fire.
  • Combine Data Mapper, Call Flow, and Scheduler to implement periodic automatic correction logic.