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
Anirban RoyAnirban Roy 

ApexFirst error: Apex CPU time limit exceeded

Hi Gurus,

I am writing the below apex code to get the response from one of the other salesforce org using request/response mechanism. But getting the below error from the asynchronous block.
'First error: Apex CPU time limit exceeded'

Please find below the code

public class SendAccountUsingRESTAPI {
    
    private final string clientId = '*****';
    private final string clientSecret = '350708****';
    private final string username = 'somnath.pa****';
    private final string password = 'June2015gPe****';
    
    public class deserializeresponse
    {
        public string access_token;
        public string name;
        public string Integration_Id;
        public string id;
        public string description;
    }
    
    public string ReturnAccessToken(SendAccountUsingRESTAPI acc)
    {
        string reqBody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;
        System.Http h = new System.Http();
        System.HttpRequest req = new System.HttpRequest();
        req.setBody(reqBody);
        req.setMethod('POST');
        req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
        System.HttpResponse res = h.send(req);
        System.debug('Actual response' + res);
        deserializeresponse resp1 = (deserializeresponse)JSON.deserialize(res.getBody(), deserializeresponse.class);
        System.debug('deserialized  response' + resp1);
        return resp1.access_token;
    }
    
    @future(callout = true)
    public static void callcreateAcc( string name, Id Accountid)
    {
        SendAccountUsingRESTAPI send = new SendAccountUsingRESTAPI();
        string access_token = send.ReturnAccessToken(send);
        
        if(access_token != null)
        {
            string endpoint = 'https://ap2.salesforce.com/services/apexrest/v1/createAccount/';
            //'{"AccName" : "' + accName + '"}';
            string jsonstr = '{"AccName" : "' + name + '","accId" : "'+ Accountid + '"}';
            Http h2 = new Http();
            HttpRequest req1 = new HttpRequest();
            req1.setHeader('Authorization','Bearer ' + access_token);
            req1.setHeader('Content-Type','application/json');
            req1.setHeader('accept','application/json');
            req1.setBody(jsonstr);
            req1.setMethod('POST');
            req1.setEndpoint(endpoint);
            try
            {
            HttpResponse res = h2.send(req1);
            System.debug('Body of the response'+res.getBody());
            System.XmlStreamReader reader = new XmlStreamReader(res.getBody());
            System.debug('Response in XML format'+reader);
            Account accnt = [select description,Integration_Id__c,Id from Account where Id=:Accountid];
            
            deserializeresponse accrec;
            List<deserializeresponse> listofAcc = new List<deserializeresponse>();
            
                while(reader.hasNext())
                {
                   if(reader.getEventType()==xmltag.START_ELEMENT) 
                   {
                       if('Account' == reader.getLocalName())
                       {
                         accrec = new deserializeresponse();   
                       }
                       else if('Name' == reader.getLocalName())
                       {
                           accrec.Name = getvalufromTag(reader);
                       }
                       else if('INTEGRATION iD' == reader.getLocalName())
                       {
                           accrec.Description = getvalufromTag(reader);
                       }
                       else if('AccountId' == reader.getLocalName())
                       {
                           accrec.Integration_Id = getvalufromTag(reader);
                       }
                       
                   }
                    else if(reader.getEventType()==xmltag.END_ELEMENT)
                    {
                        if('Account' == reader.getLocalName())
                        {
                            listofAcc.add(accrec);
                        }
                        else if('Accounts'==reader.getLocalName())
                        {
                            break;
                        }
                    }
                }
                
               /* for(Account acct:listofAcc)
                {
                    acc
                }*/
                system.debug('List value'+listofAcc.size());
                
                for(deserializeresponse resp:listofAcc)
                {
                    //accnt.Descriptions = resp.description;
                    accnt.Description = resp.description;
                    accnt.Integration_Id__c= resp.Integration_Id;
                }
                
                //insert listofAcc;
                update accnt;
            }
            catch(Exception e)
            {
                System.debug('Callout error'+ e.getMessage());
            }
        }
        
    }
    
    static string getvalufromTag(XmlStreamReader reader)
    {
        
        string tagval;
        while(reader.hasNext())
        {
            if(reader.getEventType()==xmltag.END_ELEMENT)
            {
                break;
            }
            else if(reader.getEventType()==xmltag.CHARACTERS)
            {
                tagval = reader.getText();
            }
            reader.next();
        }
        return tagval;
    }

}

Incoming request is in the below format...


<?xml version="1.0" encoding="UTF-8"?>
<Accounts>
<Account>
<Name>Test International Shipping Co.</Name>
<INTEGRATION iD>00128000003nrBhAAI</INTEGRATION iD>
<AccountId>00128000003nrBhAAI</AccountId>
</Account>
<Account>
<Name>Test International Shipping Co.</Name>
<INTEGRATION iD>00128000003nrBhAAI</INTEGRATION iD>
<AccountId>00128000003nrBhAAI</AccountId>
</Account>
</Accounts>

Regards
Somnath
 
Andries.NeyensAndries.Neyens
I doubt that the functional logic would exceed the cpu limit. Problably there is an infinit loop inside your code. So try to focus on that to find a solution.