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
Anil KamisettyAnil Kamisetty 

Findout CPU Processing Time in Apex

Apex Triggers and Classes take time in processing the data. Salesforce has set Governer limits saying that max of 10 Seconds for an UI operation and 60 seconds for a batch operation.

Is there any where in APEX programming where in the Developer can check the amount of processing time (CPU) utilized so far ? Based on the time, take appropriate action.
Best Answer chosen by Anil Kamisetty
Amit Chaudhary 8Amit Chaudhary 8
Please check below post That will help you
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_limits.htm
https://developer.salesforce.com/page/Best_Practice:_Use_of_the_Limits_Apex_Methods_to_avoid_Hitting_Governor_Limits
you can try below method
getCpuTime()
getLimitCpuTime()
trigger accountLimitExample on Account (after delete, after insert, after update) {

    System.debug('Total Number of SOQL Queries allowed in this apex code context: ' +  Limits.getLimitQueries());
    System.debug('Total Number of records that can be queried  in this apex code context: ' +  Limits.getLimitDmlRows());
    System.debug('Total Number of DML statements allowed in this apex code context: ' +  Limits.getLimitDmlStatements() );
    System.debug('Total Number of script statements allowed in this apex code context: ' +  Limits.getLimitScriptStatements());
    
   //This code inefficiently queries the Opportunity object in two seperate queries
    List<Opportunity> opptys = [select id, description, name, accountid,  closedate, stagename from Opportunity where accountId IN :Trigger.newMap.keySet()];
   
    System.debug('1.Number of Queries used in this apex code so far: ' + Limits.getQueries());
    System.debug('2.Number of rows queried in this apex code so far: ' + Limits.getDmlRows());
    System.debug('3. Number of script statements used so far : ' +  Limits.getDmlStatements());
    
    System.debug('4.Number of Queries used in this apex code so far: ' + Limits.getQueries());
    System.debug('5.Number of rows queried in this apex code so far: ' + Limits.getDmlRows());
    
    //NOTE:Proactively determine if there are too many Opportunities to update and avoid governor limits
    if (opptys.size() + Limits.getDMLRows() > Limits.getLimitDMLRows()) {
    		System.debug('Need to stop processing to avoid hitting a governor limit. Too many related Opportunities to update in this trigger');
			System.debug('Trying to update ' + opptys.size() + ' opportunities but governor limits will only allow ' + Limits.getLimitDMLRows());
			for (Account a: Trigger.new) 
				a.addError('You are attempting to update the addresses of too many accounts at once. Please try again with fewer accounts.');
	}
	
	else{
		System.debug('Continue processing. Not going to hit DML governor limits');
		System.debug('Going to update ' + opptys.size() + ' opportunities and governor limits will allow ' + Limits.getLimitDMLRows());
	    for(Account a : Trigger.new){
	    	System.debug('Number of script statements used so far : ' +  Limits.getDmlStatements());
	    	
	    	
	    	for(Opportunity o: opptys){	
	    		if(o.accountid == a.id)
	    		   o.description = 'testing';
	        }
	       
	    }
	    update opptys;
	    System.debug('Final number of script statements used so far : ' +  Limits.getDmlStatements());
	    System.debug('Final heap size: ' +  Limits.getHeapSize());
	}
}
Please let us know if this will help you

Thanks
Amit Chaudhary


 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please check below post That will help you
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_limits.htm
https://developer.salesforce.com/page/Best_Practice:_Use_of_the_Limits_Apex_Methods_to_avoid_Hitting_Governor_Limits
you can try below method
getCpuTime()
getLimitCpuTime()
trigger accountLimitExample on Account (after delete, after insert, after update) {

    System.debug('Total Number of SOQL Queries allowed in this apex code context: ' +  Limits.getLimitQueries());
    System.debug('Total Number of records that can be queried  in this apex code context: ' +  Limits.getLimitDmlRows());
    System.debug('Total Number of DML statements allowed in this apex code context: ' +  Limits.getLimitDmlStatements() );
    System.debug('Total Number of script statements allowed in this apex code context: ' +  Limits.getLimitScriptStatements());
    
   //This code inefficiently queries the Opportunity object in two seperate queries
    List<Opportunity> opptys = [select id, description, name, accountid,  closedate, stagename from Opportunity where accountId IN :Trigger.newMap.keySet()];
   
    System.debug('1.Number of Queries used in this apex code so far: ' + Limits.getQueries());
    System.debug('2.Number of rows queried in this apex code so far: ' + Limits.getDmlRows());
    System.debug('3. Number of script statements used so far : ' +  Limits.getDmlStatements());
    
    System.debug('4.Number of Queries used in this apex code so far: ' + Limits.getQueries());
    System.debug('5.Number of rows queried in this apex code so far: ' + Limits.getDmlRows());
    
    //NOTE:Proactively determine if there are too many Opportunities to update and avoid governor limits
    if (opptys.size() + Limits.getDMLRows() > Limits.getLimitDMLRows()) {
    		System.debug('Need to stop processing to avoid hitting a governor limit. Too many related Opportunities to update in this trigger');
			System.debug('Trying to update ' + opptys.size() + ' opportunities but governor limits will only allow ' + Limits.getLimitDMLRows());
			for (Account a: Trigger.new) 
				a.addError('You are attempting to update the addresses of too many accounts at once. Please try again with fewer accounts.');
	}
	
	else{
		System.debug('Continue processing. Not going to hit DML governor limits');
		System.debug('Going to update ' + opptys.size() + ' opportunities and governor limits will allow ' + Limits.getLimitDMLRows());
	    for(Account a : Trigger.new){
	    	System.debug('Number of script statements used so far : ' +  Limits.getDmlStatements());
	    	
	    	
	    	for(Opportunity o: opptys){	
	    		if(o.accountid == a.id)
	    		   o.description = 'testing';
	        }
	       
	    }
	    update opptys;
	    System.debug('Final number of script statements used so far : ' +  Limits.getDmlStatements());
	    System.debug('Final heap size: ' +  Limits.getHeapSize());
	}
}
Please let us know if this will help you

Thanks
Amit Chaudhary


 
This was selected as the best answer
Anil KamisettyAnil Kamisetty
Thanks for the comments, Yes I have to use the LIMITS class to get what I am looking for.