Copy link to clipboard
Copied
Hello, I need help trying to figure out how to create a code in a pdf from with javascript where I want to be able to, using 2 different text variables, get a narrow result and then multiply it. For example: if "name" = "john" and "sex" = "male", that set of variables gives me a value ex: "6", after that I would get another value presented in a editable field and multiply it by the result in my search.
Please check the code I came up with:
var a = this.getField("peso01").value;
var b = this.getField("uni01").value;
var c = this.getField("quant01").value;
var d = 6;
var e = 240;
var result;
switch (a) {
case '1':
a = "2KG";
break;
case '2':
b = "PAL";
break;
case '3':
b = "PACK";
if (a === "2KG" && b === "PACK") {
result = c * d;
} else if (a === "2KG" && b === "PAL") {
result = c * e;
}
break;
}
I can't seem to make it work, please help me get it right.
Copy link to clipboard
Copied
You can use this code as the custom calculation script of your field to achieve what you described above (I used 5 as the multiplier. You can of course change it to something else...):
if (this.getField("peso01").valueAsString=="2KG" && this.getField("uni01").valueAsString=="PAL")
event.value = Number(this.getField("quant01").valueAsString) * 5;
Copy link to clipboard
Copied
Your code is very strange. Why are you converting values from one thing to another? Why are you using a switch statement?
Try to explain in detail what you want to achieve and we could help you write proper code to do it.
Copy link to clipboard
Copied
Hi, i do not have much experience with javascript, so I built what I thought was right while looking at stuff online. Thank you for you response.
What I'm trying to do is (using the terms I used in my code), when the value of peso01 equals 2KG ( I have more than just this weight) and the value of uni01 equals PAL( here I have 2 types of packaging), i want to get the value in quant01 (wich is inserted by the client) and then multiply it by a number. After that I can manage to do the rest I think.
What I'm trying to do is build a pdf form where the client choses a weight and the type of the packaging and I multiply the quantity he/she wants by the number of product the packaging can take.
Copy link to clipboard
Copied
You can use this code as the custom calculation script of your field to achieve what you described above (I used 5 as the multiplier. You can of course change it to something else...):
if (this.getField("peso01").valueAsString=="2KG" && this.getField("uni01").valueAsString=="PAL")
event.value = Number(this.getField("quant01").valueAsString) * 5;
Copy link to clipboard
Copied
Thank you for the response.
It isn't working for me, I applied the code you gave to me but the field doesn't show the value like it's suposed to.
Here's an capture of one of the lines from de form for further comprehention.
As you can see, the fied "peso01"is set as 2KG, "uni01" as PAL and I gave it the quantity of 20 and it doesn't calculate.
Copy link to clipboard
Copied
Press Ctrl+J and check if there are error messages in the console window that opens.
Or share the file with us, so we can see for ourselves what's going on.
Copy link to clipboard
Copied
It worked afterall, i inserted the code in the wrong place.
Thank you!!

