Skip to main content
Known Participant
September 1, 2020
Answered

adding

  • September 1, 2020
  • 4 replies
  • 1015 views

Is there any mistake in this script?

I wanted to add few variables in a pdf form, but it is not working: (I am a learner)

 

(this.getField("A").value)*10+ (this.getField("B").value)/3.5+

(this.getField("C").value)*2+

(this.getField("D").value)/2+

(this.getField("E").value)/4+

(this.getField("F").value)/0.227+

(this.getField("G").value)/2+ ((this.getField("H").value)||(this.getField("I").value))+

((this.getField("Text1").value)||(this.getField("Text2").value) ||(this.getField("Text3").value));

 

Thanks,

This topic has been closed for replies.
Correct answer try67

Using the OR operator in such a calculation is really asking for problems. If they are "interchangable" and only one of them can have a value at any specific point then just add them up. The result of their sum should be the value of the one field that has a value from the group, and it will be much easier to follow and debug.

4 replies

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
September 1, 2020

Using the OR operator in such a calculation is really asking for problems. If they are "interchangable" and only one of them can have a value at any specific point then just add them up. The result of their sum should be the value of the one field that has a value from the group, and it will be much easier to follow and debug.

Known Participant
September 4, 2020

You are right, I just added them.

Thanks

Nesa Nurani
Community Expert
Community Expert
September 1, 2020

Hi, you can add this before your code: event.value  =

Also you need to add "Number" to your fields like this: Number(this.getField("A").value or with your current code fields J,K,L won't calculate unless there is value in H or I.

Or if you don't know how to do it, just use this code:

var a = Number(this.getField("A").value);
var b = Number(this.getField("B").value);
var c = Number(this.getField("C").value);
var d = Number(this.getField("D").value);
var e = Number(this.getField("E").value);
var f = Number(this.getField("F").value);
var g = Number(this.getField("G").value);
var h = Number(this.getField("H").value);
var i = Number(this.getField("I").value);
var j = Number(this.getField("J").value);
var k = Number(this.getField("K").value);
var l = Number(this.getField("L").value);
event.value = (a*10)+(b/3.5)+(c*2)+(d/2)+(e/4)+(f/.227)+(g/2)+(h||i)+(j||k||l);

Bernd Alheit
Community Expert
Community Expert
September 1, 2020

You can try the script in the Javascript Debugger.

Inspiring
September 1, 2020

What are you trying to achive?

Known Participant
September 1, 2020

just a simple addition with multiplication and division and adding some fields if they are available only (I used OR for that because they are interchangeable).

Inspiring
September 1, 2020

What is the name of the field where you use this script?

Are all fields values number or are there text also?

 

EDIT: Forgot to ask, what is deciding factor between "I" or "H" and between "Text1" or "Text2" or "Text3"?