0

I am getting an error of Too many SOQL queries 50001 in my schedule class.Can you suggest where am I going wrong?

global class update_Account implements Schedulable    
{
    global void execute(SchedulableContext sc) 
    {
    Map<Id,Account> AccountToChangeRequest=new Map<Id,Account>([Select id,name,lastname,(Select Id,Updated__c,Lead_Merchant_Name__c,New_Value__c,Additional_Comments__c,Request_Type__c 
    from Change_Requests__r where Updated__c= False  and Lead_Merchant_Name__c!= NULL order by LastModifiedDate asc)from Account]);
    List<Account>AccountToBeUpdated=new List<Account>();
    List<Change_Request__c> changerequestToBeUpdated=new List<Change_Request__c>();

 Account acc;
 Change_Request__c ncr;
    for(Id i:AccountToChangeRequest.keyset()) 
    { 

         if(AccountToChangeRequest.get(i).Change_Requests__r.size()>0)
         {
            acc=new Account(id=i);
            for(Change_Request__c cr:AccountToChangeRequest.get(i).Change_Requests__r)
            { 
                ncr=new Change_Request__c(id=cr.id);
                if(cr.Request_Type__c=='First Name')
                {
                    acc.firstname=cr.New_Value__c;
                    ncr.Updated__c=true;
                }
                else if(cr.Request_Type__c=='Last Name')
                {
                    acc.lastname=cr.New_Value__c;
                    ncr.Updated__c=true;
                }
                else if(cr.Request_Type__c=='Alternate Email')
                {
                    acc.Alternate_Email__c=cr.New_Value__c;
                    system.debug('--acc.Alternate_Email__c--->>'+acc.Alternate_Email__c);
                    ncr.Updated__c=true;
                }
                else if(cr.Request_Type__c=='Phone')
                {
                    acc.Phone=cr.New_Value__c;
                    system.debug('--acc.Phone--->>'+acc.Phone);
                    ncr.Updated__c=true;

                }
                else if(cr.Request_Type__c=='Alternate Phone')
                {
                    acc.Alternate_Phone__c=cr.New_Value__c;
                    system.debug('--acc.Alternate_Phone__c--->>'+acc.Alternate_Phone__c);
                    ncr.Updated__c=true;

                }
                else if(cr.Request_Type__c=='Industry')
                {
                    acc.Industry=cr.New_Value__c;
                    system.debug('--acc.Industry--->>'+acc.Industry);
                    ncr.Updated__c=true;

                }
                else if(cr.Request_Type__c=='Website')
                {
                    acc.Website=cr.New_Value__c;
                    system.debug('--acc.Website--->>'+acc.Website);
                    ncr.Updated__c=true;

                }
                else if(cr.Request_Type__c=='PersonMailingCity')
                {
                    acc.PersonMailingCity=cr.New_Value__c; 
                    system.debug('--acc.PersonMailingCity--->>'+acc.PersonMailingCity);
                    ncr.Updated__c=true;

                }


                changerequestToBeUpdated.add(ncr);
            }
            AccountToBeUpdated.add(acc);   
        }
    }    
            system.debug('--AccountToBeUpdated--->>'+AccountToBeUpdated);
            system.debug('---changerequestToBeUpdated-->>'+changerequestToBeUpdated); 
            if(!AccountToBeUpdated.isEmpty())
            update AccountToBeUpdated;
            if( !changerequestToBeUpdated.isEmpty())
            update changerequestToBeUpdated; 


   }

}
3
  • 2
    Move your logic to a batch...the error seems pretty clear. Your combination of Account and Change Request records totals more than 50k rows.
    – Adrian Larson
    Sep 23 '16 at 10:18
  • 1
    Each sub query counts as an additional query and count of records for the purpose of governor limits. Query all account in scheduler class and in batch execute method write [select id ,(inner query where accountid in :scopelist) from account where id in :scopelist]. Move your business logic into batch execute method Sep 23 '16 at 10:40
  • 2
    It's the query rows limit being hit, not number of queries.
    – Adrian Larson
    Sep 23 '16 at 10:42
0

Use limit statement in query to avoid hitting governor limit... The modified query should look like-

Map<Id,Account> AccountToChangeRequest=new Map<Id,Account>([Select id,name,lastname,(Select Id,Updated__c,Lead_Merchant_Name__c,New_Value__c,Additional_Comments__c,Request_Type__c 
    from Change_Requests__r where Updated__c= False  and Lead_Merchant_Name__c!= NULL order by LastModifiedDate asc limit :Limit.getLimitQueryRows())from Account limit :Limit.getLimitQueryRows() - Limit.getQueryRows())];

Calling Limit.getLimitQueryRows() will return maximum number of rows can be queried. Calling Limit.getQueryRows() will return current usage.

0

Code needs to be moved to Batch class to process chunks of records for each iteration of batch . Moreover, ensure where clause is properly optimized with indexed fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.