Build an order intake app
Connect a filtered datasource, validate a form before it saves, and show the right alert either way.
An order intake app takes a request in, checks it before it is saved, and tells the person whether it worked. This tutorial builds that shape: a table, a queue that only shows what still needs attention, a form that refuses to save until it makes sense, and two different alerts depending on what happened. Budget about 25 minutes.
What you'll build
- An Orders table with a customer, a product, a quantity and a status.
- An Order Queue component (Multi View) whose datasource is filtered to pending orders only.
- An Order Intake component (Single View) with a form.
- A save workflow that checks the form is filled in before it writes anything, an error Alert when it isn't, and a success Alert when it is.
This tutorial assumes the shape from Build a small CRM: a table, a Multi View list, a Single View form, and a save workflow. It spends less time on those basics and more on the parts that are new here: a filtered datasource, and a workflow that branches.
Step 1: Create the Orders table
Add the table
In the left tree, right-click Database, choose Create, set Type to Table, pick a
DBS Driver, and name it ORD - Orders.
Add the fields
On the Fields tab, use Add field for: ORD_Order_Id (String, mandatory), ORD_Customer_Name
(String, mandatory), ORD_Product (String, mandatory), ORD_Quantity (Number, mandatory),
ORD_Status (String).
Add a unique index
On the Indexes tab, add an index on ORD_Order_Id and turn on Unique.
Add sample rows
On the Data tab, add two or three orders by hand with different ORD_Status values, so the
next step has something to filter.
Step 2: Build the Order Queue with a filtered datasource
Add the component
Right-click Programs, Create, Type Component, Output Multi View, name it
Order Queue.
Connect the Datasource with a Query
Open the Datasource tab, choose DB Table, pick ORD - Orders, and set the filter mode to
Query instead of Index. Use the Builder view to add a rule: ORD_Status equals Pending.
Unlike the CRM's Contacts List, which loaded every row through an index, this component receives
only the rows that match the rule. See Component datasource.
Bring in the fields and lay out a row
On the Fields tab, bring in ORD_Customer_Name, ORD_Product, ORD_Quantity and
ORD_Status as Table fields. On the UI tab, add a row element, select it, and turn on
For Loop in Element properties so it repeats once per order still pending.
Preview it
Turn on Live Preview and confirm only the pending sample orders show up, not the ones you marked with another status.
Step 3: Build the Order Intake form
Add the component
Right-click Programs, Create, Type Component, Output Single View, name it
Order Intake.
Add its fields
On the Fields tab, add ORD_Customer_Name, ORD_Product and ORD_Quantity as Table
fields sourced from ORD - Orders. Leave ORD_Status off the form. It is set by the workflow in
the next step, not typed by the person placing the order.
Build the inputs
On the UI tab, add an input for each of the three fields, select each one, and use Bind in
Element properties to connect it to its field with xu-bind.
Step 4: Wire the save workflow, with validation
Add a Set Data program
Right-click Programs, Create, Type Set Data, name it Save Order. Leave crudMode
at C. On its Fields grid, bind ORD_Customer_Name, ORD_Product and ORD_Quantity as
Table fields, plus ORD_Status also as a Table field, the value for that last one comes
from the workflow step, not the form.
Add two Alert programs
Right-click Programs, Create, Type Alert twice: Order Saved (Type Success, Display
Toast, Title "Order received", Body "The order was saved.") and leave the invalid case for an
inline alert in the workflow itself, so it needs no separate program. See
Alert.
Start the Save button's workflow
Back on Order Intake, select the Save button, open Workflows, and Add Action to build:
Loader On (UI Utils), an inline Alert (UI Utils) for the invalid case, Set Data
(Database) pointed at Save Order with ORD_Status mapped to the fixed value Pending, Call
Alert (UI Utils) pointed at Order Saved, then Loader Off.
Make the invalid-case Alert conditional
Right-click the inline Alert step and choose Condition. Set its expression to true only when the form is incomplete, for example a customer name, product or quantity that is still empty. Fill in its Alert fields inline: Type Error, Display Toast, Title "Check the order", Body "Fill in customer, product and quantity before saving."
Make the Set Data step the opposite condition
Right-click the Set Data step, choose Condition, and set its expression to true only when the form is complete, the exact opposite of the Alert step's condition. Do the same for the Call Alert step after it, so a save only reaches the success alert when Set Data actually ran. See Events, Workflow for what Condition and Async control on a step.
Use the checkbox on a workflow step to disable it temporarily. Turn off the valid-path steps while you check the error Alert fires correctly on an empty field, then turn them back on.
Step 5: Make it a page
Add a Route
Right-click Routes, Create, name it Orders. Add a menu item titled "Queue" pointed at
Order Queue and set it as the default menu item, then a second item titled "New Order" pointed
at Order Intake.
Link intake from the queue
Select an "New order" element on Order Queue and give it a workflow: Add Action > Navigation >
Call Page, pointed at Order Intake.
Check it
Turn on Live Preview. Try saving the Order Intake form empty first, confirm the error toast
appears and nothing is written. Then fill in all three fields, save, confirm the success toast
appears and the order shows up in the Order Queue with a Pending status.