+ Start a Discussion
yvk431yvk431 

Custom Setting Usage

Hi All,

 

I had a weird issue , please help me out.

 

We are using custom settings for auto numbering few fields on opportunity, we cant use the auto number field as the users wants to trip the number some times and values is used in some other id calculation, so we had to go for this approach and we just update the field with the custom setting value in a before insert event and increment the custom setting value accordingly. This was working fine since yesterday as a user reported that they found 2 records with the same auto number , to my surprise both were created by diff.  users with a second delay so their context will be different,   if they created exactly at the same time then its fine but even with a second delay how the duplication happened ?

 

I just wanted to know that my approach is correct or is there any better way to achieve this ?

 

--yvk

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

How are you retrieving the custom setting to increment it?  If you use the custom settings methods (getAll, getInstance etc) then this will retrieve data from the application cache.  I don't know the exact details of how this works, but if a cache is involved that sounds likely to give you a race condition.  If you use SOQL to retrieve the value, it gets the latest from the database, so this should be guaranteed to be up to date in the same way as any other object.

All Answers

bob_buzzardbob_buzzard

How are you retrieving the custom setting to increment it?  If you use the custom settings methods (getAll, getInstance etc) then this will retrieve data from the application cache.  I don't know the exact details of how this works, but if a cache is involved that sounds likely to give you a race condition.  If you use SOQL to retrieve the value, it gets the latest from the database, so this should be guaranteed to be up to date in the same way as any other object.

This was selected as the best answer
yvk431yvk431

Yes Bob, I am using the getInstance method to retrieve the value .

 

So retriving the values through SOQL will fix it,  cool it didnt strike to me at all. One last question even if I use SOQL  what happens if 2 users try to insert the record exactly at the same time, we will get any error  like the custom setting is locked for updation or the same issue repeats ?

 

Thanks for your valuable time Bob.

 

 

--yvk

bob_buzzardbob_buzzard

To be honest I don't know - you might try adding a for upate into the SOQL, which should lock the setting until your transaction completes.

yvk431yvk431

Thank you Bob, guess it will be enough to fix the issue

 

--yvk

anvesh@force.comanvesh@force.com

wht is  custom settings?