I have question pertaining to calculating tax formula in ITSM/CMDB

Vishnu533
Tera Contributor

 

Hi Everyone,


I have one requirement:

I have 3 fields: 1) Cost , 2) Tax and 3) Total Cost


1. Whenever I enter a number in the cost field, it should automatically update in the Tax field after multiplying by 0.50.

The formula is (Cost x0.50) = Tax.

 

2. The Total Cost field should be updated with the addition of cost and tax values.
formula would be (Cost + Tax) = Total Cost

 

It should be noted that the formula must function while altering the cost value at the form level.


Kindly suggest or help me what are the ways to achieve this requirement and please share related scripts


Regards,

Vishnu

14 REPLIES 14

Eshwar Reddy
Kilo Sage

Hi @Vishnu533 

Create a On Change Client script on Cost Field

 


var cost = parseFloat(newValue);

var tax = cost * 0.50;

var totalCost = cost + tax;

g_form.setValue('tax', tax.toFixed(2)); 
g_form.setValue('total_cost', totalCost.toFixed(2)); 

Please mark this response as Correct and Helpful if it helps you can mark more that one reply as accepted solution


Thanks
Esh

Pradeep Thipani
Mega Sage

Hi @Vishnu533 ,

 

Please find the below article that might help you!

 

https://developer.servicenow.com/blog.do?p=/post/formula-builder/

 

and if not try to write an onChange Client script to populate values.

Example script:

(function executeRule(current, previous) {
    var cost = parseFloat(g_form.getValue('cost')) || 0;
    var tax = cost * 0.5;
    g_form.setValue('tax', tax);
    g_form.setValue('total_cost', cost + tax);
})(current, previous);

 

Thanks,

Pradeep

"If this response was useful, please select 'Accept as Solution' and mark it as 'Helpful.' This helps me provide better answers and assists the community ".

Regards,
Pradeep

Hello @Pradeep Thipani 

 

Current and previous object will not work for client script. This works on server side example business rules but in that case g_form will not work.

 

Thank You

Juhi Poddar

Juhi Poddar
Kilo Patron

Hello @Vishnu533 

 

Create a onChange client script setting the variable to cost.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	//set the tax and total cost value
	g_form.setValue('tax', newValue*0.5);
	g_form.setValue('total_cost', newValue*1.5);
}

 

"If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!"

 

Thank You

Juhi Poddar