'How to make a httpsrequest 'Get' in apex and then update a record in Salesforce

Overview:

We have a third party what hosts a text value at a given endpoint. Using a 'Get' request to a url where we also pass a key and parameters returns a string values (of decimal numbers and a space). I created some apex code, including @InvocableMethod, so I could all the apex from a flow where I pass in the URL, and then the text is returned to the flow. I then go on to update a record. Here is the Method, there is also a class, FR_Amount_Variables , storing the URL and String @InvocableVariable values.

public class FR_Amount_Sync {
  @InvocableMethod(label='FR Amount Raised Get')  
    public static List<FR_Amount_Variables>getFRamount (List<FR_Amount_Variables> inputURL) {
        FR_Amount_Variables amtvar = new FR_Amount_Variables();
        List<FR_Amount_Variables> getFRamount = new List<FR_Amount_Variables>();

        string endpoint = inputURL[0].URL;
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint(endpoint);
        request.setMethod('GET');
    HttpResponse response = http.send(request);
        string Amounts= response.getBody();
         Amounts= Amounts.replaceAll( '\\s+', '');
        if(String.isEmpty(Amounts)){
             Boolean isEmpty = true;
         Amounts = '0.00';
        }
       decimal amountss = decimal.valueOf(Amounts);
        amtvar.amount = amountss;
            getFRamount.add(amtvar);
        return getFRamount;
}  
}

The image of the flow can be seen below Update Flow

Issue:

When I run the flow in Debug mode, set the 3 input variables and run, the flow executes the apex and updates the specified record correctly. Likewise if I preset the flow's input variables (add a default value), and the just run the flow, the apex and record updates succeed with the record being update with the correct value from the 3rd party. The issue is when I try to automatically run the flow, either by Process Builder, or by Mass Action Scheduler, I receive system exception errors. An Apex error occurred:

System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

and

An Apex error occurred: System.CalloutException: Callout loop not allowed

respectively.

I was wondering if there is anyway to trigger a flow that doesn't trigger an error. Otherwise is there a way I can make a httprequest 'get' callout and then update a record with the received record.



Solution 1:[1]

We cannot do DML before the Callout in the same transaction. DML can be done after the Callout. So, the best practice is to do Callout using future method. In this way, the flow will handle the DML operations.

For example, check this link - https://www.infallibletechie.com/2020/04/how-to-do-callout-from-flow-in.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1