Using Platform Events in Flows

Slides

https://docs.google.com/presentation/d/1yMlNugksYqGpLE-48p0aNstE9uBW8CltKDrkDBtevTs/edit?usp=sharing

Video

TBA

See also https://tddprojects.atlassian.net/wiki/spaces/SF/pages/1447919617/Screen+Shots

General Platform Events Documentation

General Platform Events Posts

1. Chained Flows

What

  • I want one Flow to do multiple things and wait at particular “gates” until an external website has updated Salesforce.

Why

  • The website needs the data in a particular order.

  • The website can take a bit of time. We don’t want the users waiting around refreshing the page to see if the website has been updated.

  • But sometimes the website errors so we don’t want them to just set and forget - we want them to be alerted success or failure when the website finishes.

  • I also want it in one flow so the flow is easier to maintain, and has the logic built in - sometimes one pause will happen, sometimes three. So it’s going to go the quickest path possible.

Another Scenario

  • I have a batch process that runs in bulk to bulk update records created that day. The batch process takes a few seconds per record. There is another batch process that runs later that day to do more calculations dependent on the calculation of that first batch.

  • When I want to just run the batch process on one record, I have to run it, wait for the first batch to complete, refresh the page, then wait for the second batch to complete and refresh. Again, if we had Live Records the page would refresh for me when the batch process completed.

  • But if I created a flow with a Wait element and triggered the PE to be created when the field is updated the first batch process completing, then I could automatically run the second batch process, and both calculations would be updated with the click of a button.

Steps

See https://tddprojects.atlassian.net/wiki/spaces/SF/pages/1447919617/Screen+Shots

Gotchas

Salesforce bug

In my demo I had a few rogue Paused Flow Interviews…

There is a “bug” in Salesforce that Support won’t raise as a Known Issue and won’t include in the Documentation. If you have a heap* of Paused Flow Interviews still open, then Platform Events won’t fire. Yep, bizarre! I did a full video trying to explain the issue https://www.youtube.com/watch?v=gurBi3n6v20 and now that I review the video I can’t see there is a heap of Paused Flow Interviews hanging around so it’s making a liar out of me! Definitely raise a support ticket if you get stuck with any of this.
* unknown how many but I had hundreds due to a logic error in my Flow that was not resuming from the last pause.

Note: I would NEVER use Paused Flow Interviews for actual waiting… Eg where Time Based Workflows work really well and you can see how many are waiting and you can know exactly which record the Flow is waiting on. Paused Flow Interviews are just a bit useless still.

No Pause in Screen Flows or Record Triggered Flows

  • You can only do Pause in Autolaunched Flows, not Screen Flows. But I want to kick my process off with a Screen Flow. So I start a screen flow, then call a Subflow that has the Pause, but it has a pretty bad experience for the end user as the Screen Flow ends with this dialog that they then have to close.

  • It is better than having to send 3 different records over to the Website separately, but it’s still not a good experience. What I need to do is instead of calling a Subflow, I need to update the record at the end of the screen flow, which then triggers an Autolaunhced Flow via a Process Builder.

  • This utopia of getting rid of Process Builders is so very far from reality, isn’t it.

Notifications have some glaring issues

NOTE: Notifications ONLY work IF the user has cleared their notifications! If the user has more than 20 unread notifications and you don’t get “notified” at all.

There is also this huge gotcha with notifications - if you open the Notifications panel they are ALL marked as read. How many times I have accidentally done this when logged in as a user because I can’t stand seeing a notification count, I have lost count…
https://trailblazer.salesforce.com/ideaView?id=0873A000000TtRYQA0

Users can turn OFF notifications on Mobile.

So maybe do a Chatter Post instead. Do NOT do an email notification as that defeats the purpose of having Salesforce.

2. Replace Wait

Why

  • In my real-world scenario, I have a heap of DLRS Roll Up Summaries that need to be calculated soon after the record is updated, but do not need to be within the same transaction as the update to the record.

  • Good for high touch records that may have a heap of other triggers going on - especially from Managed Packages, and a regular save then takes too long.

Yeah, this is not a great example, because what happens when a record is deleted and you need to run DLRS then. I’m using this in an environment where the user can’t delete the child records… so only build things that work for your, not just because they exist.

Things to Note

You can’t (currently - I hope it’s coming) do Pause elements in After Save Flows. You can’t use the Commit Transaction Action (see Unofficial SF) work around either. You CAN do Pause Elements in Autolaunched Flows and you CAN do 0 minutes Wait time in Process Builder. This is the only reason I would use Wait in Process Builder.

3. External System Updates

Why

  • Because you may not want to expose the main object to the external system via the Integration User, or you may need to have a flow to run in System Context after the external user updates some key fields.

Things to Note

  • There are other ways to do this… this is just one option. Eg there is really no difference in creating a Platform Event than giving permissions to the Integration User onto the object that needs to be updated. But as they need Read access to Create, you will not want them to be able to Read all records so then you would have to set Sharing Rules also. So the Platform Event is a nice easy, safe way to provide the access. But depending on the amount of data to be updated it may be a lot of work to set it all up.

  • If you want to create a real “webhook” style endpoint that is publicly available then you need to expose that via an Apex Class, then give the Guest Site User access to that Class. It’s pretty straightforward, but the Guest Site User just can’t publish a Platform Event directly.

Resources

It’s not a good idea to use the easy option that they show in the post, and what I used in my demo, to do the SOAP Auth because it relies on a hard coded username, password and token. I used this for my Scratch Org, but don’t use it for Prod.

4. Flow Error Alerts

Why

  • Because Screen Flow error messages are awful and tell the user nothing.

  • Because sometimes showing the underlying error message directly to the user may help them quickly fix the issue and move forward, without waiting for the Admin to get the email, interpret what they were doing, and then help the user.

Things to Note

Unfortunately this is only for Screen Flows. It’s a big miss that this excellent resource can’t be used for other flow types.

  • This is not mentioned in the main Flow Help Docs that you can trigger a Flow on publishing of FlowExecutionErrorEvent. It is mentioned here though https://help.salesforce.com/articleView?id=flow_considerations_design_pause.htm&type=5

  • It’s easier to trigger the Flow than within Process Builder

  • There isn’t a lot of information in the FlowExecutionErrorEvent to use in the Notification or email.

  • See above re Notifications issues though.

Resources

This video shows how they used Platform Events for all errors https://www.youtube.com/watch?v=7iyoJGN8XN4 It may be overkill for your use-case.