Newer Version Available

This content describes an older version of this product. View Latest

End-to-End Example: Printer Supply Automation

This example demonstrates how to make sure that your office printers always have enough paper and ink by using two platform events, a process, and a flow.

Your company just received a shipment of “smart” printers. You configure the printers to send information to Salesforce once a day. You use that information to update the asset record in Salesforce that represents the printer, then decide whether to order more ink or paper from the vendor. When you do order supplies from the vendor, you schedule a technician to install the new supplies the day after they’re delivered.

Platform Events: Printer Status and Vendor Response

This example uses two platform events: one to hold the information coming from the printer (Printer Status) and one to hold the information coming from the vendor (Vendor Response).

The Printer Status platform event includes these custom fields.

API Name Field Label Data Type Description
Serial_Number Serial Number Text The printer’s unique identifier. This value is used to locate the corresponding asset record.
Ink_Status Ink Status Text Values: Full, Medium, Low, or Empty.
Paper_Level Paper Level Number Paper level in percentage.
Total_Print_Count Total Print Count Number Aggregate number of pages printed.

The Vendor Response platform event includes these custom fields.

API Name Field Label Data Type Description
Order_Number Order Number Text The order’s unique identifier.
Expected_Delivery_Date Expected Delivery Date Date The date when the vendor expects the order to be delivered
Order_Status Order Status Text Values: Ordered, Confirmed, Shipped, Delivered, Delayed, Canceled.

Process: Automating Printer Status Events

When Salesforce receives a Printer Status event, a process finds the asset record that’s associated with the printer. It then updates the Total Print Count to match the event. The process evaluates whether the printer has low ink or paper, and if so, launches a flow.

The process starts when it receives a platform event message. The process has three criteria and action groups.

Process that evalutes Printer Status events

Trigger

The process’s trigger receives a Printer Status event. It uses the serial number to find the asset record that matches the printer.

The process trigger

Criteria 1

The first criteria, its action group, and what happens when the actions are executed.

The first criteria is set to No criteria–just execute the actions! so that it always fires.

The action group contains one immediate action, which updates the asset record’s total print count to match the value from the event.

The action's field values

After the first criteria’s actions are executed, the process evaluates the next criteria.

Criteria 2

The second criteria, its action group, and what happens when the actions are executed.

The second criteria checks whether the event’s Ink Level value is set to Low.

The second criteria's conditions

The second criteria’s action group contains one immediate action, which launches a flow. The action passes a selection of fields from the asset to the flow that’s launched.

Flow Variable Type Value
assetId Reference [Asset].Id
assetOwner Reference [Asset].OwnerId
inkManufacturer Reference [Asset].Ink_Manufacturer__c
inkNeeded Boolean True
inkType Reference [Asset].Ink_Type__c
paperNeeded Boolean False
paperSize Reference [Asset].Paper_Size__c
serialNumber Reference [Asset].SerialNumber

After the second criteria’s actions are executed, the process evaluates the next criteria.

Criteria 3

The third criteria, its action group, and what happens when the actions are executed.

The third criteria checks whether the event’s Paper Level value is less than 10.

The third criteria's conditions

The third criteria’s action group contains one immediate action, which launches the same flow. The action passes a selection of fields from the asset to the flow that’s launched. It passes most of the same values to the flow as the Low Ink criteria group did with two differences: inkNeeded is set to false, and paperNeeded is set to true.

Flow Variable Type Value
assetId Reference [Asset].Id
assetOwner Reference [Asset].OwnerId
inkManufacturer Reference [Asset].Ink_Manufacturer__c
inkNeeded Boolean False
inkType Reference [Asset].Ink_Type__c
paperNeeded Boolean True
paperSize Reference [Asset].Paper_Size__c
serialNumber Reference [Asset].SerialNumber

The process has only three criteria, so after the third criteria’s actions are executed, the process stops.

Flow: Automation for Vendor Response Events

The Order Printer Supplies flow starts with a decision that determines whether to order ink or paper. Based on the decision, it submits an order of ink or paper with the vendor by using an Apex action. Then it pauses until the vendor sends a platform event message that says the order has been shipped. When Salesforce receives the specified event, the flow resumes and creates a task for the asset’s owner to install the new supplies.
The entire flow for this example

Decision Element

The decision includes two outcomes: Ink and Paper. The Ink outcome is true if the variable {!inkNeeded} is true. The Paper outcome is true if the variable {!paperNeeded} is true.

This Decision determines whether ink needs to be ordered This Decision determines whether paper needs to be ordered

Apex Action Elements

The flow includes two Apex actions that submit a supply order with a vendor but provide different information to it based on whether the flow executed the Ink outcome or Paper outcome. All the variables used for input values (like {!serialNumber} and {!paperSize}) are set when a process launches the flow.

The first Apex action provides information about which ink to order.

The Apex class's inputs determine what supplies are ordered and for what printer

The second Apex action provides information about which paper to order.

The Apex class's inputs determine what supplies are ordered and for what printer

In both Apex actions, the action returns an order number. The flow stores that value in the {!orderNumber} variable to reference in the Pause element.

The Apex class's output variables, passed into a flow variable to be referenced later

Pause Element

After the Apex action submits the supply order, the flow waits for confirmation that the order has been shipped. That confirmation is received through the Vendor Response platform event.

The flow pauses until Salesforce receives a Vendor Request event message with specific values. The order number must be the same as the order number that the Apex action provided. And the order status must be Shipped.

The event conditions subscribe the flow to a Vendor Response event only if it has a specific order number and order status

When the correct event message is received and the flow resumes, the flow stores the event message’s data in a record variable. That way, you can reference the expected delivery date to calculate when the supplies are scheduled to be installed.

Passing the event data to a Vendor Response record variable

Create Records Element

When the flow resumes, it creates a task for the asset owner to install the new supplies. Setting the task's fields with variables and flow resources

For the task’s field values, the flow uses these resources.
  • {!installDate}—A formula that calculates the day after the event’s expected delivery date.
  • {!taskDescription}—A text template that gives more details about the installation.
  • {!assetOwner}—Provided by the process that launches the flow
  • {!assetId}—Provided by the process that launches the flow
Setting the task's fields with variables and flow resources