0

I am currently trying to make a webservice callout via Apex to update some fields on an object. We have an insert trigger that is linked to an Apex handler class. To use the webservice, I must first use a POST method to get an access token, then a GET request to get the information I need.

The problem I'm running into is I have to use a @future method to get the Access Token since it is being called from a trigger which causes 2 problems: 1. I cant return the token to a public variable in the class and 2. the context is now asynchronous so I cant guarantee the order of exception for the GET request to follow the POST request.

I was thinkin about using Queueable apex to be called after the POST request. Is there a better way to do this?

3

I have to use a @future method to get the Access Token since it is being called from a trigger

Minor correction: you have to use Asynchronous Apex. You don't have to use an @future method; you just must leave the trigger context.

  1. I cant return the token to a public variable in the class

Correct, but it wouldn't matter anyway because you can't use that token in the trigger context - you can't make any callouts there, whether GET or POST.

  1. the context is now asynchronous so I cant guarantee the order of exception for the GET request to follow the POST request.

This is not quite right. Your asynchronous context is allowed to make multiple callouts, so long as you stay within governor limits. It's fine for your asynchronous Apex to make a POST followed by a GET; you simply must do all work that you wish to do with the results of those calls inside the async context or in a new job that you chain into. You can't return that data to a synchronous trigger context.

All other things being equal, I would recommend you use a Queueable Apex job because that interface allows a lot more flexibility than future methods.

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.