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.

Script include for fetching user record details via glideajax

Roshini
Giga Guru

Am trying to get the user location for onchange of a user field(contract administrator). I have wrote a script a=include and using onchange client script. AM getting the alerts for cleint script but no logs getting hit insidie the script include.

Script include:-
client callable :- checked 
Application :- global
Script:

var UserDetailsUtil = Class.create();
UserDetailsUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUserValues: function() {

        var location = '';
        var userSysID = this.getParameter('sysparm_userID');
        var GrUser = new GlideRecord("sys_user");
        GrUser.addQuery("sys_id", userSysID); //comparing sysID
        GrUser.query();
       
        if (GrUser.next()) {
            //getting user location
            location = GrUser.location;
        }
        return location;
    },
    type: 'UserDetailsUtil'
});


Client script:-

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   var userSysID = g_form.getValue('contract_administrator');
   alert("clinet script called " + userSysID);
   var GA = new GlideAjax('UserDetailsUtil');
   alert("inside GA");
   GA.addParam('sysparam_name', 'getUserValues');
   alert("called the function GA");
   GA.addParam('sysparm_userID', userSysID );
   alert("passed user id GA");
   GA.getXMLAnswer(callback);
alert("post call back function GA");
   function callback(resp)
   {
    alert("inside callback function");
    var ans = resp;
    alert("ans is " + ans);
    g_form.setValue('location', ans);
   }
   
}
1 ACCEPTED SOLUTION

Roshini
Giga Guru

I have rearranged the script and it worked

   getUserDetails: function() {
        var userID = this.getParameter('sysparm_userID');
        var GRuser = new GlideRecord('sys_user');
        GRuser.get(userID);
        var results = {
            "location": GRuser.getValue("location")
        };
        return JSON.stringify(results);
    }

View solution in original post

3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@Roshini Could you please update your script include script as follows.

 

var UserDetailsUtil = Class.create();
UserDetailsUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUserValues: function() {

        var location = '';
        var userSysID = this.getParameter('sysparm_userID');
        var GrUser = new GlideRecord("sys_user");
        GrUser.addQuery("sys_id", userSysID); //comparing sysID
        GrUser.query();
       
        if (GrUser.next()) {
            //getting user location
            location = GrUser.location+''; //converted to string.
        }
        return location;
    },
    type: 'UserDetailsUtil'
});

Hope this helps.

 

Thanks for your quick response, I tried the modifications but still not working.

Roshini
Giga Guru

I have rearranged the script and it worked

   getUserDetails: function() {
        var userID = this.getParameter('sysparm_userID');
        var GRuser = new GlideRecord('sys_user');
        GRuser.get(userID);
        var results = {
            "location": GRuser.getValue("location")
        };
        return JSON.stringify(results);
    }