Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › Unique auto-increment number for all devices
May 16, 2020 at 8:41 AM #40618
I have a field (number) with auto-increment option. This number must be unique for each record.
On each device works perfect. My issue is when I synchronize the devices because I get duplicate values in that field.
This occurs when I add new records on my iPhone, then on my iPad before I sync them. Then after sync I get those duplicates.
Any ideas how can I solve this in other way? I thought about random numbers, but I want they sequentially.
Thanks!May 16, 2020 at 2:21 PM #40619
To get the sort of autonumber feature you mention you need to have a shared generator to issue the numbers. How does the first device “know” how many records have been created on any other device?
You could set up a web service somewhere that all of your devices can refer to that sets the number. You’d need to create a web service somewhere that all of your devices can access and then a script field to retrieve the number from the service to save in your document. I’d make the script and it’s target field two so that you can handle if the number service is unavailable to give you a number.
This would give you a monotonically incrementing source of numbers though there is the risk that you will still have holes from time to time (numbers that are allocated but not used or used in deleted docs) however completely avoiding that in a fully distributed system like Tap Forms is impossible.May 17, 2020 at 8:54 AM #40621
Does your field number need to be sequential? You could create a global serial number that contains the local serial number and a device identifier. This way, the numbers will be unique.
For the device identifier, you can use
utils.getDeviceName(). Then depending on the value you get, you would append a ‘0’, ‘1’, ‘2’, etc to your local serial number to get the global serial number. As long as your devices have unique names, you get a unique global serial number.May 17, 2020 at 11:05 AM #40622
thank you for you reply! But I think is complicated for me to do all that, I mean the webservice service.
I was thinking to use a field with a create date option to get tax number which will be different in all devices.
Thank you!May 17, 2020 at 11:13 AM #40623
I think that will be what I’m looking for!
I’ll try the
utils.getDeviceName()and add the sequential number or I’ll take a combination of that number plus the incremental. Also I thought on a field with a creation date and time, which will be different on any device.
Thank you!May 17, 2020 at 5:33 PM #40630
If you are fine forgiving the number aspect, you can use a calculation field to generate a UUID that would be unique per record. If you set the calculation field to be a single execution (forget the exact name of the checkbox) then it’ll only evaluated the UUID once and never change the field.
If you’re fine with a composite ID, you could have a form that registers a device number based on the device name and use that to generate a device specific unique identifier that way as well.May 18, 2020 at 8:15 AM #40631
Finally I think I’m going to use Date to get the UUID:
var d = new Date();
var uuidfromdates = d.getFullYear() * 10000000000 + (d.getMonth() + 1) * 100000000 + d.getDate() * 1000000 + d.getHours() * 10000 + d.getMinutes() * 100 + d.getSeconds();
uuidfromdates will be like: 20200518120429
Then, uuidfromdates number will be different on any device…is a large number but is sequential, I mean the new one will be greater than the others. Also it helps my to know when it was generated :-)May 18, 2020 at 9:07 AM #40632
This works as long as you don’t create a new record on different devices at the exact same second.May 18, 2020 at 10:31 AM #40633
yes, you are right, but I think the possibilities are very small. Also I thought to use getMilliseconds()
By the way I added
utils.getDeviceName()to a new field to have a better tracking of all.
You must be logged in to reply to this topic.