[SalesForce] scheduled apex+Too many DML rows: 10001

I am getting this error when i look at my apex jobs UI

"Scheduler: failed to execute scheduled job: jobId: 7072000002391Am,
class: common.apex.async.AsyncApexJobObject, reason: Too many DML
rows: 10001"

All suggestions I have seen tell me to schedule the job but it scheduled to go everyday – do I need to break it in to smaller batches still?

This is the code, any suggestions are most appreciated

global void execute (SchedulableContext sc) {
    List<Contact> agentsToRemind = new list <Contact>
        ( [SELECT Id FROM Contact WHERE Days_since_web_registration__c  > 90 
          AND Luxury_Agent_ID__c = NULL AND join_heroes_reminder_sent__c= NULL ]);
    IF(agentsToRemind.size()>0){   
        update agentsToRemind;
    } 
}

Best Answer

You have hit a DML limit. While the algorithm you mention is correct:

if you created a class like this and scheduled it to run once a day it would update everyone in the loop, which would then be picked up by a workflow I have in there

You're not taking into account Salesforce limits. You're limited to manipulating only up to 10000 records in one execution context. If you have more than 10000 contacts in your org, you will get the exception you mention above. Read more about limits here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm

In your case, the best way to overcome this limitation is to use your scheduled job to trigger a batch Apex job: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm

Related Topic