Mytutorialrack

Governor Limits

Governor Limits in Salesforce

Salesforce enforces Governor limits to ensure that resources are shared in multi-tenant environment. Developers need to be aware of the limits and understand how these governor limits will impact coding practices.

What are governor limits ?

  • Governor limits ensure that one customer does not monopolize shared resources in a multi-tenancy environment.
  • Examples of limits monitored are the amount of CPU time Used, memory used, how long a query can run ,how many records are returned from a query.
  • Limits are monitored per transaction and per customer over a defined time period to ensure that performance in one org is not impacted by another.

What is a Transaction?

A transaction is a code execution unit that is considered as one for monitoring governor limits.
Transactions may be initiated for triggers, web services, visualForce pages and cross multiple methods and triggers.
Let’s take an example to see what happens when we exceed governor limits.

for(integer x=0;x<200 ;x++)
{
      Account newAcc=new Account(Name='myAccount-'+x);
      try
      {
         insert newAcc;
         System.debug(Limits.getDmlStatements());
      }
     catch(exception ex)
     {
        System.debug('Caught exception ');
        System.debug(ex);
     }
}
//even if I add a new account
insert new Account(Name='My Account-Deepika');

Output of the above program:

 

  • The system enforces a DML limit of 150 statements per Apex transaction.
  • If there are more that 150 items, the 151st update call returns an exception that cannot be caught for exceeding the DML statement limit of 150.
  • No accounts will be inserted.

 

Important functions related to Governor Limits:

for(integer x=1;x<5 ;x++)
{
    Account newAcc=new Account(Name='MyTutorialRack-'+x);
   try
   {
        insert newAcc;
   }
   catch(exception ex)
   {
        System.debug('Caught exception ');
        System.debug(ex);
   }
}
System.debug('Limits.getDmlStatements() ==>'+Limits.getDmlStatements());
System.debug('Limits.getDmlRows() ==>'+Limits.getDmlRows());
System.debug('Limits.getLimitDmlRows() ==>'+Limits.getLimitDmlRows());
System.debug('Limits.getLimitDmlStatements() ==>'+Limits.getLimitDmlStatements());

getDMLStatements() Returns the number of DML statements (such as insert, update) that have been called.
getLimitDMLStatements() Returns the total number of DML statements that can be called.
getDMLRows() counts the number of records that have been processed with any statement that counts against the DML limits.

Implications of Governor Limits on Transactions:

  • DML will be rolled back if governor limits are exceeded.
  • Best practices should be followed to avoid SOQL queries inside for loops.
  • For example, a SOQL query inside a trigger.new loop may exceeded the maximum number of SOQL queries allowed
  • For processing of large number of records, Batch apex can be used.Batch apex breaks the record set down to smaller batches so governor limits are not reached
System.debug('Limit Aggregate: '+Limits.getLimitAggregateQueries());
System.debug('Limit Callout: '+Limits.getLimitCallouts());
System.debug('CPU Time: '+Limits.getLimitCpuTime());
System.debug('Limit DML Rows: '+Limits.getLimitDMLRows());
System.debug('Limit DML Statements: '+Limits.getLimitDMLStatements());
System.debug('Limit Email Invocation: '+Limits.getLimitEmailInvocations());
System.debug('Limit find similar call: '+Limits.getLimitFindSimilarCalls());
System.debug('Future calls: '+Limits.getLimitFutureCalls());
System.debug('Limit heap size: '+Limits.getLimitHeapSize());
System.debug('Limit queries: '+Limits.getLimitQueries());
System.debug('Limit SOSL Queries: '+Limits.getLimitSoslQueries());

Prepare for your Platform Developer 1 exam
References :To learn more

Share:

Recent Posts