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

trouble with an if/then statement script

New Here ,
Jan 19, 2018 Jan 19, 2018

Copy link to clipboard

Copied

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

TOPICS
Acrobat SDK and JavaScript , Windows

Views

752

Translate

Translate

Report

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

correct answers 1 Correct answer

LEGEND , Jan 19, 2018 Jan 19, 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

...

Votes

Translate

Translate
LEGEND ,
Jan 19, 2018 Jan 19, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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
New Here ,
Jan 22, 2018 Jan 22, 2018

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

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 ,
Jan 22, 2018 Jan 22, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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
New Here ,
Feb 27, 2018 Feb 27, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
New Here ,
Feb 27, 2018 Feb 27, 2018

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

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