We are creating custom IDs for Accounts by incrementing an integer field in a custom class. The issue is that if two or more new Accounts are created in close sequence/simultaneously the following code will assign duplicated numbers rather than an incremented number for each:
public static void SetCustomId(Account account)
{
CustomObj__c customObj = CustomObj__c.getOrgDefaults();
account.CustomAccountId__c = String.valueOf(Integer.valueOf(customObj.CustomCounter__c));
customObj.CustomCounter__c = customObj.CustomCounter__c + 1;
update customObj;
}
Is there a way in Apex to lock a process so that a shared resource is used sequentially? In Java, C#, C++ etc. this is pretty easily done via a critical section if it's a single process or a Mutex if multiple processes access the same resource. Is there anything like this supported in Salesforce? Solutions beside Apex code/triggers would be fine as long as they meet the spec.
EDIT: It is important that the object creates be sequential and not generate errors due to lockouts.