- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2022 03:28 AM
Hi all, could anyone please help me with one requirement? i have two fields total_spend_inr and total_spend_usd. i want whenever i enter INR Amount Usd amount should autopopulate.
could anyone please help with the script.
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2022 04:35 AM
I have implemented this on catalog item variables (total_spend_inr, total_spend_usd) of type Single Line Text.
Create a client callable script include. See the below image for reference.
var INRToUSDConversion = Class.create();
INRToUSDConversion.prototype = Object.extendsObject(AbstractAjaxProcessor, {
toUSD: function() {
var totalSpendINR = parseFloat(this.getParameter('sysparm_total_spend_inr'));
var inrToEuro = new GlideRecord('fx_rate');
inrToEuro.orderByDesc('sys_created_on');
inrToEuro.addQuery('currency', 'INR');
inrToEuro.query();
if (inrToEuro.next()) {
var inrToEuroConverted = parseFloat(totalSpendINR) / parseFloat(inrToEuro.getValue('rate'));
var inrEuroConvertedToUsd = new GlideRecord('fx_rate');
inrEuroConvertedToUsd.orderByDesc('sys_created_on');
inrEuroConvertedToUsd.addQuery('currency', 'USD');
inrEuroConvertedToUsd.query();
if (inrEuroConvertedToUsd.next()) {
var inrToUSD = parseFloat(inrEuroConvertedToUsd.getValue('rate')) * inrToEuroConverted;
return inrToUSD.toFixed(2);
}
return '';
}
},
type: 'INRToUSDConversion'
});
Create an onchange client script on total_spend_inr field. See the below image for reference.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gaConversion = new GlideAjax('global.INRToUSDConversion');
gaConversion.addParam('sysparm_name', 'toUSD');
gaConversion.addParam('sysparm_total_spend_inr', newValue);
gaConversion.getXML(callBackFunction);
}
function callBackFunction(response){
var answer = response.responseXML.documentElement.getAttribute('answer');
g_form.setValue('total_spend_usd', answer);
}
Refer to below KB Article for conversion formula.
Currency conversion does not match the expected amount
Docs Link.
Community Thread.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2022 04:35 AM
I have implemented this on catalog item variables (total_spend_inr, total_spend_usd) of type Single Line Text.
Create a client callable script include. See the below image for reference.
var INRToUSDConversion = Class.create();
INRToUSDConversion.prototype = Object.extendsObject(AbstractAjaxProcessor, {
toUSD: function() {
var totalSpendINR = parseFloat(this.getParameter('sysparm_total_spend_inr'));
var inrToEuro = new GlideRecord('fx_rate');
inrToEuro.orderByDesc('sys_created_on');
inrToEuro.addQuery('currency', 'INR');
inrToEuro.query();
if (inrToEuro.next()) {
var inrToEuroConverted = parseFloat(totalSpendINR) / parseFloat(inrToEuro.getValue('rate'));
var inrEuroConvertedToUsd = new GlideRecord('fx_rate');
inrEuroConvertedToUsd.orderByDesc('sys_created_on');
inrEuroConvertedToUsd.addQuery('currency', 'USD');
inrEuroConvertedToUsd.query();
if (inrEuroConvertedToUsd.next()) {
var inrToUSD = parseFloat(inrEuroConvertedToUsd.getValue('rate')) * inrToEuroConverted;
return inrToUSD.toFixed(2);
}
return '';
}
},
type: 'INRToUSDConversion'
});
Create an onchange client script on total_spend_inr field. See the below image for reference.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gaConversion = new GlideAjax('global.INRToUSDConversion');
gaConversion.addParam('sysparm_name', 'toUSD');
gaConversion.addParam('sysparm_total_spend_inr', newValue);
gaConversion.getXML(callBackFunction);
}
function callBackFunction(response){
var answer = response.responseXML.documentElement.getAttribute('answer');
g_form.setValue('total_spend_usd', answer);
}
Refer to below KB Article for conversion formula.
Currency conversion does not match the expected amount
Docs Link.
Community Thread.