0

I am using a trigger to save 'Points' to an external database thorugh API callout. The result got from this API response should be updated to the salesforce databse though a DML operationThis callout and following DML operation is a part of CASE closing.

In the trigger i have a function as following:

public void processAfterUpdate()
{
     awardPoints( id, true)
}


@future(callout=true)
public static void awardPoints(Id caseId,Boolean isMYCard)
    {
      ----------
      ----------
        POperations pOps = new POperations();
            if(isMYCard)
                 res = pOps.processPoints(reqBody,true);
            else
                 res = pOps.processPoints(reqBody,false);

            if(res != null)
            {
             --------
             --------
                    Database.SaveResult sr = Database.update(c);
            }
        }

Here processPoints is a function in a controller class elseware. Below is the processPoints function used :

    public HttpResponse processPoints(String reqBody,Boolean isMyCard)
    {
    Http h = new Http();
        HttpRequest req = new HttpRequest();
        HttpResponse res;

        if(isMyCard)
            req.setEndpoint(BASE_URL+'/api/xxxx/xxxxx');
        else
            req.setEndpoint(BASE_URL+'/api/yyyy/yyyyy');

        req.setMethod('POST');
        req.setHeader('Content-Type','application/json;charset=UTF-8');
        req.setHeader('Accept', 'application/json');
        req.setBody(reqBody);
            req.setTimeout(TIMEOUT_MILLISECS);
        req.setClientCertificateName(CLIENT_CERTIFICATE);
        try
           {
               res = h.send(req);
           }
           catch(Exception e)
           {
             System.debug('Exception====='+e);
           }
           return res;
    }

This always gives an error

"You have uncommitted work pending. Please commit or rollback before calling out".

  • Can you post complete code.. i believe you are doing DML before callouts your future method. – Pranay Jaiswal May 11 '18 at 15:23
1

Actually, Callout may not occur after a DML statement within the same transaction. To achieve the required action, the transaction must be separated into two parts so that the DML transaction is completed before the Web Service Callout occurs.

Here is the detailed salesforce help article: https://help.salesforce.com/articleView?id=000003701&type=1

But in your case, question is how you can separate these two process. Usually, developer uses future method to separate it but you are already in future as you have to make callout from trigger; so you can not use another future from current future method.

As a workaround, I would suggest to use batch and split your process in two steps as mentioned here:

  1. Make callout in execute of batch.

  2. Update Case record in finish.

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.