add fields value and auto populate total on another field servicenow

Sydney Esposo
Tera Expert

Hi developers,

 

can someone help how to auto populate the total field value after user input the amounts needed , I made a code below while its not working. not sure what to input on Variable name. 

SydneyEsposo_0-1686799774501.png

 

 

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   //Type appropriate comment here, and begin script below
     var reg_cost = g_form.getValue(
        "registration_cost"
      );
      var est_cost = g_form.getValue(
        "estimated_costs"
      );
      var accom_cost = g_form.getValue(
        "estimated_accom"
      );
      var meal_cost = g_form.getValue("meal_costs");
      var other_cost = g_form.getVal("other_costs");
      var total =
        parseFloat(reg_cost) +
        parseFloat(est_cost) +
        parseFloat(accom_cost) +
        parseFloat(meal_cost) +
        parseFloat(other_cost);
      g_form.setValue("total_cost", total);
	alert("total");
}

 

 

 

4 REPLIES 4

Tony Chatfield1
Kilo Patron

Hi, the variable name would be the variable\field that is being chaned\updated in the UI, IE the field triggering the script to run. The catalog item is the catalog item\record producer you are developing.
If you are wanting to dynamically update the total cost whenever 1 of the component fields is changed you would need an onChange client script for each field. 
Also, you do not have to 'getValue for the field being changed, this value is available to you in your client script as the newValue parameter.

Ankur Bawiskar
Tera Patron
Tera Patron

@Sydney Esposo 

on which variable you are running the onChange? did you actually change that variable and verify the script?

Also are all the other variables populated or not?

script looks fine

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	//Type appropriate comment here, and begin script below
	var reg_cost = g_form.getValue("registration_cost");
	var est_cost = g_form.getValue("estimated_costs");
	var accom_cost = g_form.getValue("estimated_accom");
	var meal_cost = g_form.getValue("meal_costs");
	var other_cost = g_form.getVal("other_costs");
	var total = parseFloat(reg_cost) + parseFloat(est_cost) + parseFloat(accom_cost) + parseFloat(meal_cost) + parseFloat(other_cost);
	g_form.setValue("total_cost", total);
	alert("total");
}
Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Sai Kumar B
Mega Sage
Mega Sage

@Sydney Esposo 

I can see there are multiple different cost variables, If you want use onChange client script you'll have to write it on each cost variable for a better user experience or You can write an onSubmit client script to calculate the total cost upon submission of the request.
In the onChange client script in the variable name field, you have to pass the cost variable name
For example, When you create onChange on the Registration cost variable then the variable name in the client script will be registration_cost same goes for the rest of the variables.
onChange sample code on one cost variable that is registration_cost

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   //Type appropriate comment here, and begin script below
     var reg_cost = newValue //Here variable name is registration_cost so, the newValue is the latest value you input into the variable. 
      var est_cost = g_form.getValue(
        "estimated_costs"
      );
      var accom_cost = g_form.getValue(
        "estimated_accom"
      );
      var meal_cost = g_form.getValue("meal_costs");
      var other_cost = g_form.getVal("other_costs");
      var total =
        parseFloat(reg_cost) +
        parseFloat(est_cost) +
        parseFloat(accom_cost) +
        parseFloat(meal_cost) +
        parseFloat(other_cost);
      g_form.setValue("total_cost", total);
	alert("total");
}


If I could help you with my response you can mark it as helpful and correct as it benefits future viewers
Thanks,
Sai Kumar B

 

 

kalaiselvi Ravi
Tera Contributor
function onLoad() {
   
var rc=g_form.getValue('u_registration_cost');
var ec=g_form.getValue('u_estimation_cost');
var ac=g_form.getValue('u_accomodation_cost');
var mc=g_form.getValue('u_meal_cost');
var oc=g_form.getValue('u_other_cost');
var total=parseFloat(rc)+parseFloat(ec) +parseFloat(ac)+parseFloat(mc)+parseFloat(oc);
 
g_form.setValue('u_total_cost',total);
alert(total);
The Above code gets executed.Can u Check  with your instance.