Combine 2 Javascripts Into 1

Community Beginner ,
Apr 30, 2019 Apr 30, 2019

Copy link to clipboard

Copied

Hi guys

I need to combine the below 2 java scripts into 1. I would be most grateful if someone could advise if this is possible and if so how do I do it? I have marked the broken the scripts as **Script 1** and **Script 2**.

**Script 1**

//Start Script

//First you make the values for box1.

//var myPlanTypeValues = ["--SELECT--", "Leadership", "Mid-Management", "Sales"];

//Then you populate the values to box1.

//this.getField("PlanType").setItems(myPlanTypeValues);

// Then you define what has to be shown in box2 when value is selected

var accountingData = {
"Leadership": ["--SELECT--","MIB"],

"Mid-Management": ["--SELECT--","Local Bonus Plan"],

"Sales": ["--SELECT--","#1 Sales Account Manager – Aftermarket/Direct","#2 Sales Account Manager – OEM/Direct","#3 Sales Account Manager – OEM/Distribution","#4 Sales Account Manager – OEM/Overlay","#5 Sales Manager – Any Products","#6 Selling Sales Manager – Any Products","#7 New Business Developer – Aftermarket/Direct","#8 New Business Developer – OEM/Direct","#9 Sales Manager, New Business – Any Product","#10 Selling Sales Manager, New Business – Any Product","#11 Account Manager – Aftermarket/Direct","#12 Account Manager – OEM/Direct","#13 Account Manager – Aftermarket/Distribution","#14 Sales Manager, Existing Accounts – Any Product","#15 Account Service – Aftermarket/Direct","#16 Account Service – OEM/Direct","#17 Manager, Account Service – Any Product/Direct","#18 Growth Representative – Aftermarket/Distribution","Legacy Plan","TFS Sales VIC"],
"Shared Earnings/TM/Recognition": ["--SELECT--","Gainsharing Program","Local Bonus Plan","Materials Processing Incentive Bonus (MPIB)","Productivity Improvement Bonus (PIB)","Professional Incentive Bonus (PIB)", "Profit Sharing", "Recognition","SEP (Shared Earnings Plan)","SEP Plus","SSBP (Strategic Sourcing Bonus Plan)","Statutory Bonus Plan"],
};
//Finally you set the values of box2
function SetFieldValues(cDeptName){
this.getField("LocalBonus").setItems(accountingData[cDeptName]);
}
//Custom Keystroke PlanType
//if( event.willCommit ){
if(event.value == "--SELECT--"){
//app.alert(event.value)
//if nothing is chosen in box1 then box2 will be cleared
this.getField("Local Bonus").clearItems();
}else {
//app.alert(event.value)
SetFieldValues(event.value);
}
//
//ENDSCRIPT
//Finally you set the values of box2
function SetFieldValues(cDeptName){
this.getField("LocalBonus").setItems(accountingData[cDeptName]);
}
//Custom Keystroke PlanType
//if( event.willCommit ){
if(event.value == "--SELECT--"){
//app.alert(event.value)
//if nothing is chosen in box1 then box2 will be cleared
this.getField("LocalBonus").clearItems();
}else {
//app.alert(event.value)
SetFieldValues(event.value);
}

//}
//ENDSCRIPT

var accountingData = {
"Leadership": ["--SELECT--","MIB"],

"Mid-Management": ["--SELECT--","Local Bonus Plan"],

"Sales": ["--SELECT--","#1 Sales Account Manager – Aftermarket/Direct","#2 Sales Account Manager – OEM/Direct","#3 Sales Account Manager – OEM/Distribution","#4 Sales Account Manager – OEM/Overlay","#5 Sales Manager – Any Products","#6 Selling Sales Manager – Any Products","#7 New Business Developer – Aftermarket/Direct","#8 New Business Developer – OEM/Direct","#9 Sales Manager, New Business – Any Product","#10 Selling Sales Manager, New Business – Any Product","#11 Account Manager – Aftermarket/Direct","#12 Account Manager – OEM/Direct","#13 Account Manager – Aftermarket/Distribution","#14 Sales Manager, Existing Accounts – Any Product","#15 Account Service – Aftermarket/Direct","#16 Account Service – OEM/Direct","#17 Manager, Account Service – Any Product/Direct","#18 Growth Representative – Aftermarket/Distribution","Legacy Plan","TFS Sales VIC"],

"Shared Earnings/TM/Recognition": ["--SELECT--","Gainsharing Program","Local Bonus Plan","Materials Processing Incentive Bonus (MPIB)","Productivity Improvement Bonus (PIB)","Professional Incentive Bonus (PIB)","Profit Sharing","Recognition","SEP (Shared Earnings Plan)","SEP Plus","SSBP (Strategic Sourcing Bonus Plan)","Statutory Bonus Plan"],

};

//Finally you set the values of box2

function SetFieldValues(cDeptName){

this.getField("LocalBonus").setItems(accountingData[cDeptName]);

}

//Custom Keystroke Plan Type

//if( event.willCommit ){
if(event.value == "--SELECT--"){
//app.alert(event.value)
//if nothing is chosen in box1 then box2 will be cleared

this.getField("Local Bonus").clearItems();

}else {
//app.alert(event.value)
SetFieldValues(event.value);
}

//

//ENDSCRIPT

//Finally you set the values of box2

function SetFieldValues(cDeptName){

this.getField("Local Bonus").setItems(accountingData[cDeptName]);

}

//Custom Keystroke Plan Type

//if( event.willCommit ){
if(event.value == "--SELECT--"){
//app.alert(event.value)
//if nothing is chosen in box1 then box2 will be cleared

this.getField("Local Bonus").clearItems();

}else {
//app.alert(event.value)
SetFieldValues(event.value);
}

//}

//ENDSCRIPT

//Finally you set the values of box2

function SetFieldValues(cDeptName){

this.getField("LocalBonus").setItems(accountingData[cDeptName]);

}

//Custom Keystroke PlanType

//if( event.willCommit ){
if(event.value == "--SELECT--"){
//app.alert(event.value)
//if nothing is chosen in box1 then box2 will be cleared

this.getField("LocalBonus").clearItems();

}else {
//app.alert(event.value)
SetFieldValues(event.value);
}

//}

//ENDSCRIPT

**Script 2**

var fields = ["Sub Plan",];

//

for (var i in fields) {
var f = this.getField(fields);
if (event.value!="Professional Incentive Bonus (PIB)") {
f.required = true;
f.fillColor = color.transparent;
f.readonly = false;
} else {
f.required = false;
f.fillColor = color.black;
f.readonly = true;
}
}

TOPICS
Acrobat SDK and JavaScript

Views

191

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

correct answers 1 Correct Answer

Adobe Community Professional , Apr 30, 2019 Apr 30, 2019
There are a number of problems with your scripts:Having a validation script is sufficient. The validation script will get called any time the field value changes, so there is no need to also have a mouse down event that is trying to do the same thing. So remove the mouse down actions from your dropdown fields.To have the validation script act immediately after the user makes a selection, go to the "Options" tab for the field properties of these dropdown and select the "Commit values immediately"...

Likes

Translate

Translate
Adobe Community Professional ,
Apr 30, 2019 Apr 30, 2019

Copy link to clipboard

Copied

Where is the end of script 1?

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
Community Beginner ,
Apr 30, 2019 Apr 30, 2019

Copy link to clipboard

Copied

Hi Bernd

It ends

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 ,
Apr 30, 2019 Apr 30, 2019

Copy link to clipboard

Copied

Why do use 5 custom keystroke scripts there?

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
Community Beginner ,
Apr 30, 2019 Apr 30, 2019

Copy link to clipboard

Copied

Hi Bernd

I'm not sure what you mean. The bones of these scripts have been put together through different people on this help form. I have just added the necessary options.

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 ,
Apr 30, 2019 Apr 30, 2019

Copy link to clipboard

Copied

Look at the parts beginning with

//Custom Keystroke ...

And look at the console for errors.

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 ,
Apr 30, 2019 Apr 30, 2019

Copy link to clipboard

Copied

As Bernd pointed out, there is quite a bit of duplicated code in your script. Also, you have the same script for both the "Plan Type" and the "Local Bonus" dropdown control. That does not look right. I assume you want to set the "Sub Plan" dropdown based on the "Local Bonus" control.

We know that if the user selects "Professional Incentive Bonus (PIB)", you want the "Sub Plan" to "black out" - but we don't know exactly what you mean by "black out" (that's not a term that is normally used in the context of forms). And, what should happen if the user selects any of the other options?

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
Community Beginner ,
Apr 30, 2019 Apr 30, 2019

Copy link to clipboard

Copied

Hi Guys

Forgive me I know next to nothing when it comes to JavaScript. I see now were there are duplications. I can remove them. Thanks Bernd. Karl that's correct. When I say black out I mean I what the Sub Plan field to with the colour black?

Perhaps there are duplications in the script because I'm inserting it incorrect in the tabs. With my lack of experience in JavaScript I am adding the same script into both the 'Actions' and 'Validate' tabs when I don't know what the difference is between the 2?

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 ,
Apr 30, 2019 Apr 30, 2019

Copy link to clipboard

Copied

Ah, I had not even looked at that. Yes, that is wrong. But regardless of that duplication, you have the same block duplicated in the script you posted above.

So with black out, you actually mean to paint the field black. Would it be sufficient to just make the field read-only, or hide it, so that nobody can use it? A black rectangle would look more like something got redacted and would not (at least in my opinion) indicate that a field cannot be used.

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
LEGEND ,
Apr 30, 2019 Apr 30, 2019

Copy link to clipboard

Copied

It's a big problem with scripts that people rarely tell us where to put them, and many people start by reusing fragments of scripts that they find. Unfortuately, it's very important where they go. Experienced programmers will know whether they have a validation script, a formatting script, a calculation script, a document level script or whatever, but people starting out often guess. Please don't - ask, or things will go downhill fast!

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 ,
Apr 30, 2019 Apr 30, 2019

Copy link to clipboard

Copied

There are a number of problems with your scripts:

Having a validation script is sufficient. The validation script will get called any time the field value changes, so there is no need to also have a mouse down event that is trying to do the same thing. So remove the mouse down actions from your dropdown fields.

To have the validation script act immediately after the user makes a selection, go to the "Options" tab for the field properties of these dropdown and select the "Commit values immediately" checkbox.

As I said before, there is code that is duplicated. That has to be removed.

There are errors reported in the JavaScript console. One is about a field not being found, the other one is about an invalid argument type. The first error is caused by you using two different versions of the field name for the target field: "Local Bonus" and "LocalBonus" - one with and one without the space between the two words. The one without is correct, so the one that tries to access "Local Bonus" will fail because you don't have a field with that name. The second problem happens when you select "Probation Pending" as plan type - because you did not define a an array with values for that case in your accountingData data structure.

As TSN said, collecting snippets, copying and pasting them without knowing what you are doing is dangerous, and unless you know what your are doing, it's very unlikely that you will end up with a solution that works. You need to spend some time learning about what the different script types do. A good way to do that is by going to the now read-only site http://acrobatusers.com and looking at the form related tutorials. This will give you a better idea of what you need to do, and also, where to look when something is not working as expected.


Here are the cleaned up scripts for your specific form. They work for me, but that does not mean that they will keep on working for you when you change your form (or change the data that is used in the form or the scripts).

Here is the validation script for PlanType:

var accountingData = {

  "Leadership": ["--SELECT--", "MIB"],

  "Mid-Management": ["--SELECT--", "Local Bonus Plan"],

  "Sales": ["--SELECT--", "#1 Sales Account Manager – Aftermarket/Direct", "#2 Sales Account Manager – OEM/Direct", "#3 Sales Account Manager – OEM/Distribution",

  "#4 Sales Account Manager – OEM/Overlay", "#5 Sales Manager – Any Products", "#6 Selling Sales Manager – Any Products", "#7 New Business Developer – Aftermarket/Direct",

  "#8 New Business Developer – OEM/Direct", "#9 Sales Manager, New Business – Any Product", "#10 Selling Sales Manager, New Business – Any Product",

  "#11 Account Manager – Aftermarket/Direct", "#12 Account Manager – OEM/Direct", "#13 Account Manager – Aftermarket/Distribution",

  "#14 Sales Manager, Existing Accounts – Any Product", "#15 Account Service – Aftermarket/Direct", "#16 Account Service – OEM/Direct",

  "#17 Manager, Account Service – Any Product/Direct", "#18 Growth Representative – Aftermarket/Distribution", "Legacy Plan", "TFS Sales VIC"],

  "Shared Earnings/TM/Recognition": ["--SELECT--", "Gainsharing Program", "Local Bonus Plan", "Materials Processing Incentive Bonus (MPIB)",

  "Productivity Improvement Bonus (PIB)", "Professional Incentive Bonus (PIB)", "Profit Sharing", "Recognition", "SEP (Shared Earnings Plan)",

  "SEP Plus", "SSBP (Strategic Sourcing Bonus Plan)", "Statutory Bonus Plan"],

    "Probation Pending" : [ "--SELECT--", ],

};

if (event.value == "--SELECT--") {

  this.getField("LocalBonus").clearItems();

} else {

  this.getField("LocalBonus").setItems(accountingData[event.value]);

}

And here is the validation script for the LocalBonus field:

var hideSubPlan = (event.value == "Professional Incentive Bonus (PIB)");

var f = this.getField("Sub Plan");

if (f != null) {

    if (hideSubPlan) {

        f.readonly = true;

        f.display = display.hidden;

        f.value = " ";

        f.required = false;

    }

    else {

        f.readonly = false;

        f.display = display.visible;

        f.required = true;

    }

}

I did not "black out" the field, I just set it to read-only, and I am hiding it. If that's not what you want, you can combine this script with the last part of the script yo've posted to make it do that.

Please try to understand what every line and every statement in these scripts do. You will need to have the Acrobat JavaScript API document available to e.g. understand what the field properties are that are being set in the second script. You can look this up here:

Acrobat DC SDK Documentation

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
Community Beginner ,
May 03, 2019 May 03, 2019

Copy link to clipboard

Copied

LATEST

Hi Bernd & Karl

Thank you for taking the time to help me with my script. I'll take on your advice going forward. The script works brilliantly. I actually prefer the new script to the original.

Thanks

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 ,
Apr 30, 2019 Apr 30, 2019

Copy link to clipboard

Copied

How are these two scripts used? What are they doing as separate scripts? Do you have  sample form that uses both scripts that you can share?

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
Community Beginner ,
Apr 30, 2019 Apr 30, 2019

Copy link to clipboard

Copied

HI Karl

I have shared the file and the below snippet shows the 3 circled fields affected by the script. The first script is working fine. Its the 2nd script that's giving me the headache. When the user selects 'Professional Incentive Bonus (PIB)' in the local bonus field I need the sub plan field to black out.

https://adobe.ly/2XP9WPg

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