Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

JavaScript - Functions not running or giving error

New Here ,
Feb 21, 2022 Feb 21, 2022

Hi all!

I have attached the Form in question; my JS code is throwing some errors and I cannot figure out what the problem is.

Errors:

When opening the document:

SyntaxError: missing ; before statement
2:Document-Level:Dates

When tripping a form field trigger:

ReferenceError: Dates is not defined
1:Field:Mouse Down

Code:

The following is a copy paste from Document's All JS from Foxit PDF Editor Pro (this is where I created the code and where is runs successfully):

//-------------------------------------------------------------
//-----------------Do not edit the XML tags--------------------
//-------------------------------------------------------------

//<Document-Level>
//<ACRO_source>Dates</ACRO_source>
//<ACRO_script>
//<!--*********** belongs to:Document-Level:Dates ***********-->
function Dates(field){
let today = new Date();
let date = `${today.getFullYear()}-${today.getMonth()+1}-${today.getDate()}`;
return this.getField(field).value = date;
}
//</ACRO_script>
//</Document-Level>

//<Document-Level>
//<ACRO_source>Required</ACRO_source>
//<ACRO_script>
//<!--*********** belongs to:Document-Level:Required ***********-->
function Required()
{

let c = 0;

  for (let i = 0; i < this.numFields; i++) {

    let fieldName = this.getNthFieldName(i);

    let f = this.getField(fieldName);

    if (f.type != "button") {

      if (f.required == true && (f.value == "" || f.text == "")) {

        c++;

      }

    }
  
}

  if (c > 0) {

    app.alert("At least one required field is empty...",1);
  c = 0;

  }

return;

}
//</ACRO_script>
//</Document-Level>

//<AcroForm>
//<ACRO_source>page1:Date Prepared:Annot3:MouseDown:Action1</ACRO_source>
//<ACRO_script>
//<!--*********** belongs to:AcroForm:page1:Date Prepared:Annot3:MouseDown:Action1 ***********-->
Dates("Date Prepared");
//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>page1:Prepared by:Annot5:MouseDown:Action1</ACRO_source>
//<ACRO_script>
//<!--*********** belongs to:AcroForm:page1:Prepared by:Annot5:MouseDown:Action1 ***********-->
Required();
//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>page1:Principal Investigator:Annot82:MouseDown:Action1</ACRO_source>
//<ACRO_script>
//<!--*********** belongs to:AcroForm:page1:Principal Investigator:Annot82:MouseDown:Action1 ***********-->
Required();
//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>page1:Date Ordered:Annot85:MouseDown:Action1</ACRO_source>
//<ACRO_script>
//<!--*********** belongs to:AcroForm:page1:Date Ordered:Annot85:MouseDown:Action1 ***********-->
Dates("Date Ordered");
//</ACRO_script>
//</AcroForm>

As you can see, I have two fields simply calling the Document function "Dates(field)" and two more fields that are calling the "Required()" function.

 

Can anyone see or understand why I am getting this error?

TOPICS
JavaScript , PDF forms
1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
2 ACCEPTED SOLUTIONS
Community Expert ,
Feb 21, 2022 Feb 21, 2022

Date: I cannot understand your script, I changed it for a simpliest one.

Required: I replaced all "let" by "var".

 

Now it works fine.

 


Acrobate du PDF, InDesigner et Photoshoptographe

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 21, 2022 Feb 21, 2022
LATEST

Your script uses clever ECMAscript 6 code tricks including let and interpolated expressions in back tick string literals. But Acrobat doesn't use such a recent language base. Stick to the traditional basics. 

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 21, 2022 Feb 21, 2022

P.S. I forgot to explain what my code is doing.

Dates()

is simply getting today's date, putting it into a certain format, and returning that value to the field specified when the function was called.

Required()

is looping through each form field, making sure it is not a button, checking whether the field is required, checking if it is blank and then incrementing a counter. If the counter is greater than 0, a simple alert message will appear to remind the user to fill all of the required fields.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 21, 2022 Feb 21, 2022

Date: I cannot understand your script, I changed it for a simpliest one.

Required: I replaced all "let" by "var".

 

Now it works fine.

 


Acrobate du PDF, InDesigner et Photoshoptographe
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 21, 2022 Feb 21, 2022
LATEST

Your script uses clever ECMAscript 6 code tricks including let and interpolated expressions in back tick string literals. But Acrobat doesn't use such a recent language base. Stick to the traditional basics. 

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