Skip to main content
Known Participant
January 20, 2018
Answered

trouble with an if/then statement script

  • January 20, 2018
  • 1 reply
  • 1283 views

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

This topic has been closed for replies.
Correct answer George_Johnson

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.

1 reply

George_JohnsonCorrect answer
Inspiring
January 20, 2018

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.

Known Participant
January 22, 2018

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!

Inspiring
January 22, 2018

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.