Is the serial number a fixed string? If not, how do you want to generate it?
Yes it should be a fixed string
Then you can do it like this:
this.getField("UniqueNumber").value = "ABCD_" + util.printd("yymmdd-HHMMss", d);
Sorry my bad it's not a fixed string.
I have a form in which when user opens the form unique number and date and time stamp should be there in a text field which is read only. When user tries to save it in between and reopens the file, unique number and date and time stamp should not change and he should be able to make changes.
Also when he is trying to fill again from beginning it should generate new unique number with current date and time.
I don't know how is this possible, but was thinking is it can be done by pop up msg or something. I am not sure. Can you please help me.
So you want to generate a random number, or something like that?
Also, your other requirements are not very well defined. You need to specify exactly when the value should be updated, and when it shouldn't be.
Basically I have a form which will be used by multiple users. When a user open the form A random number should be generated with date and time stamp. This random number & date and time stamp should not change even if user saves this form and reopen it to make changes.
It should only change when user is trying to fill new form.
Also I have a clear form button so I was thinking when user tries to clear the form new number and date and time should be generated and if form is not cleared than random number (which is unique) date and time should remain same
How long should this random number be?
Use this code to generate a 5-digit random number. It will also only fill in the value once, until the form is cleared:
this.getField("UniqueNumber").value = Math.random().toString().substring(2,7) + "_" + util.printd("yymmdd-HHMMss", new Date());
So now what is happening is I have to click on unique number textbox every time I do clear form, than only new number is generated. What I want is when I click on clear button unique number should be automatically displayed
Then you need to move the command to the custom calculation script of the field, and adjust it like this:
if (event.target.value=="") event.value = Math.random().toString().substring(2,7) + "_" + util.printd("yymmdd-HHMMss", new Date());
Yes this worked.
Also is there any way I can convert date and time stamp to unique serial number (Not random) just like we do in Excel.
Thanks for all the help
You can use the getTime() method on a Date object to convert it to a number of milliseconds from a specific moment (the "Epoch time"), like this:
It will return something like "1548446838877".
This value is unique and reversible, ie you can take it and convert it back to a Date object to find out the exact time it represents.
How do I do this? Can you please show me steps.
util.printd("yymmdd-HHMMss", new Date())
With the code I provided before...
Is there any way I can get that number in date_time format. So that number is separated by date and time
No... For that you should use the printd method.
Hi there, I've used calculations provided. However, calculations work but it sets a default value from the beginning. When I tried sending this document, user(s) receive same number that I see on my preview screen. Do you know if there is any extra configuration that I should be doing?
The calculation scripts only execute when the value of a field in the file is changed.
If you want to run the code when the file is opened, for example, then it needs to be moved elsewhere, and also modified.