0

Am trying to create auto number through apex.

The functionality which i require there in an number field which is number/text type which is UNIQUE. Whenever an Account is created the custom number field should be incremented by 1 irrespective of the previous record are deleted the count should always keep on incrementing through a trigger. i can't use a auto number field type as there are different record types and the auto number field can't be incremented based on record type, the gap is okay and there shouldn't be no duplicates My code below.

trigger rectype on Account (before insert) {

        for(Account accitem : Trigger.New){


           Account acc = new Account();
           System.debug('rectype'+recordTypeId);
            List<Account> results =[SELECT <custom field>  FROM Account where  RecordTypeId='0120l000000PxdRAAS' order by CreatedDate desc LIMIT 1 ];
           Decimal prtnr =  results[0].custom field;

            System.debug('count'+prtnr);
            accitem.custom field= prtnr+1;
          //  i=integer.valueof(s);
            System.debug('PartnerId Visible'+accitem.<custom field>);
            }


}

The issue here is lets say the record is getting deleted , the incrementeation is happening from the recent record not with the last Customfield value.

From user3169951's comment:

i can't use a auto number filed type as there are different record types and the auto number field can't be incremented based on record type, the gap is okay and there shouldnt be no duplicates

  • Can you clarify the delete case: what if earlier records are deleted - should those numbers e re-used? – Keith C Jun 27 '19 at 8:09
  • the earlier shouldn't be reused – user3169951 Jun 27 '19 at 9:28
3

By far the easiest and least error-prone approach is to base the logic on an auto-number field and then do further Apex processing (if that is needed) in an after trigger that applies its logic to the auto-number value and then updates a separate field with the final value.

There will be gaps in the numbering, but while you might be able to construct a reliable scheme to handle the most recent record(s) being deleted (for update would need to be used to avoid duplicates), recycling earlier numbers would be tough. So the effort is only worth it if no gaps in the numbering is an explicit and high-value requirement.

  • This is actually a way better solution than mine, of course. – rael_kid Jun 27 '19 at 8:48
  • i can't use a auto number filed type as there are different record types and the auto number field can't be incremented based on record type, the gap is okay and there shouldnt be no duplicates – user3169951 Jun 27 '19 at 9:26
  • @user3169951 Oh I see. Hopefully, someone will add this comment to the question as it is important. Editing is blocked for me right now. – Keith C Jun 27 '19 at 9:34
  • @KeithC - Yes , this has been updated for further reference , any ideas to achieve such thing – user3169951 Jun 27 '19 at 10:08
  • I suggest using a list custom setting where each row is named after a record type id. Query and update the rows using for update. – Keith C Jun 27 '19 at 10:55
0

I normally use a custom setting for this. Just create a custom setting (list type, regardless of the warning SF gives), with one custom field, "Value". Create a record in this custom setting with name "Account Number" or something and with value 0. Then in your trigger get the current value for this record, assign the current value to your account and increment the value. This can be done in bulk of course.

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.