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
SFDC@ErrorSFDC@Error 

Auto Assign multiple record

Hi All,
How can i auto update records/Auo assign records.I have created custom object with a custom lookup field on user and have 5 users.Admin will upload 100 records then it will autometically change user name with 20 record  each for 5 users.How can i achieve this type of custom requirement. Any idea?
Aleksey SheldagaevAleksey Sheldagaev
Probably you need to create a before insert trigger on your custom object and use a code similar to this:
Trigger someTrigger on CstmObject__c (before insert) {
	List<User> usersToAssign = [SELECT Id FROM User WHERE ....];
	List<CstmObject__c> insertRecords = Trigger.new;
	Decimal groupSize = Math.ceil( Decimal.valueOf(insertRecords.size()) / Decimal.valueOf(usersToAssign.size()) ); //20
	Integer counter = 0;
	Integer userIndex = 0;

	for (CstmObject__c rec : insertRecords) {
		userIndex = Integer.valueOf( Math.ceil( Decimal.valueOf(counter) / groupSize ) ) - 1;
		rec.UserId__c = usersToAssign[userIndex];
		counter++;
	}
}