Skip to main content
Known Participant
November 26, 2018
Answered

Javascript

  • November 26, 2018
  • 6 replies
  • 1753 views

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?

This topic has been closed for replies.
Correct answer try67

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

});


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

    });

}

6 replies

Legend
November 26, 2018

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

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

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

try67
Community Expert
Community Expert
November 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.

Legend
November 26, 2018

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

try67
Community Expert
Community Expert
November 26, 2018

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

app.execMenuItem("SaveAs");

Known Participant
November 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."

});

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

Known Participant
November 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."

});

try67
Community Expert
Community Expert
November 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.

Known Participant
November 26, 2018

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

try67
Community Expert
Community Expert
November 26, 2018

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

return-command.