Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

required help on multiplying two fields

parnika1
Kilo Contributor

Hi All,

 

Kindly provide solution for this.

 

I have two fields[Input Value is Numbers (a,b)] .On the third field[c] , I want the result of(a*b).

The problem I am facing is both [a,b] can be editable. If I write onchange Client script  on A and if B value is edited. then c is populated with (a*b), where b is the old value but not the new value. Kindly help me in achieving this solution

1 ACCEPTED SOLUTION

Hey Mate,

 

While you can write onChange on both fields, but again if B value is edited first then it will still take old Value of A. So till value of A is changed you still see the result of new Value of B X old value of A in field C.  --> Why would this happen?? It will take the new value if you use that each time.

 

Had these scripts been querying server or something heavy I might have suggested doing this on submit/save only. But these are light weight calculations and important form a user perspective, so you want to see the result of change without having to save/submit the records. 

 

Here is the scripts, client side, for A and B and working perfectly

find_real_file.png

find_real_file.png

 

find_real_file.png

 

-Anurag

-Anurag

View solution in original post

13 REPLIES 13

asifnoor
Kilo Patron

Hi Parnika,

Technically I don't think both values will be edited at the same time. 

So when value A is edited how will the system know that B value is also going to be editable? Similarly if B is edited, how will system know that A is also going to be editable.

While you can write onChange on both fields, but again if B value is edited first then it will still take old Value of A. So till value of A is changed you still see the result of new Value of B X old value of A in field C. 

The best solution is to write this in before insert/update BR

Filter conditions:

A changes OR

B changes

Script:

current.c=parseInt(current.a)*parseInt(current.b);

Mark the comment as correct/helpful if it helps.

Hey Mate,

 

While you can write onChange on both fields, but again if B value is edited first then it will still take old Value of A. So till value of A is changed you still see the result of new Value of B X old value of A in field C.  --> Why would this happen?? It will take the new value if you use that each time.

 

Had these scripts been querying server or something heavy I might have suggested doing this on submit/save only. But these are light weight calculations and important form a user perspective, so you want to see the result of change without having to save/submit the records. 

 

Here is the scripts, client side, for A and B and working perfectly

find_real_file.png

find_real_file.png

 

find_real_file.png

 

-Anurag

-Anurag

Hi,

If the value of B is edited first and then A, then in onChange of B, you get old value of A only.  Thats what i meant in my above comment.

I think that is expected, like any value can change , no specific order as such.

-Anurag

I think that's what was the problem reported. Hence suggested BR.