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
Saugandh KSaugandh K 

Exception when API callout is made to update this response to salesforce.

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". 
Im a newbie in triggers an callouts. Please guide me.
 
Narender Singh(Nads)Narender Singh(Nads)
Hi Saugandh,
I think we i know what your problem is but i can only help with the solution if I see all your code. And by all I mean your trigger and the classes you are using with it.