6 Replies Latest reply on Feb 27, 2018 11:58 AM by adamr73245379

    trouble with an if/then statement script

    adamr73245379

      hello, i am new to acrobat javascript but have been managing pretty well following forum advice i have found for each topic i have come across until now. I am trying to populate a field with an if/then statement. I have employee names that I want to be populated into a certain field when the adjacent field has the 4 digit employee number entered into it. I found an old thread but no matter what I tried, i can not get the script to work. Here is what I have so far. For reference, Emp.0 is the employee number field and NameRow.0 is the employee name field. I have the following formula plugged in to the custom calculation script of the NameRow.0 field. Employee numbers range from 0190 to 7xxx so i didnt know if have a leading zero in the employee number is throwing anything off either for the first two i was trying to get to work.

       

      var v1 = +getField("Emp.0").value;  

       

      if (v1 === 0190) event.value = "jane doe"; 

      if (v1 === 0210) event.value = "john smith";

       

      I have also tried this formula but had it pasted into the NameRow.0 calculation script.

       

      var v1 = +event.value;

      var f2 = getField("NameRow.0");

       

      if (v1 === 0190) f2.value = "jane doe";

      if (v1 === 0210) f2.value = "john smith"

       

      I am also hoping that when no employee number is listed or one is removed, the name field will return or stay empty.

       

      Thanks,

      Adam

        • 1. Re: trouble with an if/then statement script
          George_Johnson MVP & Adobe Community Professional

          It would make more sense to do something like this:

           

          var v1 = getField("Emp.0").valueAsString; 

           

          if (v1 === "0190") event.value = "jane doe";

          if (v1 === "0210") event.value = "john smith";

           

           

          Or better yet, use something like the following as the custom Validate script of the Emp.0 field:

           

          // Create an object to associate employee numbers with names

          var oEmpNumName = {

              "0190" : "Jane Doe",

              "0210" : "John Smith",

              "1234" : "Joe Blow"

          }

           

          // Populate the name field with the corresponding name, or blank if employee number field is empty

          getField("NameRow.0").value = event.value ? oEmpNumName[event.value] : "";

           

           

          The oEmpNumName object could be defined elsewhere as long as it's loaded before the validate script is triggered (e.g., in a document-level JavaScript that runs when the form is opened), but I included it here for convenience.

           

          • 2. Re: trouble with an if/then statement script
            adamr73245379 Level 1

            Thanks George. I used the second of the two solutions and it worked perfectly. Let you know if I get stuck again with anything.

             

            As for your last comment, I am not quite sure what you mean by a document level script. Regardless the formula worked so I didnt look into it much more than verifying the script worked.


            Thanks again!

            • 3. Re: trouble with an if/then statement script
              George_Johnson MVP & Adobe Community Professional

              I left out something that I had intended to include. If the user enters a number that isn't defined in the list, it should return some sort of indication, something like the following:

               

              // Populate the name field with the corresponding name, or blank if employee number field is empty

              getField("NameRow.0").value = event.value ? oEmpNumName[event.value] || "Unknown employee number" : "";

               

               

              Instead of using the "Unknown employee number" string, you could use an empty string ("") or something else.

              • 4. Re: trouble with an if/then statement script
                adamr73245379 Level 1

                George,

                 

                Something came up when I was doing a soft test with out guys in the field and this plays into your last comment you gave me. In some cases a new employee will not be in the system so we will need to be able to custom input their name. Is there a way to set this feature into the equation or check it in a setting somewhere?

                 

                Right now what happens is when I enter a name into the form field, it will erase once I tab to the next field because it doesnt have a employee number associated with it.

                 

                Thanks,
                Adam

                • 5. Re: trouble with an if/then statement script
                  George_Johnson MVP & Adobe Community Professional

                  Can you post the script you're using. Feel free to change the employee names.

                  • 6. Re: trouble with an if/then statement script
                    adamr73245379 Level 1

                    I used one of your scripts listed above. Here is what it says (with names changed).

                     

                    // Create an object to associate employee numbers with names

                    var oEmpNumName = {

                    "190" : "Jane Doe",

                    "210" : "John Doe",

                    "247" : "John Smith",

                     

                    }

                     

                    // Populate the name field with the corresponding name, or blank if employee number field is empty

                    getField("NameRow1").value = event.value ? oEmpNumName[event.value] : "";

                     

                    I tried to remove "blank" and write "allow for custom value if..." but it didnt work. Again, that was me just taking a stab at javascript

                     

                    Thanks,

                    Adam