+ Start a Discussion
Poorna DeveloperPoorna Developer 

System.CalloutException Error - While API Integrated

Hi All,

I have a apex code to connect a third party API,

public with sharing class Saltedgelead 
{
    
      @InvocableMethod(label='leadsession' description='Create salt edge lead session')
    public static void leadtest()
        {
            Saltedgelead.geturl();
        }
    public static void geturl()
    { 
        String uri;
            Http http = new Http();
            HttpRequest request = new HttpRequest();
            request.setEndpoint('https://www.saltedge.com/api/partners/v1/lead_sessions/create');
            request.setMethod('POST');
            request.setHeader('Accept','application/json');
            request.setHeader('Content-Type', 'application/json');
            request.setHeader('App-id', 'q_-XTJ_zMjDdH3PiTTSY1q_SbhxYsdfgsg3SB1P3x_9-fJZh8');
            request.setHeader('Secret', 'iBUhgfhgfh6-5dIfUjT3gfNsGrznM-3WQ9ID8UJhMm-8ZsqLGE');
            request.setBody('{"data": {"customer_id": "2911634823132425819231","provider_code": "fake_oauth_client_xf","consent": {"scopes": ["account_details", "transactions_details"]}}}');
            HttpResponse responseg = http.send(request);  
            
         if (responseg.getStatusCode() != 201) {
            System.debug('The status code returned was not expected: ' +
            responseg.getStatusCode() + ' ' + responseg.getStatus());
             System.debug(responseg.getBody()); 
              String urli = responseg.getBody();
            Saltedgelead.sendingEmail(urli);
        } 
        else {
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(responseg.getBody());
            uri = (string) results.get('token');
        }
        // return responseg.toString();
           
        //Get
         Http httpg = new Http();
        HttpRequest requestg = new HttpRequest();
       requestg.setEndpoint('https://www.saltedge.com/api/partners/v1/customers/291163482315345819231');
        requestg.setMethod('GET');
        requestg.setHeader('Accept','application/json');
        requestg.setHeader('Content-Type', 'application/json');
       requestg.setHeader('App-id', 'q_-XTJ_zMjDdH3PiTTSY1q_SbhxY3SdsgB1P3x_9-fJZh8');
     requestg.setHeader('Secret', 'iBUdsfgg6-5dIfUjT3gfNsGrznM-3WQ9ID8UJhMm-8ZsqLGE');
       // requestg.setHeader('X-Fern-Token', uri);
        HttpResponse res = http.send(requestg);

        if (res.getStatusCode() != 200) {
            System.debug('The status code returned was not expected: ' +
            res.getStatusCode() + ' ' + res.getStatus());
        } else {
            System.debug(res.getBody());
        }   
    }
     public static void sendingEmail(String email) {
        Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
        String[] sendingTo = new String[]{'xxxxxxxx@gmail.com'};
        semail.setToAddresses(sendingTo);
        String[] sendingToBccAdd = new String[]{'xxxxxxxxxx@fernsoftware.com'};
        semail.setBccAddresses(sendingToBccAdd);
        String[] sendingTocAdd = new String[]{'xxxxxxxxxxx@gmail.com'};
        semail.setCcAddresses(sendingTocAdd);
        semail.setSubject('Single Email message Example');
        semail.setPlainTextBody(email);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail});
        //return null;
    }
}

Now I'm invoke this apex class in process builder to send the apex output data as a single mail to the user.

When my process builder start runs I get an error like - 
"We can't save this record because the “Saltedge API” process failed. Give your Salesforce admin these details. An Apex error occurred: System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out Error ID: 208143754-77391 (-1742348029"

Is I need to change my code? 
Any idea. 

Thanks all.

ShirishaShirisha (Salesforce Developers) 
Hi Poorna,

Greetings!

Seems like you are trying to update the same record using the callout which is being updated by the process builder at the same time.

You can not perform DML and Web service update on the same record.

Reference:https://help.salesforce.com/articleView?id=000326129&type=1&mode=1

You need to perform in separate transcations to avoid the error.

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Regards,
Shirisha Pathuri