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
Vitalii SysakVitalii Sysak 

Future and callout

Hi everyone!

Need help.

 

I'm need create trigger who will work at @future and callout API from external services.

 

Here example what I'm write, have custom Object with field "Cover Letter' . Can I update this field in @future after get request from external service:


here is callout with example URL 

public class JobApplicationHttpCalloutForFuture {

    public static HttpResponse calloutCoverLetter(){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        String apiUrl = 'https://th-apex-http-callout.herokuapp.com/animals';

        request.setEndpoint(apiUrl);
        request.setMethod('GET');

        HttpResponse response = http.send(request);

        return response;
    }

}

Here is trigger helpers methods

 public static void updateCoverLetter(List<Job_Application__c> listJobApplications) {
        Set<Id> jobApplicationsIds = new Set<Id>();

        for (Job_Application__c jobApplication : listJobApplications) {
            if (jobApplication.Cover_Letter__c == null) {
                jobApplicationsIds.add(jobApplication.Id);
            }
        }

        assyncUpdateCoverLetter(new List<Id>(jobApplicationsIds));
    }

    @Future(Callout=true)
    public static void assyncUpdateCoverLetter(List<Id> jobApplicationIds) {
        List<Job_Application__c> jobApplications = [SELECT Id, Cover_Letter__c FROM Job_Application__c WHERE Id IN :jobApplicationIds];
        HttpResponse response = JobApplicationHttpCalloutForFuture.calloutCoverLetter();

        if (response.getStatusCode() == 200) {
            String responseBody = response.getBody();
            List<Job_Application__c> jobApplicationsToUpdate = new List<Job_Application__c>();

            for (Job_Application__c jobApplication : jobApplications) {
                jobApplication.Cover_Letter__c = responseBody;
                jobApplicationsToUpdate.add(jobApplication);
            }

            if (!jobApplicationsToUpdate.isEmpty()) {
                update jobApplicationsToUpdate;
            }
        } else {
            System.debug('Error getting response code ' + response.getStatusCode());
        }
    }

Can Im update field after update ?

AshwiniAshwini (Salesforce Developers) 
Hi @Vitalii Sysak ,
Can you elaborate more on the ask?
Are you trying to update records after making an asynchronous callout?
Vitalii SysakVitalii Sysak

Hi @Ashwini  ,

I'm trying to understand if I call an external API and get data from there, can I write them asynchronously using future methods in the trigger helper.

For example, I updated a record, and after the update, since the data is not needed right now, one of the fields is filled in with data from an external service, for example, weather, exchange rates, or as in my example, “list of animals.”

 
AshwiniAshwini (Salesforce Developers) 

Yes,you can update a field on a record asynchronously using a future method after making an external API call. 
i.e In your code, you are updating the "Cover_Letter__c" field on a custom object (Job_Application__c) after making a callout to an external API.