- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-30-2024 11:28 PM
Current table Actual;
Current field Total;
Required fields: decamal1, decamal2
Implementation logic: Total=decamal1+decamal2.
How should I write this script code?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2024 12:06 AM
Hi @shiz ,
You can do like below. Hope decimal1 and decimal2 both are Decimal type field.
-------------------------------------------------------------------------
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
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2024 12:06 AM
Hi @shiz ,
You can do like below. Hope decimal1 and decimal2 both are Decimal type field.
-------------------------------------------------------------------------
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
-------------------------------------------------------------------------

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2024 12:38 AM
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')));