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

Javascript

Community Beginner ,
Nov 26, 2018 Nov 26, 2018

Copy link to clipboard

Copied

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

Views

661

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
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

...

Likes

Translate

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

Copy link to clipboard

Copied

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.

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
community guidelines
Community Beginner ,
Nov 26, 2018 Nov 26, 2018

Copy link to clipboard

Copied

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

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
community guidelines
Community Expert ,
Nov 26, 2018 Nov 26, 2018

Copy link to clipboard

Copied

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

return-command.

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

Copy link to clipboard

Copied

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

}

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
community guidelines
Community Beginner ,
Nov 26, 2018 Nov 26, 2018

Copy link to clipboard

Copied

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

}

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

Copy link to clipboard

Copied

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".

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
community guidelines
Community Beginner ,
Nov 26, 2018 Nov 26, 2018

Copy link to clipboard

Copied

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."

});

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

Copy link to clipboard

Copied

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

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
community guidelines
Community Expert ,
Nov 26, 2018 Nov 26, 2018

Copy link to clipboard

Copied

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

app.execMenuItem("SaveAs");

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
community guidelines
Community Beginner ,
Nov 26, 2018 Nov 26, 2018

Copy link to clipboard

Copied

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."

});

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
community guidelines
Community Expert ,
Nov 26, 2018 Nov 26, 2018

Copy link to clipboard

Copied

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."

    });

}

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
community guidelines
Community Beginner ,
Nov 27, 2018 Nov 27, 2018

Copy link to clipboard

Copied

LATEST

This works perfectly. Thank you for 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
community guidelines
LEGEND ,
Nov 26, 2018 Nov 26, 2018

Copy link to clipboard

Copied

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!)?

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
community guidelines
Community Expert ,
Nov 26, 2018 Nov 26, 2018

Copy link to clipboard

Copied

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.

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

Copy link to clipboard

Copied

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

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

Copy link to clipboard

Copied

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

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
community guidelines