Does anyone know of an elegant solution to auto assign cases to different related accounts in a before trigger? All internally created cases are associated with 1 account by code. This creates a problem when multiple cases are being created at the exact same time as one of the cases will lock the account record and the other case will be lost. I originally was going to create 10 case specific accounts (CaseAccount1, CaseAccount2, etc.), and route cases to each account based on the last digit of the case (Case Number 12345670 -> CaseAccount0, Case Number 12345671 -> CaseAccount1, etc.) Unfortunately since Case Number is an Auto Number, it is not available on a before insert trigger. I think it's possible to do a random number generator but I don't think that would be the most ideal situation. Any ideas?

  • If your Case before trigger is bulkified, then the array Trigger.new will have indices 0,1,2,3.... - Thus could you use this to assign to the related Account? This isn't round robin but it sounds like your requirement is to avoid locking.
    – cropredy
    Commented Mar 3, 2014 at 17:39

1 Answer 1


If you know the 10 accounts at all times, then you can create 2 extra fields on the account - a checkbox field (Last_Round_Robin_Assigned__c or something) and a number field (Order_Number__c 1..10).

In your trigger then you can query your accounts, order by Order_Number__c, then loop through and find which one has Last_Round_Robin_Assigned__c field set to true. Assign the case to the next account and mark it Last_Round_Robin_Assigned__c = true and also mark the previous with Last_Round_Robin_Assigned__c = false. Repeat the same for each case

Don't forget to update the accounts too :)

  • thanks! that's a good idea, I was hoping to avoid creating new fields but if I can't figure out a solution in code I'll look into this method Commented Mar 3, 2014 at 16:07
  • I was wondering can you share your APEX? My case is that I am trying to create a button on the Contact that will create an opportunity on the account that gets assigned in a round robin way to a Sales rep using some Lead Assignment rules. So for instance our SDR's work off Accounts and Contacts. They are regionally grouped with our AE's. When they set up a pitch for the AE in their pod, we want them to just be able to click a button on the contact that will create an opportunity and using regional assignment rules assign round robin to an AE in their pod. Commented Sep 8, 2016 at 14:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .