Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Glide Ajax is returning null value in UI script

Deepak89
Tera Expert

Hello Guys,

i am calling script include from UI script , but i am getting null response ( alert line 10)  , if you may say that issue in Script include but i have tested same si in on load script and it gave my response but UI script its giving null

  

UI Script :

 

addLoadEvent(function() {
    var url = document.URL;


    var company = new GlideAjax('SOG_UserCompany');
    company.addParam('sysparm_name', 'getComp');
    company.addParam('sysparm_user', g_user.userName );
    company.getXMLWait();
    var x = (company.getAnswer());
    //alert(x);
   

    if (g_user.hasRoleExactly('snc_external', true) && !g_user.hasRoleFromList('snc_internal, admin', true) && document.URL.indexOf('.do') != -1) {


        var mfaUrl1 = '/multi_factor_auth_setup_page.do';
        var mfaUrl2 = '/validate_multifactor_auth_code.do';
        var mfaUr13 = 'assessment_take2.do';
        if (url.indexOf(mfaUrl1) > 0 || url.indexOf(mfaUrl2) > 0 || url.indexOf(mfaUr13) > 0) {
            //ignore expections
            return;
        }
        window.location = x;
    }


});
 
 
Script Include ;
 
var SOG_UserCompany = Class.create();
SOG_UserCompany.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getComp: function() {
        var user = this.getParameter("sysparm_user");
        var userRecord = new GlideRecord("sys_user");
        userRecord.get("sys_id", user);

        //  var userCompanyID = gs.getUser().getCompanyID();
        // Gemeente Amsterdam
        if (userRecord.company == "a3cc7a2c8754fc1065cfcbb5cebb3529") {
            return "/adw";
        }
        //ISEC
        else if (userRecord.company == "4116f7d1978aa590bc7abaffe153afa4") {
            return "/isec";
        }
        //Every body else
        else {
            return "/csm";
        }
    },

    type: 'SOG_UserCompany'
});
7 REPLIES 7

Hello Sandeep

Thanks for replying 

initially i used g_user.userID only but same giving null value.

JUST FYI: when i impersonating with end user its giving me null value but for me its giving alert which is else condition .

is it bcoz g_user method does not worked in UI script for portal  

 

@Deepak89 I found this thread  https://www.servicenow.com/community/developer-forum/getting-a-user-object-in-a-ui-script/m-p/169583... where responders gave some helpful suggestions to get user object on UI Script. Please see if this helps you.

@Deepak89 If you are unable to capture the logged in user via client script, can't you use gs.getUserID() on the server side?