function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lily BluntLily Blunt 

Asynchronous Call with error response handling?

Hello. I have a situation where, it is required to make an @future callout on Save of a record. And this occurs from the Trigger Handler class. All is good with respect to record getting saved, and the callout being made. However, I want to do something for the scenario where the callout fails at the external system. The DML is already done, so Salesforce data is updated. However external system is out of sync.  Is there any good architecture to handle this situation?   Ideally, values in 2 systems must not go out of sync at any point in time.  If the call out fails, the values in Salesforce should be rolled back.  Any architecture recommendations?

I belive it is not possible through SavePoint and Rollback, You have to delete records in Future method if error occurs

As in the Future method you can pass the record Id, So if you encounter Error, Then Delete the Record.

For Example u have passed Set<String> accountIds to ur Future method

if error occurs

Account[] accsToDelete = new Account[]{};
for(String s : accountIds){
   accsToDelete.add(new Account(Id = s));
delete accsToDelete;

hope this helps!!