There are several problems with your code: The first one is that you are not actually checking the value of a required field: It does not matter what the field is set to, you are always displaying the message. The next problem is that you are not excluding buttons from your check - a button does not have a "required" property, so it will actually throw an exception and your script will stop. The next problem is that your submission routine will be called as soon as the first field that has a value is found, regardless of what the other fields are set to. Try this instead:
// Email for "To" field
var cToAddr = ""
var cCCAddr = ""
var cBCCAddr = this.getField("BRANCH_DROPDOWN").value
var VanNum = this.getField("VAN_NUM").value;
var Date = this.getField("WORK_ORDER_DATE").value;
var invoiceTo = this.getField("INVOICE_TO").value;
// Flatten PDF (Read Only). This doen't work in PDF Expert, therefore it's disabled
// this.flattenPages();
// Set the subject and body text for the email message
var cSubLine = "Complete Service WO for " + invoiceTo + " -- " + Date;
var cBody = "Hello, \n \n" + "The Work Order opened " + Date + " for " + invoiceTo + " has been completed and attached for your records. \n \n" + "Please feel free to contact our service department with any questions, contact details have been provided below. \n \n" + "Thank you for your business! \n \n" + "Service Technician, Van#: " + VanNum + "\n" + "Southeast Industrial Equipment \n" + "12200 Steele Creek Rd, Charlotte NC 28273 \n" + "704-399-9700";
(function() {
var formIsComplete = true;
for (var i = 0; i < this.numFields; i++) {
var fieldName = this.getNthFieldName(i);
var f = this.getField(fieldName);
if (f != null && f.type != "button" && f.required == true && f.value == "") {
app.alert("Please complete field " + fieldName);
//event.target.textColor = color.red;
formIsComplete = false;
break;
}
}
if (formIsComplete) {
// Prompt the user with a Yes/No question
var resp = app.alert({
cMsg: "Click 'YES' to complete\n" + "(Read-Only PDF)\n\n" + "Click 'NO' to edit",
nIcon: 1,
nType: 2,
cTitle: "Submit Service WO Form?"
});
// If the user clicked the "Yes" button...
if (resp === 4) {
// make all fields in a form read only;
var oField; // variable for field being processed;
// loop through the form fields;
for (var i = 0; i < this.numFields; i++) {
// process each field name;
oField = this.getField(this.getNthFieldName(i)).readonly = true;
}
// Send the email
try {
mailDoc({
bUI: true,
cTo: cToAddr,
cCc: cCCAddr,
cBcc: cBCCAddr,
cSubject: cSubLine,
cMsg: cBody
});
} catch (e) {
app.alert("Could not send email, sorry.", 1);
}
}
}
})();
As you can see, I've rearranged some things. What is still not working is the checking for the dropdown control: For this to work, you would have to check for your default value, which would make things a bit more complicated. If you can make sure that none of the other fields can ever be set to the default value of the dropdown control, you can use the following as your "if" condition. This assumes that your dropdown default value is one space (" ") character:
if (f != null && f.type != "button" && f.required == true && (f.value == "" || f.value == " ")) {
Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com