These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Creating a date auto-generator

jpoma
Registered: Apr 18 2008
Posts: 5
Answered

Hi,

I want to do the following:

When a form is opened, the current date, and a unique number are displayed (the unique number is basically an integer representation of the date/time)

I put the following code in the 'initialize' script field of the text fields that I want to have populate:

Date: var thisDate = new Date();
this.rawValue = thisDate.getMonth()+1+"/"+thisDate.getDate()+"/"+thisDate.getFullYear()

ID: this.rawValue = (Math.floor((new Date())/1000))%1000000000;

Now, when I am in the Designer Environment, and preview the PDF, the code works - they change on each preview, but when I save it as a dynamic PDF and open the file repeatedly, the values don't change. It is as if once the file is created, those values are 'frozen'.

I obviously do not want this behaviour, and want the values to reflect the load time.

As well as using the initialize field, I tried form ready and layout ready, with the same results.

Can anyone make some suggestions as to what I can try?

Thanx,
Justin

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
I believe you are using the wrong event.

The description for the "initialize" event:

"You can use this event to perform actions when an object is first created, either as the result of a form filler action or during the form creation process. For example, you can use this event to control settings for new instances of a subform object that a form filler adds to the form by using a button."

Sounds like it should be used for dynamically creating additional dynamic form elements and populating them at the time of creatina.

While the "docReady" event's description:

"This event is the first one that initiates after the form opens in Acrobat or Adobe Reader. Any calculation or scripting tasks that require the full form, or that should only run once when the form filler first opens the form, should use this event. For example, you can use the docReady event to check the version of Acrobat or Adobe Reader and return a message to the form filler if the form filler must upgrade the pplication before filling the form."

Clearly idicates that if you need a value for the form calculated upon opening the form is the action to use.

George Kaiser

jpoma
Registered: Apr 18 2008
Posts: 5
I appreciate the reply. I tried the docReady event, and it is behaving the same way unfortunately. That is, it is not updating on load.

Any other suggestions?
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
First you have to be aware that JavaScript used within LiveCycle Designer is not JAVA! But a very unique subset of JavaScript with special extentions.

Drop the labels!

Do not use object names properties or methods as variable names. You will override the item with your name!

Be very careful of using "this" and "Date".

Be aware of field formats when putting data in predefined formatted fields!

Be aware that JavaScript will automatically convert number to character strings and cause all sorts of math calculaitons problems.

Add some messag e boxes or print to the JS console to see what is happening.


// docReady JavaScript for the current date field
var CurrDate = new Date();
var FormatDate = (1 + CurrDate.getMonth()) + "/" + CurrDate.getDate() + "/" + CurrDate.getFullYear();
console.println("CurrDate: " + CurrDate);
console.println("FromatDate: " + FormatDate);
$.formattedValue = FromatDate; // date formatted to MM/D/YYYY

// ID field docReady JavaScript
var ID = (Math.floor((new Date()) / 1000) ) %1000000000;
console.println("ID: " + ID);
$.rawValue = ID;

George Kaiser

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
To get the current date into a field:
FormCalc:
----- form1.#pageSet[0].Page1.DateTimeField1::docReady - (FormCalc, client) ------------------------

$.rawValue = Num2Date(Date())

JavaScript:
----- form1.#subform[0].TextField1::docReady - (JavaScript, client) --------------------------------

var CurrDate = new Date();
$.rawValue = (1 + CurrDate.getMonth()) + "/" + CurrDate.getDate() + "/" + CurrDate.getFullYear();


For the ID:
JavaScript:
----- form1.#subform[0].ID[0]::docReady - (JavaScript, client) -------------------------------------

// ID field docReady JavaScript
$.rawValue = (Math.floor((new Date()) / 1000) ) %1000000000;

George Kaiser

jpoma
Registered: Apr 18 2008
Posts: 5
Thank you!

It seems the 'this' and 'Date' were causing some problems. I appreciate your help!

Justin