Populating multiline text field from combo box using nested if - else if

New Here ,
May 04, 2018 May 04, 2018

Copy link to clipboard

Copied

I have Acrobat Pro DC with a combo/dropdown box ("Regulation) which is populated by another parent dropdown box. When I change the value on "Regulation" box, i.e.: select a Regulation), it populates yet another combo box ("RegSections") with the appropriate sections for the particular regulation selected such as: 2, 2(1), 2(2) etc.. See image of fields below.

adobe.png

That all works great as they're using the "Case" command. BUT when I choose a section from the last box, it's supposed to populate a text box with the appropriate regulatory description for the section selected. It is a good 1225 lines long and works fine on the "RegSections" Blur event but that means an officer would need to click outside the box before the proper text shows up in the text box ("sfd_reg"). Currently the js is simply a nested if/else-if setup.

What I've been trying to do is make it work on the "RegSections" Custom Keystroke event. So I added a if(!event.willcommit){ to the top of the script and the closing "}" right at the end so it encapsulates the entire script. However, this approach doesn't seem to be working as effectively as the Blur event because when I select a different section, the text field is not updated until I perform the selection a second time. (???) I hope this all makes sense.

Below are two versions of a portion of the script that I've chopped up into small versions (didn't see an option here for inserting a code block):

1) In the BLUR EVENT (recap: works but have to click outside box, no immediate update to text field "sfd_reg":

//Setup form variables

var reg = this.getField("Regulation").value;

var rsec = this.getField("RegSections");

var rsf = this.getField("sfd_reg");

if (reg == "Antarctic Environmental Protection Regulations (AEPR)") {

    if (rsec.value  == "12(1)(a)") {

        rsf.value = "Failure to ensure that specified persons receive specified training";

    } else if (rsec.value == "12(1)(b)") {

        rsf.value = "Failure to inform specified persons of permit and Regulations requirements";

    } else if (rsec.value == "12(1)(c)") {

        rsf.value = "Failure to provide specified report within specified period";

    }

} else if (reg == "Concentration of Phosphorous in Certain Cleaning Products Regulations (CPCCPR)") {

    if (rsec.value == "8") {

        rsf.value = "Failure to maintain record containing information as specified";

    }

} else if (reg == "Export and Import of Hazardous Waste and Hazardous Recyclable Materials Regulations (EIHWHRMR)") {

    if (rsec.value == "3(a)") {

        rsf.value = "Releasing, allowing or causing release of halocarbon from specified system, container or device";

    } else if (rsec.value == "3(b)") {

        rsf.value = "Releasing, allowing or causing release of halocarbon from specified system, container or device";

    }

} else {

    app.alert("No Regulation selected", 3, 0, "Select a Regulation");

    rsf.value = "Choose an Act and Section";

}

2) In the CUSTOM KEYSTROKE (recap: kind've works but have to make selection twice before "sfd_reg" is populated with desired text (no one would like that and it's not intuitive):

//Setup form variables

var reg = this.getField("Regulation").value;

var rsec = this.getField("RegSections");

var rsf = this.getField("sfd_reg");

if(!event.willCommit)

{

if (reg == "Antarctic Environmental Protection Regulations (AEPR)") {

    if (rsec.value == "12(1)(a)") {

        rsf.value = "Failure to ensure that specified persons receive specified training";

    } else if (rsec.value == "12(1)(b)") {

        rsf.value = "Failure to inform specified persons of permit and Regulations requirements";

    } else if (rsec.value == "12(1)(c)") {

        rsf.value = "Failure to provide specified report within specified period";

    }

} else if (reg == "Concentration of Phosphorous in Certain Cleaning Products Regulations (CPCCPR)") {

    if (rsec.value == "8") {

        rsf.value = "Failure to maintain record containing information as specified";

    }

} else if (reg == "Export and Import of Hazardous Waste and Hazardous Recyclable Materials Regulations (EIHWHRMR)") {

    if (rsec.value == "3(a)") {

        rsf.value = "Releasing, allowing or causing release of halocarbon from specified system, container or device";

    } else if (rsec.value == "3(b)") {

        rsf.value = "Releasing, allowing or causing release of halocarbon from specified system, container or device";

    }

} else {

    app.alert("No Regulation selected", 3, 0, "Select a Regulation");

    rsf.value = "Choose an Act and Section";

}

}

HELP. Thanks!!!!

TOPICS
Acrobat SDK and JavaScript, Windows

Views

242

Likes

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
Adobe Community Professional ,
May 07, 2018 May 07, 2018

Copy link to clipboard

Copied

HI,

I think the problem here is that drop-down lists are not updated by default with the value until after you click away from the field, if you look at the settings for your drop-down field, there is a checkbox labeled "Commit selected value immediately" on the Options tab of the field preferences. If you set that option and possibly move your code to the Calculation Validation event then it should update upon selection.

Word of caution, if your script is long as it appears it may be, placing it on the calculate event might cause issues with the performance of your form, if this is the case then it might be worth storing the value of the field the last time it changed and comparing this to the current value so that you only run the calculation when the drop down you care about has changed.

If this is not suitable then your second option should work, although it all depends on when the event is triggered.

Hope this helps

Malcolm

Edited : for Clarity and to incorporate MatLac​ suggestions, meaning it makes more sense for anyone finding this thread later.

Likes

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
Engaged ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

It is best practice to use VALIDATION scripts instead of CALCULATION scripts whenever possible.  That way, you make sure the script doesn't run for nothing each and every time something that has nothing to do with the script changes.  I tend to avoid using the BLUR event exactly for what you describe.  The user has to KNOW he needs to exit the field for the script to run.

When using dropdowns, always check "Commit selected value immediately" and run scripts from the validate tab instead. 

Also, resort to "export values" instead of using long strings that could lead to potential Typos.  You can set export values just under the element field of the option tab of a dropdown menu.  You can use abbreviations or maybe numbers.  Whan an export value is used, it is returned by the value property of the field object instead of the element value.

This:

if (reg == "Antarctic Environmental Protection Regulations (AEPR)")

Should read as this:

if (reg == "AEPR")

Likes

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 ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

Hey MatLac, thanks for the pointers, much appreciated:

Honestly, I don't really know the difference between calc and validation, but will take what you say under advisement. At least I got the Blur issue.

Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10

Likes

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
Engaged ,
May 09, 2018 May 09, 2018

Copy link to clipboard

Copied

The difference is important.  The calculate event has nothing to do with calculation in a mathematical way.  The validation event takes place when you commit the value to a specific field while the calculate event takes place whenever a value is commited to ANY field, regardless if it has anything to do with your script or your field.  It sort of re-evaluate the integrity of a field in regards to the whole document everytime the document's integrity has changed

It will not matter if you have only some fields on a document but if you have many (like hundreds) you will want to avoid each script to fire everytime something changes.

For example, let's say you have a script that, when a value is entered, switches the value to uppercase.

event.value = event.value.toUppercase()

Assume your form has 1000 fields named field1 all the way to field1000.  The script will work regardless of where you put it (validate or calculate).  If you put it in the validate script of field22, script 22 will trigger only when value 22 is updated, while all other 999 scripts will not trigger.  If you put it in the calculate tab of all 1000 fields, each time one of the field is updated, you will trigger 1000 scripts one after the other (for nothing since other values didn't change).

Now, the correct way to implement such a script would be at the keystroke event, which happens as soon as a key is pressed, updating the value of the keystroke (also taking care of the eventuality of a Copy/Paste)

event.change = event.change.toUpperCase()

Likes

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 ,
May 09, 2018 May 09, 2018

Copy link to clipboard

Copied

LATEST

Now that's gold. I appreciate your follow-up and subsequent clarification!!

Completely agree. I will implement this logic.

Cheers!!

CW

Likes

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