Expanding Your IoT Explorer Prototype

November 9, 2017 Appirio

By Dave Stanton

In a previous blog, we created a base IoT Explorer orchestration, and spent some time discussing state-machine design patterns. Now, we’re going to expand our baseline and use our orchestration to execute business logic across our Salesforce footprint.

Refactoring to align with business requirements

Your state machine should be designed based on business requirements, so we should take advantage of variables to make our rules more readable to business users. Having magic numbers (e.g., 40) hard-coded in logic is obtuse. We’ll also use global rules to handle scenarios where we might want an instance to be able to transition from or to any state at any time.


For our coffee use case, what we really want to know is if the coffee-hopper level if full, medium, or low. We should define these levels as variables, then refactor our rules to reference these variables.

  1. Edit the orchestration:
    1. Open Setup.
    2. Search for Orchestrations.
    3. Edit your Coffee Hopper orchestration.
    4. Click the Variables tab.
  2. Add a variable to represent the minimum level for a hopper to be considered full:
    1. Name: HopperLevelFull
    2. Data Type: Number
    3. Initial Value: 90
    4. Description: The minimum level (as percent) for a hopper to be considered full.
  3. Add a variable to represent the threshold for a hopper to be considered medium:
    1. Name: HopperLevelMedium
    2. Data Type: Number
    3. Initial Value: 40
    4. Description: The threshold level (as percent) for a hopper to be considered medium.
  4. Add a variable to represent the threshold for a hopper to be considered low:
    1. Name: HopperLevelLow
    2. Data Type: Number
    3. Initial Value: 10
    4. Description: The threshold level (as percent) for a hopper to be considered low.
  5. Click Save.
  6. Click on Rules tab.
  7. Update your rules to use your variable names.
  8. Click Save.

Global Rules

Global rules run every time a new platform event reaches your context. Combining global rules and variables unlocks the full potential of IoT Explorer use cases. We will add a global rule to handle common offline scenarios.

Every device is going to go offline at some point. This could be during installation, updates, rebooting, or other kinds of partial-service outage. We will define a generic Unknown state to catch these scenarios. We also will create a state-specific rule to allow an instance to transition back into service.

  1. Click on the Rules tab.
  2. Add a state named Unknown.
  3. Define a rule to transition from Unknown back to Normal state:
    1. When: Coffee_Hopper__e
    2. Condition: NOT(ISBLANK(Coffee_Hopper__e.Level__c))
    3. Transition: Normal
  4. Click Save.
  5. Click on the Global Rules tab.
  6. Define a global rule for any event that has a null level:
    1. When: Coffee_Hopper__e
    2. If: ISBLANK(Coffee_Hopper__e.Level__c)
    3. Change State: Unknown
    4. Description: An instance without level value should be considered as unknown state.
  7. Click Save

We now have a global rule to take any instance without a known level and transition to Unknown. Once that instance resumes sending complete data, we’ll transition back to Normal status. Depending on the true state of the instance, we may have to wait for a few events to be processed before the instance will settle into the true state within our orchestration.

Output Actions

We’ve designed an orchestration to represent our instance states, but we aren’t really doing anything with this information yet. Now, we are going to act on our event data to create records in Service and Sales clouds.

If the instance owner also has a subscription for coffee refills, we will create a Case record. The coffee hopper will be refilled before it becomes completely empty, so we have provided a proactive service. The customer enjoys continuous coffee availability. However, if the instance owner does not have an active subscription, we will create and Opportunity record to hopefully sell a refill subscription (or at least sell a one-time hopper refill).

  1. Click on the Rules tab.
  2. Find the Fault state.
  3. Click the chevron to the right then Add rule.
  4. Again, add a rule.
  5. Click the chevron to the right of our transition rule and Move rule down.
  6. Again, move the transition rule down, so that it becomes the bottom-most rule for the Fault state.

Define the top rule to create a Case if there is an active subscription:

  1. When: State Entered.
  2. Condition: Asset.Subscription__c = TRUE
  3. Actions: Add an action > Salesforce Record:
    1. Object: Case
    2. Action Type: Create
  4. Click Next.
  5. Click Add Field:
    1. Field: Subject
    2. Value: “Refill hopper ” & Asset.Id
    3. Action Name: CaseOutputAction
  6. Click Finish.

Define the middle rule to create an Opportunity if there is no subscription

  1. When: State Entered.
  2. Condition: Asset.Subscription__c = FALSE
  3. Actions: Add an action > Salesforce Record:
    1. Object: Opportunity
    2. Action Type: Create
  4. Click Next.
  5. Assign values to record fields:
    1. Name: “Sell subscription for hopper ” & Asset.Id
    2. StageName: “Prospecting”
    3. CloseDate: TODAY()
    4. Action Name: OpportunityOutputAction
  6. Click Finish.
  7. Click Save.
  8. Click Activate and destroy existing instances.

Testing Your Expanded Orchestration

Now we need to go back to Workbench and create events for an asset with an active subscription, and also for an asset with no subscription.

Get Asset Ids

In the previous Hello IoT Explorer post, we added a custom field to the Asset object to track if a status has an active subscription. We created two assets, one with and one without a subscription. We need to get the Salesforce Ids for these Asset records.

  1. Click on the App Launcher.
  2. Click on Assets.
  3. Click into the details for your two previously created assets.
  4. Note the Salesforce Ids of your asset records. We need to send these Ids in our event body, so that Salesforce map our event attributes to object fields.

Open Workbench

  1. Keep your Traffic view open in one browser window.
  2. Open Workbench in another browser window.
  3. Click Utilities > REST Explorer.
  4. Select POST method.
  5. Set endpoint as /services/data/v41.0/sobjects/Coffee_Hopper__e

Send events for an asset with a subscription

We’re going to send four requests. These will register and transition an instance though all states. When the instance transitions into the Fault state, a Case record will be created. You should click between Workbench and Traffic windows between each event to verify state transitions are happening as you expect. Please substitute the Asset_Id__c value with the id for the asset you created that has a subscription.

  1. The new hopper is installed and transitions into unknown state : { “Asset_Id__c”: “02i1I000000DNfnQAG”, “Level__c”: null }
  2. The hopper begins reporting with a full hopper: { “Asset_Id__c”: “02i1I000000DNfnQAG”, “Level__c”: 100 }
  3. The hopper reaches a warning level: { “Asset_Id__c”: “02i1I000000DNfnQAG”, “Level__c”: 30 }
  4. The hopper reaches a fault level: { “Asset_Id__c”: “02i1I000000DNfnQAG”, “Level__c”: 9 }
  5. Click the App Launcher
  6. Go to Cases
  7. You should see a newly created Case record with a subject something like: Refill hopper 02i1I000000DNfnQAG

Send events for an asset without a subscription

The only thing left for us to do is send all of those same events again, but with the Id for the Asset you created that does not have a subscription. After sending in the event payloads, you can see your newly created Opportunity record.

Ready to take your IoT project to scale?

We’ve gone through all of the features of IoT Explorer and created a solid foundation for your IoT initiatives. The state machine is comprised of business-defined rules. We use variables to abstract logic to help keep our rules readable. Global rules handle repetitive actions and edge cases. You leverage objects and fields to provide contextual information. Actions create or update other records to keep your service and sales staff immediately aware of the most important work.

I hope this series of posts has shown you the capabilities of Salesforce IoT Explorer and given you the foundational skills you need to begin your business-transformation journey.

Whether you’re using AWS, Azure, Particle, Predix, or another IoT platform, Appirio can help you design and implement your IoT strategy to be scalable, reliable, and secure. Get in touch to see how we can help.

Previous Article
Installing Marketing Cloud Connect in Salesforce
Installing Marketing Cloud Connect in Salesforce

By Diane Enriquez We’ve identified some important considerations to prepare for when installing Marketing C...

Next Article
Custom Error Messages in Salesforce Lightning
Custom Error Messages in Salesforce Lightning

By Nitin Goyal Visualforce pages have built-in implementation for showing custom error messages using the p...