multiple glideajax in client script

si21
Tera Guru

Hi experts,

 

We have a requirement to show choices based on logged in user country and parent company on a record producer.

For question 'Transaction' ,

If user is from USA and company.parent is  'ABC' then show options t1, t2 and t3

If user is from USA and company.parent is not 'ABC' then 

show t4 and t5.

 

 

We already have script include which has functions for getting location and parent company.

var getLocation = Class.create();
getLocation.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getUserLoc: function() {
    var userRecord = new GlideRecord("sys_user");
	var exm= this.getParameter('sysparm_userID');
    userRecord.get(this.getParameter('sysparm_userID'));
    return userRecord.location + '';
  },

   getUserCompanyParent: function() {
    var userRecord = new GlideRecord("sys_user");
    userRecord.get(this.getParameter('sysparm_userID'));
    return userRecord.company.parent.getDisplayValue();
  },
	type: 'getLocation'
});

 

Can we use two glideajax in one client script.

Could you please help me with client script.

 

TIA

 

1 ACCEPTED SOLUTION

Chaitanya ILCR
Kilo Patron

Hi @si21 ,

Yes you can make as many as GlideAjax calls you want. But for your requirement you don't have to do that.

you can make one server call and you can pull the info.

you can update the script include to below

var getLocation = Class.create();
getLocation.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getUserLoc: function() {
        var userRecord = new GlideRecord("sys_user");
        var exm = this.getParameter('sysparm_userID');
        userRecord.get(this.getParameter('sysparm_userID'));
        return userRecord.location + '';
    },

    getUserCompanyParent: function() {
        var userRecord = new GlideRecord("sys_user");
        userRecord.get(this.getParameter('sysparm_userID'));
        return userRecord.company.parent.getDisplayValue();
    },
    getUserCompanyandLocationDetails: function() {
        var res = {};
        var userRecord = new GlideRecord("sys_user");
        userRecord.get(gs.getUserID());
        res.country = userRecord.location.country.toString();
        res.companyParent = userRecord.company.parent.getDisplayValue();
        return JSON.stringify(res);
    },
    type: 'getLocation'
});

 

and you can use this onload client script

function onLoad() {

    var gaLocation = new GlideAjax('getLocation');
    gaLocation.addParam('sysparm_name', 'getUserCompanyandLocationDetails');
    gaLocation.getXMLAnswer(function(answer) {
        var transactionFiledName = 'transaction'; //replace it with your transaction varible bakend name
        var userDetails = JSON.parse(answer);
        g_form.addOption(transactionFiledName, 't4', 'Transaction 4');
        g_form.addOption(transactionFiledName, 't5', 'Transaction 5');

        if (userDetails.country == 'USA' && userDetails.companyParent == 'ABC') {
            g_form.clearOptions('transaction');
            g_form.addOption(transactionFiledName, 't1', 'Transaction 1');
            g_form.addOption(transactionFiledName, 't2', 'Transaction 2');
            g_form.addOption(transactionFiledName, 't3', 'Transaction 3');

        }
    });
}

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

View solution in original post

1 REPLY 1

Chaitanya ILCR
Kilo Patron

Hi @si21 ,

Yes you can make as many as GlideAjax calls you want. But for your requirement you don't have to do that.

you can make one server call and you can pull the info.

you can update the script include to below

var getLocation = Class.create();
getLocation.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getUserLoc: function() {
        var userRecord = new GlideRecord("sys_user");
        var exm = this.getParameter('sysparm_userID');
        userRecord.get(this.getParameter('sysparm_userID'));
        return userRecord.location + '';
    },

    getUserCompanyParent: function() {
        var userRecord = new GlideRecord("sys_user");
        userRecord.get(this.getParameter('sysparm_userID'));
        return userRecord.company.parent.getDisplayValue();
    },
    getUserCompanyandLocationDetails: function() {
        var res = {};
        var userRecord = new GlideRecord("sys_user");
        userRecord.get(gs.getUserID());
        res.country = userRecord.location.country.toString();
        res.companyParent = userRecord.company.parent.getDisplayValue();
        return JSON.stringify(res);
    },
    type: 'getLocation'
});

 

and you can use this onload client script

function onLoad() {

    var gaLocation = new GlideAjax('getLocation');
    gaLocation.addParam('sysparm_name', 'getUserCompanyandLocationDetails');
    gaLocation.getXMLAnswer(function(answer) {
        var transactionFiledName = 'transaction'; //replace it with your transaction varible bakend name
        var userDetails = JSON.parse(answer);
        g_form.addOption(transactionFiledName, 't4', 'Transaction 4');
        g_form.addOption(transactionFiledName, 't5', 'Transaction 5');

        if (userDetails.country == 'USA' && userDetails.companyParent == 'ABC') {
            g_form.clearOptions('transaction');
            g_form.addOption(transactionFiledName, 't1', 'Transaction 1');
            g_form.addOption(transactionFiledName, 't2', 'Transaction 2');
            g_form.addOption(transactionFiledName, 't3', 'Transaction 3');

        }
    });
}

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya