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

I am having difficulty with if statements in this code. When I first tried it - it worked as expected. Is their a simpler way to write this and does anyone know of a good text book that I can get to help me improve my skills

Explorer ,
Feb 19, 2018 Feb 19, 2018

// Sets variables for the entire document and Automatically Populates certain variable based in inputs throughout the document.;

// Assigns variables for populating the rest of the document;

var ord = this.getField("Order").valueAsString;
var bol = this.getField("BOL").valueAsString;
var trac = this.getField("Tractor").valueAsString;
var trail = this.getField("Trailer").valueAsString;
var lead = this.getField("Lead").valueAsString;
var lpay = this.getField("LPay").valueAsString;
var team = this.getField("Team").valueAsString;
var tpay = this.getField("TPay").valueAsString;
var lead1 = this.getField("Lead1").valueAsString;
var lpay1 = this.getField("LPay1").valueAsString;
var team1 = this.getField("Team1").valueAsString;
var tpay1 = this.getField("TPay1").valueAsString;
var olec = this.getField("OLEC").valueAsString;
var trail1 = this.getField("Trailer1").valueAsString;
var refin = this.getField("Refinery").valueAsString;
var unit = this.getField("Unit").valueAsString;
var tmt = this.getField("TMT").value;
var tmt1 = this.getField("TMT1").value;

// Ensures that global variables do not have a values until certain conditions are met;

var ptrip = 0;
var ptrip1 = 0;
var drop = 0;
var drop1 = 0;
var hose = 0;
var border = 0;
var border1 = 0;
var mile = 0;
var mile1 = 0;
var local = 0;
var local1 = 0;
var ldr = 0;
var ldr1 = 0;
var ttpay = 0;
var ttpay1 = 0;

event.value = "";

if (lead.value !== null || lead.value !=="") {

      if (team.value !== null || team.value !== "") {

            if (lead1.value == null || lead1.value == "") {

               event.value=team;
               var tpayz = tpay;
               var ptrip = 9.45;
               var ptrip1 = 9.45;
               var drop = 9.45;
               var drop1 = 9.45;
               var hose = 10.50;
               var border = 78.75;
               var border1 = 78.75;
               var mile = .305;
               var mile1 = .305;
               var ldr = tmt;
               var ldr1 = tmt;
}
           if ((lead1.value !== null || lead1.value !== "")&&(team1.value == null || team1.value == "")) {

               event.value=lead1;
               var tpayz = lpay1;
               var ptrip = 9.45;
               var ptrip1 = 18.9;
               var drop = 9.45;
               var drop1 = 18.9;
               var hose = 10.5;
               var border = 78.75;
               var border1 = 157.50;
               var mile = .305;
               var mile1 = .483;
               var ldr = tmt;
               var ldr1 = tmt1;
}

        if ((lead1.value !== null || lead1.value !== "")&&(team1.value !== null || team1.value !== "")) {

               event.value=lead1;
               var tpayz = lpay1;
               var ptrip = 9.45;
               var ptrip1 = 9.45;
               var drop = 9.45;
               var drop1 = 9.45;
               var hose = 10.5;
               var border = 78.75;
               var border1 = 78.75;
               var mile = .305;
               var mile1 = .305;
               var ldr = tmt;
               var ldr1 = tmt1;
}}

      if (team.value == null || team.value == "") {

            
         if ((lead1.value !== null || lead1.value !== "")&&(team1.value == null || team1.value == "")) {

               event.value=lead1;
               var tpayz = lpay1;
               var ptrip = 18.9;
               var ptrip1 = 18.9;
               var drop = 18.9;
               var drop1 = 18.9;
               var hose = 10.5;
               var border = 157.5;
               var border1 = 157.50;
               var mile = .483;
               var mile1 = .483;
               var ldr = tmt;
               var ldr1 = tmt1;
}

         if ((lead1.value !== null || lead1.value !== "")&&(team1.value !== null || team1.value !== "")) {

               event.value=lead1;
               var tpayz = lpay1;
               var ptrip = 18.9;
               var ptrip1 = 9.45;
               var drop = 18.9;
               var drop1 = 9.45;
               var hose = 10.5;
               var border = 157.5;
               var border1 = 78.75;
               var mile = .483;
               var mile1 = .305;
               var ldr = tmt;
               var ldr1 = tmt1;
}}}

TOPICS
Acrobat SDK and JavaScript , Windows
301
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 ,
Feb 19, 2018 Feb 19, 2018

Lines like this:

if (lead.value !== null || lead.value !=="") {

should be:

if (lead) {  // If lead is not an empty string...

A field value will never be equal to the special JavaScript value of null, so there's no need to test against it. In the above example, the variable lead is the value of the field as a string, so it won't have a value property itself.

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 ,
Feb 19, 2018 Feb 19, 2018

I am not understanding how do I test if the field is empty with your example. Would it be a simple    "if (lead) {   }  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 ,
Feb 19, 2018 Feb 19, 2018

Thank You - it should clean it up a lot.

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 ,
Feb 19, 2018 Feb 19, 2018
LATEST

Yes.

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 ,
Feb 19, 2018 Feb 19, 2018

You should only use the "var" keyword to define a variable once. After that refer to it directly, or you're risking having multiple copies of the same variable in different scopes, which is not a good idea.

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