Hi all. I have a custom field on the customer reco...
# suitescript
n
Hi all. I have a custom field on the customer record that I want to set with increment from the last value used. Currently, the custom field is set with value in sequence from 1 - 12. Then the next value is 23. I want to fill the gap between 12 and 23 and want to assign the next value as 13 (for new customer). I doubt I can achieve this using user event. There will be an issue when 2 or more customers are created simultaneously. Can someone confirm this?
s
This will be hard to do, for certain. First, consider how you would even programmatically find the first “missing” value in a non-continuous sequence. It can be done, but would require many searches. I would first try to create a reliable SuiteScript that can even tell you that 13 is the next unallocated number. That is not trivial. Also, consider how you will detect when you have closed one gap in the sequence and then need to find the next gap. Then, if you can solve that, my suggestion would be to make this a scheduled script. That will be much easier than doing it synchronously in user events, where each UE script would have to find the next available value, and there will definitely be race conditions.
n
13 is next is not difficult to find. I already have customers with values already assigned in the custom field. I also know that the sequence should be from 1 to 1000. So from 1 search I can get the already assigned values and compare it with the sequence from 1 to 1000 will give me the next "missing" number. But you confirmed my understanding by suggesting a scheduled script. I was planning the same but wanted to make sure I am not missing something related to the race condition.
b
the safe guide describes techniques for race conditions around page 77
✔️ 1
s
Yep, and quoting from the safe guide:
To significantly (but not completely) reduce the chances of SuiteScripts getting into race conditions, developers should move their scripts’ critical sections into individual scheduled scripts.
That could be either in the form of delegating critical sections to a scheduled script task from another script, or just doing everything in a scheduled script to begin with, if real time updates are not a requirement.