Get user data from portal client script

davidgustafsson
Tera Expert

I am trying to fetch some user data from current user in a client script on a catalog item. I am trying to get it through a script include but somehow my "answer" is is still undefined as it is returned. Could anyone of you please help me find some issue in my code.

I have a field with name "phone_number" on the form.

Catalog client script:

function onLoad() {
	var ga = new GlideAjax('GetUserData');
	ga.addParam('sysparm_name', 'getUserData');
	ga.addParam('sysparm_userID', "user");
	ga.getXML(userDataResponse);
}

function userDataResponse(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	var clearvalue; // Stays Undefined
	var returneddata = answer.evalJSON(true);
	g_form.setValue("phone_number", returneddata.phone);
}

 

And the script include:

var GetUserData = Class.create();
GetUserData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUserData: function() {
		var user = new GlideRecord('sys_user');
		user.addQuery('sys_id', gs.getUserID());
		user.query();
		user.next();
		var json = new JSON();
		var results = {};
		results.sys_id= user.sys_id;
		results.first_name= user.first_name;
		results.last_name= user.last_name;
		results.name= user.name;
		results.cost_center= user.cost_center;
		results.department= user.department;
		results.active= user.active;
		results.email= user.email;
		results.phone= user.phone;
		results.mobile_phone= user.mobile_phone;
		results.source= user.source;
		return json.encode(results);
	},
	type: 'GetUserData'
});
1 ACCEPTED SOLUTION

SanjivMeher
Kilo Patron
Kilo Patron

Can you pass few parameters and try?

 

Also instead of onLoad, you can use default value, something like

 

javascript: var user = new GlideRecord('sys_user'); user.get(gs.getUserID()); user.phone;


Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

4 REPLIES 4

SanjivMeher
Kilo Patron
Kilo Patron

Can you pass few parameters and try?

 

Also instead of onLoad, you can use default value, something like

 

javascript: var user = new GlideRecord('sys_user'); user.get(gs.getUserID()); user.phone;


Please mark this response as correct or helpful if it assisted you with your question.

Thank you soooo much. This worked for me to fetch manager data as default value

ChrisBurks
Mega Sage

I believe the main issue is the way the population of the results properties in the script include. You'll need to make sure that the results properties are receiving the real values and not the object being passed. 

If you were to console.log out the response in the GlideAjax callback you'll see that the properties of your results are populated with empty objects.

To resolve this the .getValue or .getDisplayValue methods should be used.
i.e.:  result.first_name = user.getValue('first_name'); // this will populate the property properly

 

A few other notes:

1) If .evalJSON() doesn't work in Service Portal switch to JSON.parse()

2) Since you're defining the user sys_id within the Script Include, you don't need to send it as a GlideAjax param

3) As Sanjiv Meher suggested, you can just use "user.get('sys_id', gs.getUserID());" instead of "user.addQuery('sys_id', gs.getUserID());"
    Then you can remove user.query() and user.next() as the .get method will already "get" the record if it exists. This is more performant
    than having the system search the whole sys_user table when it already knows the sys_id.

    Example:

    var user = new GlideRecord('sys_user');
    user.get('sys_id', gs.getUserID());
    if(user.isValidRecord()){
          // do some stuff with the record

         ...
    }

 

 

Jim Coyne
Kilo Patron

Just to be clear, if you are using the default value method, is the context for this Catalog Item only the current user?  i.e. the current user cannot select another user the item is for?

Reason I ask is the default value method would not work in that context.