0

I have a requirement where I have a custom field named ClientId on my Account object. So anytime i create an Account, I need to call one API to get the value of this client Id and save/update for this Account.

I tried to create an after insert trigger on Account which will call an APEX class method. This method makes an Apex callout to get the clientiD from api end point.

Since callouts are not supported in trigger, i made the method call as Asynchronous using @future. But then I realized that future call should be void so I passed the account id in the class method and then make to api call to get clientId and then update the Account object with this value.

But now my challenge is that since future calls are asynchronous, my account update statement gets executed before I get a response back from API. How to ensure that my update account command runs after the api response is back.

  • 3
    what did you try so far? – RedDevil Jun 15 '17 at 2:12
1

You would make the update in your future method:

@future
public static void makeCallout(Set<Id> accountIds)
{
    List<Account> records = [SELECT ... FROM Account WHERE Id IN :accountIds];

    // call out to external service
    // set fields on records as desired

    update records;
    // add error handling as desired.
}
  • Adrian, thanks for your reply. I am doing the exact same thing. Issue is that since @future makes the callout asynchronous, when i try to set the the field on record, callout hasn't returned the value and value is always set as blank. – Suresh Dalal Jun 16 '17 at 2:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.