Calculate and Populate 3rd value based on the other two field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2024 08:35 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2024 10:44 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2024 09:29 PM - edited 04-23-2024 09:32 PM
Hi @Jon23 ,
Attaching the screenshot from the form page. Based on these combination I wanted to populate the total cost.
Regards
Ajith A Pillai
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2024 07:09 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2024 01:53 AM
Hi @Ajith A Pillai ,
You need to create onSubmit Client script as below:
Please mark my answer helpful and accept as a solution if it helped 👍✔️
Kavita Bhojane