Wednesday, 10 December 2014



SOQL issue limit of apex transaction also include the SOQL issued by TRIGGER invoked by it.




One of the Governor Limit Says : “Total number of SOQL queries issued1                100(synchronous Apex transaction)                200 (synchronous Apex transaction)”.



As my general understanding SOQL issue Limit of a apex transaction would include the SOQL issued directly and SOQL issued from the methods called.

In one the great experience I learnt the fact that, SOQL issue Limit will also include the trigger’s are been triggered due to any insert or update done in that particular apex transaction. If I execute a apex code with three insertion of object, each of these insertion will invoke a trigger. The number of SOQL issue in this particular apex code transaction is SUM of all SOQL issued in these trigger and the direct SOQL issues in the code.

The exception “More SOQL issued : 101” can be raised in two cases :

1.       Extravagant complex code with a complex business requirement. Such as a case where we have to handle more number of the object types.
2.       In appropriate code practices Like DML operations in for loop.

Following the code sample which raise exception “Too many SOQL queries: 101” due to bad practices.

/*Trigger Code. Accepted that trigger is bad code. But needed for raise the error*/
trigger BadTriggerNeverHaveIt on TestObject__c (before insert) {
/*Complex logic which involves 10 SOQL execution */
      List<TestObject1__c > temprecords=[Select id from TestObjec1t__c ];
     List<TestObject2__c > temprecords=[Select id from TestObject2__c ];
     List<TestObject3__c > temprecords=[Select id from TestObject3__c ];
     List<TestObject4__c > temprecords=[Select id from TestObject4__c ];
   List<TestObject5__c > temprecords=[Select id from TestObject5__c ];
.
.
   List<TestObject10__c > temprecords=[Select id from TestObject10__c ];

    }
/*Code sniplet throw the exception: "Too many SOQL queries: 101"  due to bad  practice */
for(integer i=1;i<10;i++)
{
    TestObject__c tempObject= new TestObject__c ();
    tempObject.das__gender__c='Male';
    tempObject.das__gender__c='123455';
    tempObject.Name='TestName'+i;
    insert tempObject;      /* Each insert call the trigger which invokes 10 SOQL Queries  */
}
                                   
/*At the end of this Loop. This apex transaction already issued 100 SOQL*/
List<TestObject10__c > temprecords=[Select id from TestObject10__c ];
 /* Throws the exception of "Too many SOQL queries: 101"  */

/*Code sample  not throw the exception */
List< TestObject__c> tempObjects=new List< TestObject __c>
for(integer i=1;i<2;i++)
{
    TestObject__c tempObject= new TestObject__c ();
    tempObject.das__gender__c='Male';
    tempObject.das__gender__c='123455';
    c.Name='TestName'+i;
tempObjects.add( tempObjects);
}
    insert tempObjects; /*Trigger is called So 10 SOQL queries are issued*/
                             /* This following queries would be 11 SOQL issued by the apex transaction */
List<TestObject10__c > temprecords=[Select id from TestObject10__c ];


This blog is not to demonstrate the best practices but to show a  Un expected Error. As a developer, one should understand the scope of the governor limit extending to the trigger called due to the insertion / update in the apex transaction. 

The best practices avoid developer to face these exception. It always good to know the reason for this issues and ways to solve it.

Conclusion : 

The implication of the governor limit of Apex transaction is not only to the direct apex code or classes called in this transaction but also to the triggers that are invoked from it.

No comments:

Post a Comment