Function is not getting the correct fields for calculation with dropdown populating

Community Beginner ,
Apr 08, 2022 Apr 08, 2022

Copy link to clipboard

Copied

I made a "DropDown1" with 2 choices based in differente attributes "ValueInt"and "ValueCon" to populate a "Text2".

My issue is that no matter my choice in the Dropdown, it will always get the "var con"

Any help to make the code get the correct var for each option?

 

var int = getField("ValueInt");
var con = getField("ValueCon");
var HPAtrib = { "Administração (Int)":{ atrib: event.value = Math.round(int.value * 1) },
"Medicina (Int)":{ atrib: event.value = Math.round(con.value * 1) }};
function SetFieldValues(cHpa)
{
this.getField("Text2").value = HPAtrib[cHpa].atrib;
}

TOPICS
PDF forms

Views

431

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 3 Correct answers

Adobe Community Professional , Apr 09, 2022 Apr 09, 2022
Using a data object is really just complicating things for you here. Use this instead: function SetFieldValues(cHpa) { var int = Number(this.getField("ValueInt").valueAsString); var con = Number(this.getField("ValueCon").valueAsString); if (cHpa=="Administração (Int)") this.getField("Text2").value = Math.round(int); else if (cHpa=="Medicina (Int)") this.getField("Text2").value = Math.round(con); else this.getField("Text2").value = ""; }

Likes

Translate

Translate
Adobe Community Professional , Apr 10, 2022 Apr 10, 2022
Here you go...

Likes

Translate

Translate
Adobe Community Professional , Apr 12, 2022 Apr 12, 2022
Change the doc-level script to this: function SetFieldValues(cHpa, targetFieldName) { var int = Number(this.getField("ValueInt").valueAsString); var con = Number(this.getField("ValueCon").valueAsString); if (cHpa=="Administração (Int)") this.getField(targetFieldName).value = Math.round(int); else if (cHpa=="Medicina (Int)") this.getField(targetFieldName).value = Math.round(con); else this.getField(targetFieldName).value = ""; } And then use just this code from the fields: SetField...

Likes

Translate

Translate
Adobe Community Professional ,
Apr 08, 2022 Apr 08, 2022

Copy link to clipboard

Copied

Remove all instances of "event.value = " from the definition of the HPAtrib variable.

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 08, 2022 Apr 08, 2022

Copy link to clipboard

Copied

Also, this is not going to work correctly if you change the value of the int or con fields after the file is opened, as that code is only executed once. If you move it into the function it might solve this issue, though.

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 09, 2022 Apr 09, 2022

Copy link to clipboard

Copied

If it won't update the fields when I input new values to the variables, it won't solve my problem.
Isn't there a way to the form updates the values?

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 09, 2022 Apr 09, 2022

Copy link to clipboard

Copied

Did you try what I described?

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 09, 2022 Apr 09, 2022

Copy link to clipboard

Copied

Yes, I removed all "event.value = " from the definition of the HPAtrib variable.

Bur I didn't really catch "If you move it into the function", what did you mean?

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 09, 2022 Apr 09, 2022

Copy link to clipboard

Copied

I am using this code in the dropdown Format > Custom Keystroke, is that correct?

 

//inserir em "Dropdown3"
if( event.willCommit )
{
if(event.value = "- Escolha uma habilidade profissional")
this.resetForm(["Text2"]);
else
SetFieldValues(event.value);
}

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 09, 2022 Apr 09, 2022

Copy link to clipboard

Copied

Using a data object is really just complicating things for you here.

Use this instead:

 

function SetFieldValues(cHpa) {
	var int = Number(this.getField("ValueInt").valueAsString);
	var con = Number(this.getField("ValueCon").valueAsString);
	if (cHpa=="Administração (Int)")
		this.getField("Text2").value = Math.round(int);
	else if (cHpa=="Medicina (Int)")
		this.getField("Text2").value = Math.round(con);
	else this.getField("Text2").value = "";
}

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 10, 2022 Apr 10, 2022

Copy link to clipboard

Copied

Thank you very much. I managed to correct my file.
But there's still a persisntent issue: "Text2" won't update when I change "ValueInt" and "ValueCon", I need to reset the field than select from the dropdown again to get updated values.
My dropdown has around 120 options, not updating isn't an option.
Is there a way to make "Text2" update when I change "ValueInt" and "ValueCon"? I am sending my test file for you.

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 10, 2022 Apr 10, 2022

Copy link to clipboard

Copied

Works just fine for me.

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 10, 2022 Apr 10, 2022

Copy link to clipboard

Copied

If i update ValueCon or ValueInt, the value in Text2 won't update.
That's the issue.

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 10, 2022 Apr 10, 2022

Copy link to clipboard

Copied

I need Text2 to update whenever I change ValueInt or ValueCon.

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 10, 2022 Apr 10, 2022

Copy link to clipboard

Copied

Move the code to the field's custom calculation script and remove this line from it (as well as the curly brackets that go with it:

if( event.willCommit )

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 10, 2022 Apr 10, 2022

Copy link to clipboard

Copied

I am a little confused now.
What code should I move to what field? I didn't understand that.
If it won't bother you, can you please fix it in the file I sent before?

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 10, 2022 Apr 10, 2022

Copy link to clipboard

Copied

Here you go...

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 10, 2022 Apr 10, 2022

Copy link to clipboard

Copied

Perfect!
It worked. Thank you very much for your patience and your time to save us!

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 10, 2022 Apr 10, 2022

Copy link to clipboard

Copied

Sorry to bother you again...
I can't use more than 2 var's in the Document SetFieldValues function.
If I try to put a third and forth var, it will say "missing variable name". I have 7 variables:
ValueFor
ValueDes
ValueInt
ValueCon

ValueSab

ValueCar

ValueVon

 

I tried to insert ValueFor in the script you made like this:

function SetFieldValues(cHpa) {
var int = Number(this.getField("ValueInt").valueAsString);
var sab = Number(this.getField("ValueSab").valueAsString);
var for = Number(this.getField("ValueFor").valueAsString);
if (cHpa=="Administração (Int)")
this.getField("HabPAtrib.0").value = Math.round(int);
else if (cHpa=="Armeiro (Int)")
this.getField("HabPAtrib.0").value = Math.round(for);
else if (cHpa=="Medicina (Int)")
this.getField("HabPAtrib.0").value = Math.round(sab);
else this.getField("HabPAtrib.0").value = "";
}

 

It will return the error like the image below

Rodrigo23877206a2zm_0-1649630214447.png

 

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 10, 2022 Apr 10, 2022

Copy link to clipboard

Copied

I am also trying to make it multirow...

But the multirow script isn't working...

 

function SetFieldValues(cHpa) {
oFld = event.target.name;
var nNumRow = oFld.substring(oFld.lastIndexOf('.')+1);
var nHabPAtrib = this.getField("HabPAtrib.0." + nNumRow).value;
if (cHpa=="Armeiro (Int)")
nHabPAtrib.value = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Administração (Int)")
nHabPAtrib.value = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Alvenaria (Int)")
nHabPAtrib.value = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Barbeiro (Int)")
nHabPAtrib.value = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Bordado (Int)")
nHabPAtrib.value = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Carpinteiro (Int)")
nHabPAtrib.value = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Cerâmica (Int)")
nHabPAtrib.value = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Construção naval (Int)")
nHabPAtrib.value = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Contabilidade (Int)")
nHabPAtrib.value = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Culinária (Int)")
nHabPAtrib.value = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Curtimento (Int)")
nHabPAtrib.value = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Destilaria (Int)")
nHabPAtrib.value = Math.round(Number(this.getField("ValueInt").valueAsString));
else nHabPAtrib.value = "";
}

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 10, 2022 Apr 10, 2022

Copy link to clipboard

Copied

I also tryed

function SetFieldValues(cHpa) {
oFld = event.target.name;
var nNumRow = oFld.substring(oFld.lastIndexOf('.')+1);
var nHabPAtrib = this.getField("HabPAtrib.0." + nNumRow).value;
if (cHpa=="Armeiro (Int)")
nHabPAtrib = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Administração (Int)")
nHabPAtrib = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Alvenaria (Int)")
nHabPAtrib = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Barbeiro (Int)")
nHabPAtrib = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Bordado (Int)")
nHabPAtrib = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Carpinteiro (Int)")
nHabPAtrib = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Cerâmica (Int)")
nHabPAtrib = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Construção naval (Int)")
nHabPAtrib = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Contabilidade (Int)")
nHabPAtrib = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Culinária (Int)")
nHabPAtrib = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Curtimento (Int)")
nHabPAtrib = Math.round(Number(this.getField("ValueInt").valueAsString));
else if (cHpa=="Destilaria (Int)")
nHabPAtrib = Math.round(Number(this.getField("ValueInt").valueAsString));
else nHabPAtrib = "";
}

 

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 11, 2022 Apr 11, 2022

Copy link to clipboard

Copied

You should spend some time learning the core JS syntax.

To address your issues:

- "for" is a reserved word in JS, so you can't define a variable with that name.

- To attach multiple lines to an if condition you must place them in curly brackets, like so:

 

if (condition) {

line 1

line 2

line 3

} else if (condition2) {

line 4

line 5

} else {

line 6

}

 

If you don't use curly brackets only the first following line is associated with the conditional statement.

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 11, 2022 Apr 11, 2022

Copy link to clipboard

Copied

quote

You should spend some time learning the core JS syntax.

To address your issues:

- "for" is a reserved word in JS, so you can't define a variable with that name.

- To attach multiple lines to an if condition you must place them in curly brackets, like so:

 

if (condition) {

line 1

line 2

line 3

} else if (condition2) {

line 4

line 5

} else {

line 6

}

 

If you don't use curly brackets only the first following line is associated with the conditional statement.


By @try67

 

I totally agree that I should learn JS...
We aren´t used to forms that we must program to work, but the licence for the software we use expired, so we are trying to work around to not cease people benefits... I work in an Educational Institute and those forms are intend to grant poor students a financial help to keep studying.
I really appreciate your help and your patience. I am indeed a beginner.

And I didn´t get that code to work for the rows...
I need to somehow insert this:

oFld = event.target.name;
var nNumRow = oFld.substring(oFld.lastIndexOf('.')+1);
var nHabPAtrib = this.getField("HabPAtrib.0." + nNumRow).value;

 

So when I duplicate the Atrib fields (that you helped me to figure out how to update the data), when I duplicate, they will tranpose the code to the below row.

I am going to work more in this.

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 12, 2022 Apr 12, 2022

Copy link to clipboard

Copied

I couldn't figure out how to make the rows get what I need when I multiply the rows.
I made an example in the file attached, of how the information must work. But I couldn't get the "Text2" fields to be filled after I create multiple copies of the fields as below rows.
Adobe automatic insert the row number in the end of the Text2 field name, but the JS code isn't updated aswell.
For "Text4" I used a simple solution and it worked, but it won't work for "Text2", because Text2 is filled by the dropdown calculation and the document function...
Any Idea? My example document is attached.

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 12, 2022 Apr 12, 2022

Copy link to clipboard

Copied

- The name of the second field is "Text2.1", not "Text2.". However, this entire section of the code is not needed. You should do it in the function.

- You will need to pass the name of the target field as a parameter to the SetFieldValues function.

- Under the first drop-down you're calling a function called "SetFieldValuesHabPAtrib", but no such function exists.

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 12, 2022 Apr 12, 2022

Copy link to clipboard

Copied

quote

- You will need to pass the name of the target field as a parameter to the SetFieldValues function.


By @try67

 

How can I do that?
I did something similar for Text4, but i can't make it work in the function.
I corrected the function name, but it didn't fixed the issue...

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 12, 2022 Apr 12, 2022

Copy link to clipboard

Copied

Change the doc-level script to this:

 

function SetFieldValues(cHpa, targetFieldName) {
	var int = Number(this.getField("ValueInt").valueAsString);
	var con = Number(this.getField("ValueCon").valueAsString);
	if (cHpa=="Administração (Int)")
		this.getField(targetFieldName).value = Math.round(int);
	else if (cHpa=="Medicina (Int)")
		this.getField(targetFieldName).value = Math.round(con);
	else this.getField(targetFieldName).value = "";
}

 

And then use just this code from the fields:

 

SetFieldValues(event.value, "Text2.0"); // calculate script for "Dropdown5.0"

 

SetFieldValues(event.value, "Text2.1"); // calculate script for "Dropdown5.1"

 

SetFieldValues(event.value, "Text2.2"); // calculate script for "Dropdown5.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