How to create a business rule to calculate multiple (11) currency fields

deneech
Giga Expert

Hello Everyone

I wasn't able to accomplish a business rule to calculate 11 currency fields so I create to subtotals for each column. I was able to get this to work.

Now I am trying to create a BR to calculate the two subtotals and my Condition or Code is not working.   Can someone please?

updated tco.JPG

help.JPG

thanks

Denee

____________________________________________________

I have a form that has 11 currency fields, that I need to get the the total when saved/Updated or submitted.

I am struggling with the condition. My current Business Rule

tco.JPG

Thanks for all your help. I will make sure i mark your advice as helpful or correct if you can help me solve this problem.

Denee

Message was edited by: Denee Harris

1 ACCEPTED SOLUTION

Your script looks okay to me - are these all configured to run "before"? You could have an ordering problem where "Calculate Total TCO" is running before those other fields are themselves calculated.



There's an easy way to trace this down if you add some debug statements to your business rules. You should use gs.debug("Debug message here") in all 3 business rules to print the field values and the calculation result. Adding the debug logs makes it very easy to see what the business rule has actually done when you are debugging.



Add some debug statements to all of these business rules and enable the debug logging and business rule debugging tools via System Diagnostics > Session Debug > Debug Log, and System Diagnostics > Session Debug > Debug Business Rule.



After you do this, change some values on your form, save it, and look at the output at the bottom of the page. The debugging logs will stand out with [DEBUG]. You need to read these logs and see how these rules are flowing, and verify the order they are running in.


View solution in original post

16 REPLIES 16

Runjay Patel
Giga Sage

Check out this video, it will clear all your doubts and help you to understand Business Rule queries in details.

Link: https://www.youtube.com/watch?v=hLottNnk21U&ab_channel=ServiceNowHelpdesk

It help you to understand below points.

  • Scenario based business rule uses
  • Best business use cases for all types of business rules
  • Current .update in before business rule
  • Difference between Before BR and Before query BR?
  • Where can we use query business rule?
  • All type of business rule with real world scenario
    • Before
    • After
    • Async
    • Display

 

Please mark reply as Helpful/Correct, if applicable. Thanks!!

yad_achyut
Giga Guru

Hi ,

Instead of using the Business rule you can also make use of servicenow OOB feature i.e. Calculated Value. which comes under the advanced view.

This works same as your business rule where you can access the fields using current.fieldname and you can perform the calculation as per your requirement.

for Example :
You have three fields Price, Tax and Total so you can write the following code in the Calculated Value section of the Field "Total " by using following code:
var price = current.Price;
var tax = current.Tax;
var total = parseFloat(price) + parseFloat(Tax);

return total;

This will return the values in the same currency which is been used for the field without any extra line of code.