We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

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

Part 1. This video serves as the introductory episode of the ServiceNow Fundamental Course playlist. Here, I've outlined the course content that I'll be delving into throughout this series. For document please visit: https://servicenowwithrunjay.com/ Follow Facebook page for latest update on ...
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

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

 

 

Part 1. This video serves as the introductory episode of the ServiceNow Fundamental Course playlist. Here, I've outlined the course content that I'll be delving into throughout this series. For document please visit: https://servicenowwithrunjay.com/ Follow Facebook page for latest update on ...

OlaN
Tera 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')));