function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Deepak Kemidi 5Deepak Kemidi 5 

Implement Ranking logic using batch Apex

Hey Guys, I have a requirement where I have to bulk insert around 3000 records into a table every Saturday. This will be done using a batch job. After insertion, I have to update the Rank fields of the records based on a Revenue field while grouping it by Region and Team.
The logic is pretty simple, create a list of all the records, 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? Thankyou.
Paul S.Paul S.
Deepak - are you ranking just the 3,000 records that were inserted, or all records that now exist post-insert?  Does each region and team have its own ranking?  In other words, East: 1, 2, 3, etc., West: 1, 2, 3, etc.  In general, my initial instinct is to use a batch class that implements Database.stateful along with a counter variable that is not reset between batch executions.