0

I have this code I've pieced together linked to an action button in this PDF. It is supposed to verify if all required fields are entered and if so, it will pull the name and employee number from the document and generate an email with the PDF attached. If it fails these checks, there's a pop-up that warns the user of the missing information.

The problem I have is that the when the code runs, you can bypass the warning and still email the document.

How do I adjust this to stop until all of the fields are answered?

Desired "Pseudo code": Check empty fields (get Manager's Name) (get manager's EE nubmer). If required fields (checkbox) and comments are empty, alert user. If it's good, build email subject line and text from these areas...

Here's the code as it stands:

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! This checklist is incomplete:\n" + emptyFields.join("\n"));
}

// Build the subject line text from several fields form fields
var subj_text = getField("Weekly Management Critical Items:").valueAsString;
subj_text += ": " + getField("Manager Name").value;
subj_text += "- " + getField("Manager EE#").valueAsString;

// Send the email
mailDoc({
    cTo: "dennis.aikens@fedex.com",

    cSubject: subj_text,

    cMsg: "Hey Dennis, here's my checklist for this week. \r" + "Let me know if you have any questions. Thank you."
});

I know it's a little long, so I appreciate you for reading.

Thanks for all your time and input!

Gary Thomas
  • 2,291
  • 1
  • 9
  • 21
  • Is this pseudo code or do you really not have flow control statements to prevent the `mailDoc` function from being called even if `emptyFields.length > 0`? In other words, what happens if you place all the code below the alert `if` statement in an `else` clause? – Nick Feb 19 '19 at 15:22
  • If all this code is contained in a single function, then in the block after `if (emptyFields.length > 0)`, just type `return;` – TKoL Feb 19 '19 at 15:23
  • Try adding `else { /* code block that should not always be executed */ }` – Peter B Feb 19 '19 at 15:24
  • Or you put a condition with `return false;` / `return true;` at the end of your javascript validation, and make this run to here or there based on the checklist - Or you clean up your html form's `action` method and make the posting via javascript once the requirements are fulfiled. – statosdotcom Feb 19 '19 at 15:27
  • -Nick, yes the first part was pseudo code; or at least what I understand it to be. I didn't have flow control statements and didn't really know where to place them. – Dennis Aikens Feb 20 '19 at 06:24
  • Statosdotcom and Peter- I gave both of these a try but kept getting a syntax error. I guess I just didn't know where to place them. I'm still very much a novice when it comes to writing code by hand. Thanks for your input! – Dennis Aikens Feb 20 '19 at 06:29

1 Answers1

1

You should write down the clean control flow code... for now you can use that

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! This checklist is incomplete:\n" + emptyFields.join("\n"));
}else{
sendMail();
}


var sendMail = function(){
// Build the subject line text from several fields form fields
var subj_text = getField("Weekly Management Critical Items:").valueAsString;
subj_text += ": " + getField("Manager Name").value;
subj_text += "- " + getField("Manager EE#").valueAsString;

// Send the email
mailDoc({
    cTo: "dennis.aikens@fedex.com",

    cSubject: subj_text,

    cMsg: "Hey Dennis, here's my checklist for this week. \r" + "Let me know if you have any questions. Thank you."
});
} 
Vasim Hayat
  • 909
  • 11
  • 16