The Zurich release has arrived! Interested in new features and functionalities? Click here for more

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

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

You have to write on change client scripts on both field A and B. 2 on change client scripts, cant achieve by 1.

Both the scripts would be same, you read the value of A and B and multiply them and put the result on C.

Another caution you can use is, in the script on change of A , make sure that field b is not empty, if you multiply x with null you will get error

 

-Anurag

 

-Anurag

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Agree with Anurag.

you would require onChange on both the fields so that in case either changes the calculation happens

Adding to the point:

ensure you use parseInt() during the multiplication to be on safer side if those fields are of string type

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

sachin_namjoshi
Kilo Patron
Kilo Patron

You can configure on submit client script to calculate value of c so that you don't need to worry about on change.

If this is NOT catalog item, you should configure on after business rule on even better, use flow designer for no code approach.

 

Regards,

Sachin

Generally speaking it is a better user experience, and these scripts are not heavy so ill still vote to go on change.

-Anurag