I am having an issue because I believe I don't understand batch apex/scheduling very well. Every time I use this I get either a SOQL error of trying to query too many rows of 50001, or a DML error trying to add too many things in the list to update. Any help would be greatly appreciated
The code
global class LastMktoSyncDate implements Database.Batchable<sObject>{
global List<sObject> start(Database.BatchableContext c)
{
date d = system.today().addDays(-7);
List<sObject> scope = new List<sObject>();
scope.addAll([SELECT id, Name, Sync_to_mkto__c, account_name__c from Contact where ((Last_Marketo_Sync_Date__c = NULL OR Last_Marketo_Sync_Date__c < :d) AND Sync_To_Mkto__c != True)]);
scope.addAll([SELECT id, Name, Sync_to_mkto__c from Lead where ((Last_Marketo_Sync_Date__c = NULL OR Last_Marketo_Sync_Date__c < :d) AND Sync_To_Mkto__c != True)]);
return scope;
}
global void execute(Database.BatchableContext c, List<sObject> scope)
{
List<Contact> con_toupdate = new List<Contact>();
List<Lead> lead_toupdate = new List<Lead>();
for(sObject obj : scope)
{
switch on obj
{
when Contact con
{
con.Last_Marketo_Sync_Date__c = system.today();
con_toupdate.add(con);
}
when Lead lea
{
lea.Last_Marketo_Sync_Date__c = system.today();
lead_toupdate.add(lea);
}
}
}
update con_toupdate;
update lead_toupdate;
}
global void finish(Database.BatchableContext c)
{
}
}
The Scheduler
global class ScheduledMktoSync implements Schedulable{
global void execute(SchedulableContext sc){
LastMktoSyncDate l = new LastMktoSyncDate();
database.executeBatch(l);
}
}