1

I was reading about using @future and the asynchronous process and I wanted to get clarification on persistence.

For example, if I have an @future method that makes a call to an endpoint in which I'm sending over data to the endpoint and I need to make an update in Salesforce based on the response, the response would need to send back the id of the record to update along with the data values to update, correct?

The values sent in the request are not persisted while the process is queued and waiting for the response or are they?

If the response only sent back a simple string of "success" or "failure" and I had to make updates inside of Salesforce based on that response, the id and data values sent in the request are no longer available and I would have no way of knowing what record to update at that point. Is that correct?

  • 1
    Can you include some code to illustrate what you're trying to get at? You can just parse the callout response... – Adrian Larson Jan 20 '17 at 16:26
  • @AdrianLarson I don't have any code example at the moment. I'm just trying to understand the persistence of an async process. I understand I can parse the callout response. But, if the callout response does not contain the record id and data values that were sent in the request, then I have no way of updating the record in Salesforce. The values sent in the request do not persist, correct? – Dman100 Jan 20 '17 at 16:34
  • If a variable is already within scope in your method, it's not going to suddenly fall out of scope...you're asking this question in a strange way and it's making it hard to answer. Even if you just use pseudo-code, it will be much easer to understand the core issue you are trying to get at if you at least give us an outline. – Adrian Larson Jan 20 '17 at 16:36
3

Making a callout via @future is not itself asynchronous. The "asynchronous" bit comes from the concept that the future method is not tied to the context that called it. As example pseudo-code, you might write this:

@future(callout=true) public static void queryExternalSystem(Id recordId) {
    Account record = [SELECT Name FROM Account WHERE Id = :recordId];
    HttpRequest req = new HttpRequest();
    req.setEndpoint(Label.SERVICE_ENDPOINT+'?Name='+EncodingUtil.urlEncode(record.Name, 'UTF-8'));
    req.setMethod('GET');
    // This callout is synchronous relative to this thread
    HttpResponse res = new Http().send(req);
    record.Synchronized__c = res.getStatus() == 200;
    update record;
}

Now, when you called this method, that would be asynchronous relative to the calling thread:

Account record = new Account(Name='Test');
insert a;
queryExternalSystem(a.Id);
// At this point, the callout has *NOT* been made yet.
0

Each @future call is isolated from the its class or context or the transaction by it called. You will have to rely on what you have sent in request, parameters and what you receive in response. Based on these value you can fetch some other information or perform actions only.

It is not possible to use variables outside of that future method. Because of two reason:

  1. Future methods are always static
  2. Future call are asynchronous. It will not wait to complete all the statements present in its parent method.

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.