+ Start a Discussion
jagadeep kjagadeep k 

how to add 10 records of an aibject to 3 logged in users using apex

I am having a policy object which is having10 records and I need to assign these 10 records to logged in users for example if there are 3 users I need to distribute these 10 records among those 3 users.
PriyaPriya (Salesforce Developers) 
Hi Jagadeep,

Can you please elaborate your requirement with some examples?
Do you want to make those 3 users as owner of records ?
Are those 3 users have access to Policy Object ?
jagadeep kjagadeep k
Hi Priya, To answer your questions let's say I have 3 users who were logged in at this time, they have access to the Policy object and I have 10 records per Policy object which I need to assign to these 3 users I have written the code but I don't know where I am making mistake could you please help me out. This is a batch apex class and I am getting error in the execute method if policysize>0 public class QuotePolicyBatch implements Database.Batchable,Schedulable { public void execute(SchedulableContext sc) { String cronStr = '0 0 0 ? * * *'; System.schedule('Process Quote Records', cronStr, new QuotePolicyBatch()); Database.executeBatch(new QuotePolicyBatch()); } public Database.QueryLocator start(Database.BatchableContext bc) { return Database.getQueryLocator('select id,Assigned_To__c,OwnerId,isActive__c,Queue__c,Name from TGSI_Workflow_Distribution__c where Queue__c=\'TGSI\''); } public void execute(Database.BatchableContext bc, List scope) { Set userIdsSet = new Set(); Set queueIdsSet = new Set(); Set myqueue = new Set(); Integer i=0; for(TGSI_Workflow_Distribution__c dist : scope){ myqueue.add(dist.id); queueIdsSet.add(dist.Queue__c); } List dcriteria = new List([select id,Record_Type__c, Status__c, TGSI_Workflow_Distribution__c from TGSI_Workflow_Distribution_Criteria__c where TGSI_Workflow_Distribution__c in :myqueue ]); Set criteria = new Set(); for(TGSI_Workflow_Distribution_Criteria__c mycriteria: dcriteria){ criteria.add(mycriteria.Record_Type__c); } List policy = new List([SELECT Id,TGS_Is_Assigned__c,OwnerId FROM TGS_Quote_Policy__c WHERE TGS_Is_Assigned__c =FALSE AND RecordType.Name IN :criteria]); list usersList = new list ([SELECT OwnerId FROM TGSI_Workflow_Distribution__c WHERE Queue__c ='TGSI' AND Assigned_To__c='Logged In Users']); for(TGSI_Workflow_Distribution__c user: usersList){ userIdsSet.add(user.OwnerId); } List users = new List([SELECT UsersId FROM AuthSession WHERE IsCurrent = true AND UsersId IN:userIdsSet ]); //From here you have to check the code if(policy.size()>0){ for(TGS_Quote_Policy__c policyRecord : policy){ // for(i=users.size();i>0;i--){ while(i availableUsers = new List(); // List availableUsers = new List(); availableUsers.addAll(users); users=availableUsers; } } // policyRecord.OwnerId=String.valueof(users[0]); // policyRecord.TGS_Is_Assigned__c=TRUE; // if(users.size()==i){ // users=users; // } } update policy; // update scope; } } public void finish(Database.BatchableContext bc) { system.debug('JOB IS FINISHED'); } } Thanks & Regards K.P.Jagadeep