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

Javascript

Explorer ,
Nov 26, 2018 Nov 26, 2018

I am currently working on a pdf form that uses Javascript. I have a Javascript setup to validate that all required fields are filled in, followed by a save as, and then followed by email that populates the subject field using form fields. There are three javascripts built into one button. Is there anyway that I can put a stop in that if all required form fields aren't filled in that the other two javascripts won't run?

TOPICS
Acrobat SDK and JavaScript
1.6K
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

correct answers 1 Correct answer

Community Expert , Nov 26, 2018 Nov 26, 2018

Use this code:

var emptyFields = [];

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

     var f= this.getField(this.getNthFieldName(i));

     if (f.type!="button" && f.required && f.valueAsString==f.defaultValue) {

        emptyFields.push(f.name);

     }

}

if (emptyFields.length>0) {

     app.alert("Error! You must fill in the following fields:\n" + emptyFields.join("\n"));

} else {

    app.execMenuItem("SaveAs");

    var parts = this.getField("Job Name").valueAsString;

    var kind = this.getField("Today's Date").v

...
Translate
Community Expert ,
Nov 26, 2018 Nov 26, 2018

Not if they are separate items, no. But it should be pretty simple to combine them all into a single script, and then it will be possible.

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
Explorer ,
Nov 26, 2018 Nov 26, 2018

ok and how do I put a stop into my javascript that would do this?

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 ,
Nov 26, 2018 Nov 26, 2018

Using an if-condition or by placing the code in a function and calling the

return-command.

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 ,
Nov 26, 2018 Nov 26, 2018

You can't put in a stop, so you just don't do the things you don't want to happen.

if ( we_dont_have_to_stop )

{

  do_the_other ( things ) ;

}

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
Explorer ,
Nov 26, 2018 Nov 26, 2018

This is what my javascript looks like

var emptyFields = [];

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

     var f= this.getField(this.getNthFieldName(i));

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

          if ((f.type=="text" && f.value=="") || (f.type=="checkbox" && f.value=="Off")) emptyFields.push(f.name);

     }

}

if (emptyFields.length>0) {

     app.alert("Error! You must fill in the following fields:\n" + emptyFields.join("\n"));

}

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 ,
Nov 26, 2018 Nov 26, 2018

That's the first part, presumably. You say you have three pieces of JavaScript. You need to put the remaining two parts into an "else".

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
Explorer ,
Nov 26, 2018 Nov 26, 2018

Currently one button does three things consecutively not in the same script.

The next thing would just be execute a menu item > File > Save as > PDF

and the last part of my javascript would be this

var parts = getField("Text Field 1").value

var kind = getField("Text Field 2").value

var date = getField("Holiday").value

this.mailDoc({

cTo: "Becky.Tonn@email.com",

cSubject: parts + " - " + kind + " - " + date,

cMsg: "A new request has been sent from sales."

});

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 ,
Nov 26, 2018 Nov 26, 2018

All the parts need to be JavaScript. So see the saveAs method in the document class.

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 ,
Nov 26, 2018 Nov 26, 2018

They won't be able to use saveAs without a doc-level script. Instead, use this:

app.execMenuItem("SaveAs");

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
Explorer ,
Nov 26, 2018 Nov 26, 2018

Sorry but I am a little lost on this. Here is what I have setup so far in my Notepad. Where would I add the part about the save as? Sorry I am a graphic designer and not a programmer.

var emptyFields = [];

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

     var f= this.getField(this.getNthFieldName(i));

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

          if ((f.type=="text" && f.value=="") || (f.type=="checkbox" && f.value=="Off")) emptyFields.push(f.name);

     }

}

if (emptyFields.length>0) {

     app.alert("Error! You must fill in the following fields:\n" + emptyFields.join("\n"));

}

var parts = getField("Job Name").value

var kind = getField("Today's Date").value

var date = getField("Holiday").value

this.mailDoc({

cTo: "Becky.Tonn@bicgraphic.com",

cSubject: parts + " - " + kind + " - " + date,

cMsg: "A new request has been sent from sales."

});

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 ,
Nov 26, 2018 Nov 26, 2018

Use this code:

var emptyFields = [];

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

     var f= this.getField(this.getNthFieldName(i));

     if (f.type!="button" && f.required && f.valueAsString==f.defaultValue) {

        emptyFields.push(f.name);

     }

}

if (emptyFields.length>0) {

     app.alert("Error! You must fill in the following fields:\n" + emptyFields.join("\n"));

} else {

    app.execMenuItem("SaveAs");

    var parts = this.getField("Job Name").valueAsString;

    var kind = this.getField("Today's Date").valueAsString;

    var date = this.getField("Holiday").valueAsString;

    this.mailDoc({

        cTo: "Becky.Tonn@bicgraphic.com",

        cSubject: parts + " - " + kind + " - " + date,

        cMsg: "A new request has been sent from sales."

    });

}

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
Explorer ,
Nov 27, 2018 Nov 27, 2018
LATEST

This works perfectly. Thank you for the help.

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 ,
Nov 26, 2018 Nov 26, 2018

Can't saveAs be used without a filename and do a prompt, in just the same way as the execMenuItem (though that's a good solution I'd forgotten!)?

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 ,
Nov 26, 2018 Nov 26, 2018

https://forums.adobe.com/people/Test+Screen+Name  wrote

Can't saveAs be used without a filename and do a prompt, in just the same way as the execMenuItem (though that's a good solution I'd forgotten!)?

No, the cPath parameter is required.

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 ,
Nov 26, 2018 Nov 26, 2018

Like I said, you add an else

if (emptyFields.length>0) {

     app.alert("Error! You must fill in the following fields:\n" + emptyFields.join("\n"));

}

is what you want to happen if it FAILS. If it PASSES you continue to

else

{

do stuff ;

more stuff ;

}

It all goes in there

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 ,
Nov 26, 2018 Nov 26, 2018

Also you didn't add the code we gave you for replacing the save as.

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