Saturday, August 27, 2011

Best Practice of escaping the govenner limits in apex salesforce

Hi guys I have a  interesting topic to escaping the governor limits hitting in our apex script.
I just have the trigger on object Account which exposing some limits hitting in apex. And we can escape it by inspecting the limits while executing the script.


trigger LimitExample 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());    
 
   //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('Total heap size: ' +  Limits.getLimitHeapSize());
        System.debug('Final number of script statements used so far : ' +  Limits.getDmlStatements());
        System.debug('Final heap size: ' +  Limits.getHeapSize());
     
    }
}


cheers.....................

No comments:

Post a Comment