+ Start a Discussion

Sequence number generation - Custom Setting



I am generating a sequence number using custom setting. This sequence number is required only for a particular type of record. So I chose custom setting. Actually I have a new problem now with the sequence number generation. Two of the records are having the same value. What I do is, I access the custom setting value when the user clicks on Save button, assign it to a field and then update the custom setting and save the record. What I thought was, since the custom setting  record access and updation will take only few milliseconds, unless and until the user clicks on Save at the same time, this wouldn't have been possible. Probably I am missing something. Can anyone please give me some pointers on this? Are there any alternate ways? Thanks a lot.




Can you share the code , so that one can see what actually you are trying to achieve, though one can understand that you are trying to generate autonumbers that too for a particular record.


Here's a suggestion in the record you can have a field which stores the autogenerated number say AutoNum__c, so instead of using custom setting


AggregateResult[] MaxResults = [Select MAX( AutoNum__c)maxi FROM YourObject__c];

Object MaxNum = MaxResults[0].get('maxi');

Use MaxNum +1 for assigning new autonumber.


Hope this helps.....






This is multi-tanent achitecture and same events can happen by different user at the same time. I would suggest you to create a object which will have single record for a field say 'sequenece__c'. Read the field  value from the code  to maintain the sequenece and update the field with value+1 so that next time you have next sequence number.

At a time two users wont be able to update this perticular object and if it happens then other users will get standard error. To handle this situation you can display a user friendly error.




Sasha ManfordSasha Manford
Hello -

Our opportunity reference numbers that auto-populate are out sequence and random. 

For example, the sequence jumped from 02525 12312

1. What is the cause of this
2. How do we fix this
Sasha ManfordSasha Manford

Following up on this pending matter - top management is inquiring and we need specific answers/workaround for this.