Convert Given OnChange GlideRecord Client Script to GlideAjax so Script Works while Impersonating End User

alantpham
Kilo Contributor

The following OnChange client script is being used for a catalog item I'm working on and works for admins but not for end users I try to impersonate while testing. I believe using a GlideAjax approach would work but I can't figure out how to convert this script to GlideAjax to achieve the same result. Please rescript and explain.

 

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

var user = new GlideRecord('sys_user');
user.addQuery('sys_id',newValue);
user.query(setOnBehalfDetails);
function setOnBehalfDetails(user) {
if(user.next())
{

g_form.setValue('user_id',user.user_name);

9 REPLIES 9

Aman Kumar S
Kilo Patron

Hey,

Yes you are right, GlideAjax will be needed here.

SO basically your gliderecord query will go to a script inlcude and from your onchange client script you will be making an ajax call.

Refer to below link, it should assist you:

GlideAjax cheatsheet

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions 🙂

Best Regards
Aman Kumar

Sagar Pagar
Tera Patron

Hi,

 

Here is updated scripts.

Client Scripts:

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

	var ga = new GlideAjax('ScriptIncludeName');
	ga.addParam('sysparm_name', 'getUserDetails');
	ga.addParam('sysparm_user_id', newValue);
	ga.getXML(getFunction);

	function getFunction(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
		g_form.setValue('user_id', answer); // make sure about the variable name
	}
}

 

Script Include:

var ScriptIncludeName = Class.create();
ScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
		getUserDetails: function(){
		var userId = this.getParameter('sysparm_user_id');
		var userName = '';
		
		var mygr = new GlideRecord('sys_user');
		mygr.get(userId);
		if(mygr.next()){
			userName = mygr.user_name;
		}
		return userName;
	
	},
	
	
	
    type: 'ScriptIncludeName'
});

 

Thanks,
Sagar Pagar

The world works with ServiceNow

Hi Sagar,

The variable isn't populating, is the client script the code to be put in the catalog client script? I've created the prior script as a catalog item script so I want to make sure the solution works as a catalog item script and not as an incident script. Thanks.

Yes, the script goes into a Catalog Client Script.

 

Blind guessing here, please check all 3 points:

-"Client Callable" checkbox is set to true on your Script Include

-Your script include's name is reflected in the Script Include's First, Second and second to last lines

-You should replace the dummy text "ScriptIncludeName" with your Script Include's name in your Catalog Client Script