Calculate and Populate 3rd value based on the other two field

Ajith A Pillai
Tera Contributor

Hi All,

I have a requirement to populate third value based on the calculation of other two fields.

The Requirement is I have added 2 fields in a catalog item one is a string field and another one is duration field which consists of values as 3 months, 6 months and other. If user choose other option an alternative field will be populated as string type which user can enter the duration whatever it is. I have achieved this through UI Policy.

I have created a couple other 3 fields called license(3 separate fields), one field for quantity. 

Cost for one license is $14/month and second one for $19/month and third one for $33/month. If user choose any one of the license or choosen three licenses, based on these 3 values and input of duration I wanted to populate total cost field based on the input. 

For e.g: If I required 3 licenses, license 1 is 11 quantity, license 2 is 10 quantity and license 3 is 15 quantity for 6 months. So based on this input it has to calculate 11*$14 + 10*$19 + 15*$33 * 6months has to populate the total cost in the field. 

 

If anyone has any idea on this, please help me to achieve this with full steps.

 

Thanks in Advance!

 

Regards

Ajith A Pillai

 

4 REPLIES 4

Jon23
Mega Sage

Hi @Ajith A Pillai ,

Look at using onChange Catalog scripts (and GlideAjax call) to perform the calculations and return the total.

A screen shot (or 2) of your form may help the community to provide solutions with full steps.

Hi @Jon23 ,

Attaching the screenshot from the form page. Based on these combination I wanted to populate the total cost.

 

Regards

Ajith A Pillai

Hi @Ajith A Pillai ,

My suggestion is to have onChange client script for each variable that will re-calcualate the total cost.  This will allow the total cost to update before for the user to see before submission.

For example:

Variable onChange script (this would be the same script for each variable):

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

    var duration = g_form.getValue('duration');
    if (duration === '10') { //other selected
        duration = g_form.getValue('duration_other');
    }

    var ga = new GlideAjax('global.totalCostAjax');
    ga.addParam('sysparm_name', 'calculateIt');
    ga.addParam('sysparm_softwareQty', g_form.getValue('softwareQty'));
    ga.addParam('sysparm_antivirusQty', g_form.getValue('antivirusQty'));
    ga.addParam('sysparm_productQty', g_form.getValue('productQty'));
    ga.addParam('sysparm_duration', duration);
    ga.getXMLAnswer(_handleResponse);

    function _handleResponse(response) {
        g_form.setValue('total_cost', response);
    }
}

Script Include:

var totalCostAjax = Class.create();
totalCostAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    calculateIt: function() {
        try {
            var softwareMonthlyCost = 19;
            var antivirusMonthlyCost = 14;
            var productMonthlyCost = 33;

            var softwareQty = this.getParameter('sysparm_softwareQty');
            var antivirusQty = this.getParameter('sysparm_antivirusQty');
            var productQty = this.getParameter('sysparm_productQty');
            var duration = this.getParameter('sysparm_duration');

            var softwareCost = (softwareQty * softwareMonthlyCost) * duration;
            var antivirusCost = (antivirusQty * antivirusMonthlyCost) * duration;
            var productCost = (productQty * productMonthlyCost) * duration;
            var totalCost = softwareCost + antivirusCost + productCost;

            return totalCost;
        } catch (e) {
            gs.error("SI global.totalCostAjax: " + e);
        }
    },
    type: 'totalCostAjax'
});

 

If the user does not need to see the total cost before submission you could simply total everything up onSubmit per @Kavita_Bhojane suggestion.

 

Kavita_Bhojane
Tera Guru

Hi @Ajith A Pillai ,

 

You need to create onSubmit Client script as below:

function onSubmit() {
//Type appropriate comment here, and begin script below
var other_duration = g_form.getDisplayValue('other_duration');
var set_duration = '';
var duration = g_form.getValue('duration');
if(duration == 'three_months'){
set_duration = 3;
}
else if(duration == 'six_months'){
set_duration = 6;
}
else//logic for other duration. Change as per your requirement
set_duration = Math.round(other_duration/30);
}

var quantity_1 = g_form.getDisplayValue('quantity_1');
var quantity_2 = g_form.getDisplayValue('quantity_2');
var quantity_3 = g_form.getDisplayValue('quantity_3');

var cost = ((quantity_1 * 19 + quantity_2 * 14 + quantity_3 * 33)* set_duration);
 
g_form.setValue('total_cost', cost);
 
}
 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

Thanks,
Kavita Bhojane