After a record is updated, i need to perform a callout to an external system, in order to synchronize the data. For that purpose, i have created a record-triggered flow for my custom object that:
- After record is updated
- invokes an Apex invocable method (asynchronously) to perform the callout.
I will describe what i am trying to achieve with an example:
- User goes to the record, changes data and clicks the save button
- Record Triggered flow is triggered to make the callout (We don't know when the callout will be performed)
- User goes again immediately and updates again the record
- Record Triggered flow is triggered to make the callout (We don't know when the callout will be performed)
What i want to achieve is avoiding making the second callout if the first hasn't been performed yet.My goal is to avoid invoking multiple times the external system for the "same" dataset. Is there a way to handle this somehow? Could Platform events help handle this somehow?
Best Answer
I would handle this with platform cache. Your invocable Apex should do the following high-level steps:
Some benefits of this approach:
All of this hinges on a good understanding of the platform cache and cachebuilder class interface. The Apex-recipes link above is a great example of how to use cachebuilder but be sure to also look at it's corresponding test class. (https://github.com/trailheadapps/apex-recipes/blob/main/force-app/tests/Platform%20Cache%20Recipes/PlatformCacheBuilderRecipes_Tests.cls)