unique identifier field with unique serial number

Community Beginner ,
Jan 24, 2019

Copy link to clipboard

Copied

I am using the code below to get date and time stamp on my file  but I also want to add serial number with it which is unique to identify it later when I see list of that. Can anyone please help.

// get the current date and time
var d = new Date();
this.getField("UniqueNumber").value = util.printd("yymmdd-HHMMss", d);

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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()); 

TOPICS
Acrobat SDK and JavaScript

Views

350

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

unique identifier field with unique serial number

Community Beginner ,
Jan 24, 2019

Copy link to clipboard

Copied

I am using the code below to get date and time stamp on my file  but I also want to add serial number with it which is unique to identify it later when I see list of that. Can anyone please help.

// get the current date and time
var d = new Date();
this.getField("UniqueNumber").value = util.printd("yymmdd-HHMMss", d);

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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()); 

TOPICS
Acrobat SDK and JavaScript

Views

351

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jan 24, 2019 0
Most Valuable Participant ,
Jan 24, 2019

Copy link to clipboard

Copied

Is the serial number a fixed string? If not, how do you want to generate it?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 24, 2019 0
Community Beginner ,
Jan 24, 2019

Copy link to clipboard

Copied

Yes it should be a fixed string

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 24, 2019 0
Most Valuable Participant ,
Jan 24, 2019

Copy link to clipboard

Copied

Then you can do it like this:

this.getField("UniqueNumber").value = "ABCD_" + util.printd("yymmdd-HHMMss", d);

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 24, 2019 0
Community Beginner ,
Jan 24, 2019

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 24, 2019 0
Most Valuable Participant ,
Jan 24, 2019

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 24, 2019 0
Community Beginner ,
Jan 24, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 24, 2019 0
Most Valuable Participant ,
Jan 24, 2019

Copy link to clipboard

Copied

How long should this random number be?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 24, 2019 0
Most Valuable Participant ,
Jan 24, 2019

Copy link to clipboard

Copied

Use this code to generate a 5-digit random number. It will also only fill in the value once, until the form is cleared:

if (this.getField("UniqueNumber").valueAsString=="")

    this.getField("UniqueNumber").value = Math.random().toString().substring(2,7) + "_" + util.printd("yymmdd-HHMMss", new Date());

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 24, 2019 0
Community Beginner ,
Jan 25, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 25, 2019 0
Most Valuable Participant ,
Jan 25, 2019

Copy link to clipboard

Copied

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()); 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 25, 2019 1
Community Beginner ,
Jan 25, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 25, 2019 1
Most Valuable Participant ,
Jan 25, 2019

Copy link to clipboard

Copied

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:

new Date().getTime()

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 25, 2019 0
Community Beginner ,
Jan 25, 2019

Copy link to clipboard

Copied

How do I do this? Can you please show me steps.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 25, 2019 0
Most Valuable Participant ,
Jan 25, 2019

Copy link to clipboard

Copied

Replace this:

util.printd("yymmdd-HHMMss", new Date())

With the code I provided before...

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 25, 2019 1
Community Beginner ,
Jan 25, 2019

Copy link to clipboard

Copied

Is there any way I can get that number in date_time format. So that number is separated by date and time

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 25, 2019 0
Most Valuable Participant ,
Jan 25, 2019

Copy link to clipboard

Copied

No... For that you should use the printd method.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 25, 2019 0
New Here ,
Apr 21, 2020

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 21, 2020 0
try67 LATEST
Most Valuable Participant ,
Apr 21, 2020

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 21, 2020 0