You need to sign in to do that
Don't have an account?
Governor limits and scope in Batch Apex
I have a batch apex job and the code is as follows
global Database.QueryLocator start(Database.BatchableContext BC) { return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC , List<sobject> scope) { List<Test_Key_Event__c> keyEventsToInsert = new List<Test_Key_Event__c>(); List<RecordType> recordTypes = [Select r.Id, r.Name, r.SobjectType from RecordType r Where r.SobjectType = 'Test_Key_Event__c' ]; For(sobject s : scope) { Contact con = (Contact)s; For(ActivityHistory task : [Select c.AccountId, c.Id, (Select AccountId, CreatedById, CreatedDate, WhoId, Id, OwnerId, Subject From ActivityHistories Where Subject like '%KE: Autodemo%') from Contact c Where c.Id =: con.Id ].ActivityHistories) { //if(task.OwnerId == '00530000001rFmIAAU' || task.OwnerId == '00530000001tz0yAAA') if(task.CreatedById == '00530000001rFmIAAU' && task.CreatedById == '00530000001tz0yAAA') { Test_Key_Event__c keyEvent = new Test_Key_Event__c(); keyEvent.Account__c = task.AccountId; keyEvent.Contact__c = task.WhoId; keyEvent.Name = 'AutoDemo'; keyEvent.Score__c = 50; keyEvent.Test_Key_Event_Date__c = task.CreatedDate; keyEvent.Test_Key_Event_Type__c = 'KE:Autodemo'; for(RecordType rec : recordTypes) { if(rec.Name == 'default') keyEvent.RecordTypeId = rec.Id; } keyEventsToInsert.add(keyEvent); //insert keyEvent; } } } insert keyEventsToInsert; }
I am running into the governor limits in the execute (Too many SOQL queries: 201). I am assuming its because I have a subQuery in the for loop and because I am using Database.QueryLocator its allowing only processing data in atches of 200 but subquery is counted as another query.
My questions are :
Am I right in my presumption with the governor limits?
if yes
What is the possible solution . I am guessing using the scope param in execute batch but wont that limit the num of records queried in the start method query. I have around 500000 records returned in that query.
Whats the possible workaround solution.?
I had to adjust the scope param in executeBatch to pass on less than 200 (default for query locator) records in each batch job. And yeah every subquery is considered another query by apex so the governor limits kicked in.