I want to calculate total of Catalog item variables using script?

Virendra K
Kilo Sage

Hi All,

I have variables created with Single line text Type. I want to calculate their total value in another variable. I have written client script on Onchange event of each variable. If I am opening a saved item to edit or update the values,  Total values  (Total of AB and Total of AB and XY)  are not getting updated/calculated as expected/properly

Here is the actual code. Please correct me if I am doing any mistake here in script.

=========================================================================================

function onChange(control, oldValue, newValue, isLoading) {
    
    // Initialize dollar pattern
    var dollar_value = /^\$ ([1-9]?[0-9]*?)(\.[0-9]{2})?$/;
    if (isLoading /* || newValue == '' */ || dollar_value.test(newValue)) {
      return;
    }
   
    if (newValue == '') {
        newValue = 0;
    }
    
    // First get total billable value
    var total_sum_value = 0.0;
    var total_sum = g_form.getValue('Total_of_AB_and_XY');
    if (total_sum != '') {
        total_sum = total_sum.replace(/[^0-9.]/g ,'');
        total_sum_value = parseFloat(total_sum);
    }
  
    
    // First get the total billable value
    var capital_sum_value = 0.0;
    var capital_sum = g_form.getValue('Total_of_AB');
    if (capital_sum != '') {
        capital_sum = capital_sum.replace(/[^0-9.]/g ,'');
        capital_sum_value = parseFloat(capital_sum);
    }
    var old_capital_sum_value = capital_sum_value;
       
    var o_value = 0.0;
    if (oldValue != '') {
        var o = String(oldValue).replace(/[^0-9.]/g ,'');
        o_value = parseFloat(o);
        if (o_value != 0  && String(o_value) != 'NaN') {
            if (String(o_value).indexOf('.') > -1) {
                o_value = String(o_value).split('.')[0] + '.' +
                    (String(o_value) + '00').split('.')[1].substring(0,2);
            }
        } else {
            o_value = 0;
        }
    }
    
    var n = String(newValue).replace(/[^0-9.]/g ,'');
    var n_value = parseFloat(n);
    //alert('** n_value [' + n_value + ']');
    
    if (n_value != 0  && String(n_value) != 'NaN') {
        if (String(n_value).indexOf('.') > -1) {
            n_value = String(n_value).split('.')[0] + '.' +
                (String(n_value) + '00').split('.')[1].substring(0,2);
        }
    } else {
        n_value = 0;
    }

    
    if (o_value > 0) {
        capital_sum_value = capital_sum_value + n_value - o_value;
    } else {
        capital_sum_value = capital_sum_value + n_value;
    }
 
    
    g_form.setValue('Total_of_AB_and_XY','$ ' + n_value);
    g_form.setValue('Total_of_AB', '$ ' + capital_sum_value);
    
    if (old_capital_sum_value > 0) {
        total_sum_value = total_sum_value + capital_sum_value - old_capital_sum_value;
    } else {
        total_sum_value = total_sum_value + capital_sum_value;
    }
    g_form.setValue('requested_fund','$ ' + total_sum_value);
   
}

=========================================================================================

Below is just an example to describe the above scenario,

find_real_file.png

Let me if code in a different way apart from this ?

Thanks,

Virendra

 

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Is your question solved with this? Or do we need to follow-up on this?

Please mark this answer as correct if it solves your problem. This will help others who are looking for a similar solution. Also marking this answer as correct takes the post of the unsolved list.
Thanks.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

3 REPLIES 3

Mark Roethof
Tera Patron
Tera Patron

Hi there,

I just did a small test. See the results below. I did only do the var_a, var_b, total_ab, though you'll get the logic of it. Maybe it could be done a bit more efficient with variable names, variable reusing, etc..
In my test I tried to simplify the situation, I saw your script was massive! If calculating var_a + var_b, no need to calculate other stuff at that point. Just do that in another short catalog client script where you know the input is clean, avoided the NaN, etc..

Though just a quick working test!

Obviously, you could have slightly different requirements. What if the user forgets to enter the dollar sign, is that bad? Could you onChange at the dollar sign instead of giving an error? What to do if someone empties the value, by default there's a check newValue == '' after which the Catalog Client Script returns, maybe change this into setting the default value of $0.00 in that case.
Etc..  

find_real_file.png

find_real_file.png

find_real_file.png

function onChange(control, oldValue, newValue, isLoading) {
	
	if(isLoading || newValue == '') {
		return;
	}
	
	// Define Regular Expression
	var regex = /^\$[0-9]*\.[0-9]{2}$/;
	
	if(!regex.test(newValue)) {
		g_form.addErrorMessage('Currency validation');
		g_form.setValue('var_a', '$0.00');
		return;
	}
	
	var var_a = g_form.getValue('var_a');
	var_a = var_a.slice(1);
	var_a = parseFloat(var_a);

	var var_b = g_form.getValue('var_b');
	var_b = var_b.slice(1);
	var_b = parseFloat(var_b);

	var total_ab = parseFloat(var_a + var_b);
	total_ab = total_ab.toFixed(2);
	
	g_form.setValue('total_ab', '$' + total_ab);
	
}

find_real_file.png

function onChange(control, oldValue, newValue, isLoading) {
	
	if(isLoading || newValue == '') {
		return;
	}
	
	// Define Regular Expression
	var regex = /^\$[0-9]*\.[0-9]{2}$/;
	
	if(!regex.test(newValue)) {
		g_form.addErrorMessage('Currency validation');
		g_form.setValue('var_b', '$0.00');
		return;
	}
	
	var var_a = g_form.getValue('var_a');
	var_a = var_a.slice(1);
	var_a = parseFloat(var_a);

	var var_b = g_form.getValue('var_b');
	var_b = var_b.slice(1);
	var_b = parseFloat(var_b);

	var total_ab = parseFloat(var_a + var_b);
	total_ab = total_ab.toFixed(2);
	
	g_form.setValue('total_ab', '$' + total_ab);
	
}

find_real_file.png

function onChange(control, oldValue, newValue, isLoading) {
	
	if(isLoading || newValue == '') {
		return;
	}
	
	var total_ab = g_form.getValue('total_ab');
	total_ab = total_ab.slice(1);
	total_ab = parseFloat(total_ab);

	var total_cd = g_form.getValue('total_cd');
	total_cd = total_cd.slice(1);
	total_cd = parseFloat(total_cd);

	var total_abcd = parseFloat(total_ab + total_cd);
	total_abcd = total_abcd.toFixed(2);
	
	g_form.setValue('total_abcd', '$' + total_abcd);
	
}

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hi there,

Is your question solved with this? Or do we need to follow-up on this?

Please mark this answer as correct if it solves your problem. This will help others who are looking for a similar solution. Also marking this answer as correct takes the post of the unsolved list.
Thanks.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Is your question solved with this? Or do we need to follow-up on this?

Please mark this answer as correct if it solves your problem. This will help others who are looking for a similar solution. Also marking this answer as correct takes the post of the unsolved list.
Thanks.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn