How to write the script include

PRAGHATIESH S
Tera Expert

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, 

3 REPLIES 3

Sid_Takali
Kilo Patron
Kilo Patron

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); 
    });
}

 

Soni Tushar
Tera Guru

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!

Bert_c1
Kilo Patron

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.