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;
}
}
Account
andChange Request
records totals more than 50k rows.