For a simplified example, assume that all our accounts have a ranking based on revenue. Every month I want to update these rankings based on new accounts that were added. Assume we have 50,000 accounts.
The logic is pretty simple, create a list of all the accounts, order by revenue DESC, loop through the list and use the index as the ranking.
However, I'm hung up on doing this at a large scale. It can't be done through a trigger since it gives me a 10,001 DML error. I've tried Apex Batch Update, but that seems to only load 200 records at a time before applying the above logic. However, I need to run the logic on all the records at once. I'm not very familiar with Batch Apex so maybe there is a different way to load the data.
How can this be done?
Thanks
Below is my existing code.
global class BatchTierUpdate implements Database.Batchable<sObject> {
global final String Query;
global BatchTierUpdate()
{
Query = 'SELECT Id FROM Account Order by Revenue__c';
}
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> scope) {
Integer i = 1;
For (Account ac : scope) {
ac.Rank__c = i;
i = i + 1;
}
update scope;
}
global void finish(Database.BatchableContext BC) {
}
}