+ Start a Discussion
Derek Patrick DayaDerek Patrick Daya 

Reaching the limit for SOQL Query

Hi All,

I have a VF template with Apex Class on our Production. I update the Apex Class on our sandbox to add a filter criteria to limit the result. unfortunately when I try to deploy it, I receive an error that says "DerekUpdateSource.updateUpdateSource(), Details: System.LimitException: AVTRRT:Too many SOQL queries: 101 (AVTRRT)".

The tricky part is DerekUpdateSource test class is not what I am deploying its a different one and that "DerekUpdateSource" is already deployed on our Production.

What I did was, I run a test on DerekUpdateSource on our Production and it gave the error "Too many SOQL queries: 101 " but on our Sandbox when I run the test it passed.

The only changes that happened on our Org recently was Target Recruit (third party app) deployed a patch update on our production to resolve a bug. After that I can no longer deploy any class that uses queries record. 

Target Recruit on our Sandbox is not yet update to the lastest patch of Target Recruit.

Is the DerekUpdateSource test class that has a problem or the recent patch of target recruit?

If its DerekUpdateSource test class, I really do not have any other ideas how to shorten this.

DerekUpdateSource Test Class:

@IsTest
public class DerekUpdateSource {

    static testmethod void updateUpdateSource(){

        AVTRRT__Config_Settings__c mycs = AVTRRT__Config_Settings__c.getValues('Default');
    if(mycs == null) 
    {
        mycs = new AVTRRT__Config_Settings__c(Name= 'Default');
        mycs.Trigger_Disable_UpdateAccountName__c = true;
        insert mycs;
    }
        Contact c = new contact();
            c.LastName = 'Test';
            c.RecordtypeID = '012A0000000v0La';
            c.AVTRRT__Source__c = null;
        insert c;
        
        AVTRRT__ETCObject__c a = new AVTRRT__ETCObject__c();
            a.AVTRRT__Name__c = 'jobsDB';
            a.AVTRRT__Candidate__c = c.id;
        insert a;        
        AVTRRT__ETCObject__c b = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'jobsDB' and AVTRRT__Candidate__c = :c.Id limit 1];
        update b;
        
        Contact c1 = new contact();
            c1.LastName = 'Test1';
            c1.RecordtypeID = '012A0000000v0La';
            c1.AVTRRT__Source__c = null;
        insert c1;
        
        AVTRRT__ETCObject__c e = new AVTRRT__ETCObject__c();
            e.AVTRRT__Name__c = 'Clyde Marine Recruitment';
            e.AVTRRT__Candidate__c = c1.id;
        insert e;
        AVTRRT__ETCObject__c f = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'Clyde Marine Recruitment' and AVTRRT__Candidate__c = :c1.Id limit 1];
        update f;
        
        Contact c2 = new contact();
            c2.LastName = 'Test2';
            c2.RecordtypeID = '012A0000000v0La';
            c2.AVTRRT__Source__c = null;
        insert c2;
        
        AVTRRT__ETCObject__c h = new AVTRRT__ETCObject__c();
            h.AVTRRT__Name__c = 'Corporate Event';
            h.AVTRRT__Candidate__c = c2.id;
        insert h;
        AVTRRT__ETCObject__c f1 = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'Corporate Event' and AVTRRT__Candidate__c = :c2.Id limit 1];
        update f1;
        
        Contact c3 = new contact();
            c3.LastName = 'Test3';
            c3.RecordtypeID = '012A0000000v0La';
            c3.AVTRRT__Source__c = null;
        insert c3;
        
        AVTRRT__ETCObject__c k = new AVTRRT__ETCObject__c();
            k.AVTRRT__Name__c = 'e Financial Careers';
            k.AVTRRT__Candidate__c = c3.id;
        insert k;
        AVTRRT__ETCObject__c f3 = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'e Financial Careers' and AVTRRT__Candidate__c = :c3.Id limit 1];
        update f3; 
        
        Contact c4 = new contact();
            c4.LastName = 'Test4';
            c4.RecordtypeID = '012A0000000v0La';
            c4.AVTRRT__Source__c = null;
        insert c4;
        
        AVTRRT__ETCObject__c n = new AVTRRT__ETCObject__c();
            n.AVTRRT__Name__c = 'Email/Mailer Application';
            n.AVTRRT__Candidate__c = c4.id;
        insert n;
        AVTRRT__ETCObject__c f4 = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'Email/Mailer Application' and AVTRRT__Candidate__c = :c4.Id limit 1];
        update f4;
        
        Contact c5 = new contact();
            c5.LastName = 'Test5';
            c5.RecordtypeID = '012A0000000v0La';
            c5.AVTRRT__Source__c = null;
        insert c5;
        
        AVTRRT__ETCObject__c q = new AVTRRT__ETCObject__c();
            q.AVTRRT__Name__c = 'Indeed';
            q.AVTRRT__Candidate__c = c5.id;
        insert q;
        AVTRRT__ETCObject__c f5 = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'Indeed' and AVTRRT__Candidate__c = :c5.Id limit 1];
        update f5;      
        
        Contact c6 = new contact();
            c6.LastName = 'Test6';
            c6.RecordtypeID = '012A0000000v0La';
            c6.AVTRRT__Source__c = null;
        insert c6;
        
        AVTRRT__ETCObject__c t = new AVTRRT__ETCObject__c();
            t.AVTRRT__Name__c = 'Jobstreet';
            t.AVTRRT__Candidate__c = c6.id;
        insert t;
        AVTRRT__ETCObject__c f6 = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'Jobstreet' and AVTRRT__Candidate__c = :c6.Id limit 1];
        update f6;
        
        Contact c7 = new contact();
            c7.LastName = 'Test7';
            c7.RecordtypeID = '012A0000000v0La';
            c7.AVTRRT__Source__c = null;
        insert c7;
        
        AVTRRT__ETCObject__c w = new AVTRRT__ETCObject__c();
            w.AVTRRT__Name__c = 'LinkedIn - Resourcer';
            w.AVTRRT__Candidate__c = c7.id;
        insert w;
        AVTRRT__ETCObject__c f7 = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'LinkedIn - Resourcer' and AVTRRT__Candidate__c = :c7.Id limit 1];
        update f7;
        
        Contact c8 = new contact();
            c8.LastName = 'Test8';
            c8.RecordtypeID = '012A0000000v0La';
            c8.AVTRRT__Source__c = null;
        insert c8;
        
        AVTRRT__ETCObject__c a1 = new AVTRRT__ETCObject__c();
            a1.AVTRRT__Name__c = 'Linkedin Advert';
            a1.AVTRRT__Candidate__c = c8.id;
        insert a1;
        AVTRRT__ETCObject__c f8 = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'Linkedin Advert' and AVTRRT__Candidate__c = :c8.Id limit 1];
        update f8;  
        
        Contact c9 = new contact();
            c9.LastName = 'Test8';
            c9.RecordtypeID = '012A0000000v0La';
            c9.AVTRRT__Source__c = null;
        insert c9;
        
        AVTRRT__ETCObject__c a9 = new AVTRRT__ETCObject__c();
            a9.AVTRRT__Name__c = 'Reed';
            a9.AVTRRT__Candidate__c = c9.id;
        insert a9;
        AVTRRT__ETCObject__c f9 = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'Reed' and AVTRRT__Candidate__c = :c9.Id limit 1];
        update f9;
        
        Contact c10 = new contact();
            c10.LastName = 'Test10';
            c10.RecordtypeID = '012A0000000v0La';
            c10.AVTRRT__Source__c = null;
        insert c10;
        
        AVTRRT__ETCObject__c a10 = new AVTRRT__ETCObject__c();
            a10.AVTRRT__Name__c = 'Red Website';
            a10.AVTRRT__Candidate__c = c10.id;
        insert a10;
        AVTRRT__ETCObject__c f10 = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'Red Website' and AVTRRT__Candidate__c = :c10.Id limit 1];
        update f10;
        
        Contact c11 = new contact();
            c11.LastName = 'Test11';
            c11.RecordtypeID = '012A0000000v0La';
            c11.AVTRRT__Source__c = null;
        insert c11;
        
        AVTRRT__ETCObject__c a11 = new AVTRRT__ETCObject__c();
            a11.AVTRRT__Name__c = 'Trade Winds';
            a11.AVTRRT__Candidate__c = c11.id;
        insert a11;
        AVTRRT__ETCObject__c f11 = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'Trade Winds' and AVTRRT__Candidate__c = :c11.Id limit 1];
        update f11;
}
}
Best Answer chosen by Derek Patrick Daya
Roy LuoRoy Luo
Always queue up your objects and do as less db access as possible. You may try something like this:
 
List<Contact> contacts = new List<Contact>();
List<AVTRRT__ETCObject__c> avtObjs = new List<AVTRRT__ETCObject__c>();
for(Integer i=0; i<10; i++)
{
   Contact c = new contact();
            c.LastName = 'Test';
            c.RecordtypeID = '012A0000000v0La';
            c.AVTRRT__Source__c = null;
    contacts.add(c);
}
insert contacts;

for(Integer i=0; i<10; i++)
{
    AVTRRT__ETCObject__c a = new AVTRRT__ETCObject__c();
            a.AVTRRT__Name__c = 'jobsDB';
            a.AVTRRT__Candidate__c = contacts.get(i).Id;
    avtObjs.add(a);
}

insert avtObjs;
       
update avtObjs;

 

All Answers

Roy LuoRoy Luo
Always queue up your objects and do as less db access as possible. You may try something like this:
 
List<Contact> contacts = new List<Contact>();
List<AVTRRT__ETCObject__c> avtObjs = new List<AVTRRT__ETCObject__c>();
for(Integer i=0; i<10; i++)
{
   Contact c = new contact();
            c.LastName = 'Test';
            c.RecordtypeID = '012A0000000v0La';
            c.AVTRRT__Source__c = null;
    contacts.add(c);
}
insert contacts;

for(Integer i=0; i<10; i++)
{
    AVTRRT__ETCObject__c a = new AVTRRT__ETCObject__c();
            a.AVTRRT__Name__c = 'jobsDB';
            a.AVTRRT__Candidate__c = contacts.get(i).Id;
    avtObjs.add(a);
}

insert avtObjs;
       
update avtObjs;

 
This was selected as the best answer
Sfdc CloudSfdc Cloud
Hi Derek,

Two things just wanted to check.Are you performing any DML operation on trigger.If yes then might be your trigger is calling itself recursivly.That would executing SOQL query which u have written in ur trigger.

Refer Below link for Recursive trigger
https://help.salesforce.com/HTViewSolution?id=000002357&language=en_US

Second thing i would consider in these type of situation is to have Test.StartTest() and Test.StopTest() as those help to refresh the context and hence avoid governor limit condition.Few changes you need to do is put all Soql query and DML operation inside Test.StartTest() and Test.StopTest().
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods


Please Let mw know if you will face any issue further.
Mark it for helps others:)
Thanks!!