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
kvm1231kvm1231 

System.LimitException: Apex CPU time limit exceeded?

HI,

I am getting below error while calling for Webservice method.

System.LimitException: Apex CPU time limit exceeded
Error is in expression '{!init}' in component <apex:page> in page idm_support:testsendcustomer:
Class.Test.parseDealerResponse: line 1143, column 1
Class.Test.SendDealerRequest: line 1128, column 1
Class.SendCustomerController.init: line 14, column 1

I believe with below code i am facing this issue.
 
public static void parseDealerResponse(string dResponseXML,Account acc){
        XmlStreamReader dealerXMLReader = new XmlStreamReader(dResponseXML);
        map<string,string> ResponseMap = new map<string,string>(); 
        string currentNodeName = '';
        while(dealerXMLReader.hasNext()){
            if(dealerXMLReader.getEventType()== XmlTag.START_ELEMENT){
                currentNodeName = dealerXMLReader.getLocalName();
                dealerXMLReader.next();
                if(dealerXMLReader.hasText())
                    ResponseMap.put(currentNodeName, dealerXMLReader.getText());
            }    
        }
        if(ResponseMap.ContainsKey('Fault')){
            string faultstring = '';
            if(string.isNotBlank(ResponseMap.get('faultcode'))) faultstring+=ResponseMap.get('faultcode');
            if(string.isNotBlank(ResponseMap.get('faultstring'))) faultstring+=ResponseMap.get('faultstring');
            if(string.isNotBlank(ResponseMap.get('message'))) faultstring+=ResponseMap.get('message');            
            faultstring = faultstring.length()>255?faultstring.substring(0,254):faultstring;
            acc.IDSService_Error__c = faultstring.length()>255?faultstring.substring(0,254):faultstring;
            update acc;
            return;
        }
        acc.In_Sync_With_Rapport__c = true;
        acc.Last_Successful_Sync__c = DateTime.now();
        update acc;        
    }

Please help me why this error is getting and please let me know if you need any more information.

Thanks in advance
KVM
srlawr uksrlawr uk
Are you familiar with the Salesforce "governor limits" - check them out here:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm

Each transaction is limited (due to the nature of the shared hardware philiosophy of the cloud) and what is happening in your code is you are consuming too much CPU time in your transaction. 

If I had to guess, your
 
while(dealerXMLReader.hasNext())

loop, which is pulling out a lot of node text from your input is probably killing it. How much XML are you throwing at that?

You might need to find a way to make dResponseXML smaller, or navigate/substring the relevant part of it out more efficently than "while hasNext" on the whole DOM.