How to write a script to calculate the current field value based on other field values?

shiz
Tera Contributor

shiz_0-1730355949509.png

Current table Actual;
Current field Total;
Required fields: decamal1, decamal2
Implementation logic: Total=decamal1+decamal2.
How should I write this script code?

1 ACCEPTED SOLUTION

Runjay Patel
Giga Sage

Hi @shiz ,

 

You can do like below. Hope decimal1 and decimal2 both are Decimal type field.

RunjayPatel_0-1730358313039.png

 

RunjayPatel_1-1730358372667.png

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

 

View solution in original post

In this video i have explained about Web service integration in ServiceNow like how it works, how we can configure it, what are the prerequisite and many more. I have covered below topics in this video. 1. understand Web Service. Like when and how we will use it. 2. Talked about Inbound and ...
2 REPLIES 2

Runjay Patel
Giga Sage

Hi @shiz ,

 

You can do like below. Hope decimal1 and decimal2 both are Decimal type field.

RunjayPatel_0-1730358313039.png

 

RunjayPatel_1-1730358372667.png

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

 

In this video i have explained about Web service integration in ServiceNow like how it works, how we can configure it, what are the prerequisite and many more. I have covered below topics in this video. 1. understand Web Service. Like when and how we will use it. 2. Talked about Inbound and ...

OlaN
Giga Sage
Giga Sage

Hi,

I would advice you NOT to use a calculated field.

These fields are calculated every time a list of records is loaded and when a form is loaded, so the calculations happen a lot.

It's not very efficient if the value only is set once in a while, when one of the two decimal-fields are changed.

 

Instead, I would write a business rule that sets the value, and add a condition to set the field only when decimal1 or 2 is changed.

The same script logic would be applied, as below.

current.setValue('your_total_field_name', parseFloat(current.getValue('decimal1')) + parseFloat(current.getValue('decimal2')));