1

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;
    } 
}
8
  • I think it would be better to create batch job, which you can execute in this class.
    – kurunve
    Dec 18, 2015 at 14:25
  • i dont really understand that, I thought 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; am i missing a large amount of info from this process - if so i will go back to the drawing board instead of bothering you guys Dec 18, 2015 at 14:25
  • here is some examples about batch job developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
    – kurunve
    Dec 18, 2015 at 14:27
  • On a different note, you can initialize your list as List<Contact> agents ToRemind = [SELECT ...] . You don't need need the "new List" construct in this case. This makes your code cleaner. I would also use !agentsToRemind.isEmpty() instead of size () >0. You never know whether size() may be enumerating the collection.
    – mkorman
    Dec 18, 2015 at 14:49
  • 1
    @mkorman -- the test for list size>0 or empty is unnecessary - sfdc will execute the DML on an empty list and if the list is empty, not count it against dml call limits
    – cropredy
    Dec 18, 2015 at 17:22

2 Answers 2

5

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

1

There are so many ways to do it 1. You should customize way of creating list. 2. You need to limit the size of list to 10,000 on which you going to perform DML operation. 3. You can process next 10,000 records in next DML operation (need to define a valid criteria.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .