I'm trying to track an incrementing number in Salesforce with the following caveat: The number should only increment when a Lead or Contact meets certain criteria.

This has been working ok with a custom setting number - however issues arise when two records that match the criteria are created at the same time. I was first getting UNABLE_TO_LOCK_ROW, then when adding FOR UPDATE to the query call I'm receiving Record Currently Unavailable.

Is there a better solution to this problem that doesn't use Custom Settings?

Code below:

myObject.Number__c = myObject.Number__c + 1;

    try {
        update myObject;
    } catch(Exception e) {
  • I assume you're updating the custom setting in a trigger. It might be worth rewriting your update code to work in a batch class which is run every hour or so, or pushing recalc jobs out into a @future queue. Its not the custom setting thats the issue, its the code which is changing the setting. Can you edit your question to include your code (or pseudocode even)? – battery.cord Nov 5 '18 at 18:44
  • Added, essentially whenever certain criteria on the Lead/Contact are true, I increment this number and if it's divisible by 10 head into a different logic branch (we only want to do this for 1/10 Leads/Contacts). – McD Nov 5 '18 at 18:54
  • I'd try to move that extra logic branch (i assume thats the one which updates the setting) to a batch job imo – battery.cord Nov 5 '18 at 19:06
  • @battery.cord it can still cause collision and would end up with 2 records with same number – Pranay Jaiswal Nov 5 '18 at 19:08
  • How accurate does your operation on "every tenth Lead/Contact" need to be? Does it matter if you skip the occasional one? Things like this sound simple in theory, but get complex when you start dealing with concurrent transactions and rollbacks. – Daniel Ballinger Nov 5 '18 at 23:47

if you can sacrifice an Object ,DML and SOQL probably that way it could work. Here you would never hv to work about record locking as you are inserting new record

  1. Create an Object called as AutonumberObject and Its name field would be autonumber.
  2. Whenever you feel you need the next number, just insert it. Inserting it would poupulate the name field. Use that name field for your reference
  3. Rollback your Autonumber record. By this way you dont have to worry about if 2 transaction happen at same time still they won't have same number.

    public static String getIncrementedNumber(){
        Savepoint sp= Database.setSavepoint();
        AutonumberObject__c aa= new AutonumberObject__c ();
        insert aa;
        String incrementedNumber = [Select Id,Name from AutonumberObject__c where id=:aa.Id][0].Name.split('-)[1]);
        return incrementedNumber ;
  • 1
    Dear @Downvoter would love to know the reason for downvote. – Pranay Jaiswal Nov 5 '18 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.