How to write the script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2024 10:57 PM
Hi,
I just created one table "Rating", on that two fields like "Currency" reference to the international currency table and "Rate" is a decimal field. Already load the data of currency and rate for that particular currency.
In the record producer form we have same field "Currency" and " Rate".
Now I need a script include and client script when currency field is selected, it will look up the " Rating" table which currency we selected based on that " Rate" field populate.
Can someone kindly help here,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2024 01:31 AM
Hi @PRAGHATIESH S Try below code
1. Create a Client Callable Script Include with name "RatingUtils"
var RatingUtils = Class.create();
RatingUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getRateByCurrency: function(currency) {
var rate = '';
var gr = new GlideRecord('u_rating');
if (gr.get('u_currency', currency)) {
rate = gr.getValue('u_rate');
}
return rate;
},
type: 'RatingUtils'
});
2. create an onChange Client script on Currency field
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('RatingUtils');
ga.addParam('sys_id', newValue);
ga.getXMLAnswer(function(response) {
var rate = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_rate', rate);
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2024 06:17 AM - edited 08-11-2024 06:18 AM
Hello @PRAGHATIESH S ,
Script Include: FetchRate
var FetchRate = Class.create();
FetchRate.prototype = {
initialize: function() {
},
getRate: function(currency) {
var rate = 0;
var ratingGR = new GlideRecord('rating'); // Replace 'rating' with your table name
ratingGR.addQuery('currency', currency);
ratingGR.query();
if (ratingGR.next()) {
rate = ratingGR.rate; // Replace 'rate' with your field name
}
return rate;
},
type: 'FetchRate'
};
onChange Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var fetchRate = new GlideAjax('FetchRate'); // Name of the Script Include
fetchRate.addParam('sys_id', newValue); // Pass the new currency value (or sys_id if needed)
fetchRate.getXMLAnswer(function(response) {
var rate = response;
g_form.setValue('rate', rate); // Replace 'rate' with your field name
});
}
If you found my response helpful, please consider marking it as "Helpful" or "Accept Solution." Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2024 11:55 AM - edited 08-11-2024 05:55 PM
Seems both proposals above haven't been tested. Missing the script include's function name in the client script for one problem. If I assume a custom table with two fields 'u_currency' and 'u_rate'. An onChange client script defined on the 'u_currency' table would be:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Find the rate
var ga = new GlideAjax('RatingUtils');
ga.addParam('sysparm_name', 'getRateByCurrency');
ga.addParam('sysparm_sys_id', newValue);
ga.getXMLAnswer(getResponse);
// callback function for returning the result from the script include
function getResponse(response) {
var rate = response;
g_form.setValue('u_rate', rate);
}
}
And the client callable script include with name "RatingUtils" would have:
var RatingUtils = Class.create();
RatingUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getRateByCurrency: function() {
var currency = this.getParameter("sysparm_sys_id");;
var rate = 0.0;
var gr = new GlideRecord('u_rating');
if (gr.get('u_currency', currency)) {
rate = gr.getValue('u_rate');
}
return rate;
},
type: 'RatingUtils'
});
I haven't tested in a Record Producer though. Seems odd to query existing records in the custom table when creating new records in that same table with the same currency value, to get the rate value for the new record.