[SalesForce] Round Robin Auto-assignment of Related Account on Case object with before insert trigger

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?

Best Answer

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 :)

Related Topic