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
anji punyamanthulaanji punyamanthula 

how to assign records to different users

I have 3 users i want to fetch 10 records from Account object and assign first 3 records to first user next 3 records 2nd user another 3 records to 3rd user and again it will go to 1st user to assign another record how to do that can any one help me in that.
my code is below 

batch class:

global class Batch_Example_3 implements Database.Batchable<sobject>,Database.Stateful {
    global Static Integer count;
  global  Database.QueryLocator Start(Database.BatchableContext bc){
        String s='select name,ownerid from Account  limit 10';
        return database.getQueryLocator(s);
                                
 }
    global void execute(Database.BatchableContext bc,List<Account> accs){
        count=0;
        List<User> myuser=[select id from user limit 3];
        
        for(user u:myuser){
           for(Account a:accs){
            a.OwnerId=u.id;
              count+=1;
               update a;
        
        
        }
        
        }
        
        //update accs;
        
    }
    global void finish(Database.BatchableContext bc){
        
    }

}

execution:
Batch_Example_3 ba=new Batch_Example_3();
Id JobId=database.executeBatch(ba,1);

 
GauravGargGauravGarg
Hi Anji,

Try something like this.
 
Integer i = 0;
Integer count = 1;
for(Account acc : accs)
{
	if(count == 3)
	{
		i++;
		count = 1;  // Reset count. 
	}
	if(u != null && u.size() >= i)
	      Account.ownerId = u[i].Id;
	count++;
}

Thanks,
Gaurav
Skype: gaurav62990​