Copy link to clipboard
Copied
I have a form that if the user supplies the date, I want to add 5 days and populate other fields.
So, when the user puts an address in the Transaction date, it needs to add 5 day and autofill in the due date of HOADate and NoSPDS. The code I have is giving me a syntax error 6 on Line 7.
Copy link to clipboard
Copied
Now about code placement.
1. Scripts that do the same thing should not exist in a document. Put the script in one place and one place only. Do not have multiple copies of it in different places in the document.
2. Fields where the user manually types into the field do not have calculation scripts. If you want something to happen after the user entered data, then use a Validation Script.
3. Readonly fields whose value is calculated from different fields have calculation scripts.
For your code. The action is being driven from the "TransactionDate" field. The other fields only change when the "TransactionDate" field changes, so the code needs to in the Validate event for the "TransactionDate" field.
I did not mean for you to run the script in the console window, just to look in it.
However, by running the code there you immediately found the error 🙂 so it worked.
Copy link to clipboard
Copied
No, remove the second line of code altogether. The "date" was changed once, it doesn't need to be changed again.
Like this:
date.setDate(date.getDate() +5);
this.getField("HOADate").value = util.printd("mm/dd/yyyy",date);
this.getField("NoSPDS").value = util.printd("mm/dd/yyyy",date);
Copy link to clipboard
Copied
Please post the code you are using.
Copy link to clipboard
Copied
var date = util.scand("mm/dd/yyyy",event.value);
if(event.value ==""){
this.getField("HOADate").value = "";
this.getField(”NoSPDS”).value = "";}
else {
date.setDate(date.getDate() +5);
this.getField("HOADate").value = util.printd("mm/dd/yyyy",date);
date.setDate(date.getDate() +5);
this.getField("NoSPDS").value = util.printd("mm/dd/yyyy",date);
}
This is the code I am using, it does not give me the error I was getting, but now does not return anything in the HOADate or the NoSPDS fields.
Copy link to clipboard
Copied
And where is this script placed?
Copy link to clipboard
Copied
script is placed in the TransactionDate custom calculate.
Copy link to clipboard
Copied
Ok, does the user manually enter the date into the "TransactionDate" field? If so, then the custom calculate is the exact wrong place to put the script. Please move the script to the Custom Validation script.
Then test it and tell us what errors are reported in the console window.
Copy link to clipboard
Copied
The way to fix this issue is to figure out what is being returned in "event.value", and if it is being convered to a date object correctly. The first part of this is to see what's being reported in the console window.
Please watch this video: https://www.pdfscripting.com/public/images/video/AcroJSIntro/AcroJSIntro_ConsoleWindow.cfm
But we still need to know where you placed this script, Field name, purpose, and type of script.
Copy link to clipboard
Copied
The calculation is in the field name "TransactionDate", under Custom Calculation Script in the Calculate Tab.
The calculation is not giving me an error at this time, however, it does not add 5 days to the TransactionDate and show that in the HOADate and NoSPDS fields like I wish.
The formula should be that the user enters a date in the TransactionDate field, it adds 5 days and then shows that date in numerous fields, such as the HOADate and the NoSPDS fields on the form.
Copy link to clipboard
Copied
If I use
var date = util.scand("mm/dd/yyyy",event.value);
if(event.value =="")
this.getField("HOADate").value = "";
else {
date.setDate(date.getDate() +5);
this.getField("HOADate").value = util.printd("mm/dd/yyyy",date);}
it will populate the HOADate field correctly by adding 5 days to the TransactionDate.
Copy link to clipboard
Copied
So the problem is probably with the "NoSPDS" field.
Did you move the script to the Validate event? This is important for proper operation of the form. Calculation script do not belong on fields where data is manually entered.
Next, for the script that has "NoSPDS" in the code. Is anything reported in the console window?
If not, then the next step is to remove each of the offending lines separately and see if there is a difference.
Add these lines back in
date.setDate(date.getDate() +5);
//this.getField("NoSPDS").value = util.printd("mm/dd/yyyy",date);
But notice that the second line is commented out. If the "HOADate" is filled correctly, then try changing the line that is commented:
//date.setDate(date.getDate() +5);
this.getField("NoSPDS").value = util.printd("mm/dd/yyyy",date);
This should fill both "HOADate" and "NoSPDS" with the same date. If not, there's your error.
Copy link to clipboard
Copied
I am thorougly confused. I have the calculation in the TransactionDate field. Should it be in the HOADate field and the NoSPDS fields?
I put
var date = util.scand("mm/dd/yyyy",event.value);
if(event.value ==""){
this.getField("HOADate").value = "";
this.getField(”NoSPDS”).value = "";}
else {
date.setDate(date.getDate() +5);
this.getField("HOADate").value = util.printd("mm/dd/yyyy",date);
date.setDate(date.getDate() +5);
this.getField("NoSPDS").value = util.printd("mm/dd/yyyy",date);
}
in the run custom validation under the Validate Tab and it still does not populate the fields.
This is what I see when I run the console Javascript:
var date = util.scand("mm/dd/yyyy",event.value);
if(event.value ==""){
this.getField("HOADate").value = "";
this.getField(”NoSPDS”).value = "";}
else {
date.setDate(date.getDate() +5);
this.getField("HOADate").value = util.printd("mm/dd/yyyy",date);
date.setDate(date.getDate() +5);
this.getField("NoSPDS").value = util.printd("mm/dd/yyyy",date);
}
SyntaxError: illegal character
4:Field:Validate
SyntaxError: illegal character
4:Field:Calculate
SyntaxError: illegal character
4:Field:Validate
SyntaxError: illegal character
4:Field:Calculate
SyntaxError: illegal character
4:Field:Validate
Copy link to clipboard
Copied
I changed the "HOADate" to "NoSPDS" and it populated just fine.
Copy link to clipboard
Copied
You found your error. There is an illegal double quote character exactly where it is telling you it is.
Here:
”NoSPDS”
Note how quotes look a bit fancy. This is a UNICODE character.
In JS, quoted string use plain ASCII quotes.
Like this:
"NoSPDS"
Copy link to clipboard
Copied
Alright, when I changed the code to this, it populated both fields. The HoaDate was correct by adding 5 to the TransactionDate but the NoSPDS field was not. The code added another 5 to the date, then making it 10 days from the TransactionDate.
Copy link to clipboard
Copied
That's exactly how the code is written.
// This line adds 5 days to the "date"
date.setDate(date.getDate() +5);
// This lines formats and saves the modified "date"
this.getField("HOADate").value = util.printd("mm/dd/yyyy",date);
// This line adds 5 days onto the "date" object, which was already modified
// with 5 additional days, bringing the total number of added days to 10
date.setDate(date.getDate() +5);
// This line formats and saves the modified "date", now with 10 days added
this.getField("NoSPDS").value = util.printd("mm/dd/yyyy",date);
Copy link to clipboard
Copied
So, if I just want to use the original 5 days, I can change the +5 on the NoSPDS to +0?
Copy link to clipboard
Copied
No, remove the second line of code altogether. The "date" was changed once, it doesn't need to be changed again.
Like this:
date.setDate(date.getDate() +5);
this.getField("HOADate").value = util.printd("mm/dd/yyyy",date);
this.getField("NoSPDS").value = util.printd("mm/dd/yyyy",date);
Copy link to clipboard
Copied
That works fantastically!! Thank you for your help!
Copy link to clipboard
Copied
You are welcome!! How about a "correct answer" for one of my posts?
Copy link to clipboard
Copied
Now about code placement.
1. Scripts that do the same thing should not exist in a document. Put the script in one place and one place only. Do not have multiple copies of it in different places in the document.
2. Fields where the user manually types into the field do not have calculation scripts. If you want something to happen after the user entered data, then use a Validation Script.
3. Readonly fields whose value is calculated from different fields have calculation scripts.
For your code. The action is being driven from the "TransactionDate" field. The other fields only change when the "TransactionDate" field changes, so the code needs to in the Validate event for the "TransactionDate" field.
I did not mean for you to run the script in the console window, just to look in it.
However, by running the code there you immediately found the error 🙂 so it worked.