Client Script to add variables

pkman
Tera Contributor

I need to add 6 values together and provide a total. I would like this to display in the catalog item. I have used input from the community, and for some strange reason still cannot get the field to calculate. Below is my script.  I tried the same using several methods on the calculation (number, parseInt, etc). The "total" field is unaffected.

 

 

function onChange(control, oldValue, newValue ) {
   if (newValue == '') {
      return;
   }
 var air = g_form.getValue('air');
 var hotels = g_form.getValue('hotels');
 var meals = g_form.getValue('meals');
 var transportation = g_form.getValue('transportation');
 var fuel = g_form.getValue('fuel');
 var misc = g_form.getValue('misc');
 var total = parseFloat(air) + parseFloat(hotels) + parseFloat(meals) + parseFloat(transportation) + parseFloat(fuel) + parseFloat(misc);
 
 
 g_form.setValue('total',total);
   
}

 

 

1 ACCEPTED SOLUTION

Hi @pkman ,
I have tried in my PDI its working you can user below script

 

function onChange(control, oldValue, newValue, isLoading) {
   if (newValue === '' || isNaN(newValue)) {
      g_form.setValue('total', '');
      return;
   }

   var air = parseFloat(g_form.getValue('air')) || 0;
   var gas = parseFloat(g_form.getValue('gas')) || 0;
   var water = parseFloat(g_form.getValue('water')) || 0;

   var total = air + gas + water;

   g_form.setValue('total', total);
   
}

 

AnandKumarP_2-1696442128696.png

 

 

AnandKumarP_1-1696441955969.png

Please mark it as helpful and solution proposed if it helps.

Thanks,

Anand

 

View solution in original post

6 REPLIES 6

Sandeep Rajput
Tera Patron
Tera Patron

@pkman I tested with the following script and it runs fine for me.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var fuel=g_form.getValue('fuel');
	var hotel=g_form.getValue('hotel');
	var total = parseFloat(fuel)+parseFloat(hotel);
	g_form.setValue('total',total);
}

 

Could you please share the snapshot of your Client script form here.

Screen Shot 2023-10-04 at 11.43.36 AM.png

@pkman Where are you testing this feature on Service Portal or backend? If you are testing on the Service Portal then play change the UI Type to All and the script will start working on portal too. 

 

Screen Shot 2023-10-04 at 11.43.36 AM.png

Hi @pkman ,
I have tried in my PDI its working you can user below script

 

function onChange(control, oldValue, newValue, isLoading) {
   if (newValue === '' || isNaN(newValue)) {
      g_form.setValue('total', '');
      return;
   }

   var air = parseFloat(g_form.getValue('air')) || 0;
   var gas = parseFloat(g_form.getValue('gas')) || 0;
   var water = parseFloat(g_form.getValue('water')) || 0;

   var total = air + gas + water;

   g_form.setValue('total', total);
   
}

 

AnandKumarP_2-1696442128696.png

 

 

AnandKumarP_1-1696441955969.png

Please mark it as helpful and solution proposed if it helps.

Thanks,

Anand