onChange catalog client script to search user table and set email field

JJG
Kilo Guru

Hello,

On my catalog record producer, I have a reference field to the user table called 'name_of_recruiter'. I have an onChange catalog client script that is supposed to search for that user in the sys_user table and then return the email address of that user. The email address is then supposed to be set as the variable on the catalog record producer called 'recruiter_email'. It does not seem to be working, is my code incorrect? Thank you for any help.

 

Catalog client script:

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

var id = g_form.getValue('name_of_recruiter');


var user = new GlideRecord('sys_user');
user.addQuery('sys_id', id);
user.query();

if (user.next()) {

g_form.setValue('recruiter_email', user.email);

}

//Type appropriate comment here, and begin script below

}

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi JJG,

If I remember correctly, a few weeks back we've setup a Script Include (getUserPropertiesAJAX) with some user functions. Are you still using that and is it for the same scoped app?

If so, you could expand the Script Include slightly with:

get_email : function() {
	var grUser = new GlideRecord('sys_user');

	if(grUser.get(this.getParameter('sysparm_user'))) {
		return grUser.getValue('email');
	}
},

Then in the Catalog Client Script, use:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

	if(isLoading) {
		return;
	}

	if(newValue === '') {
		g_form.clearValue('recruiter_email');
	}

	var gaEmail = new GlideAjax('getUserPropertiesAJAX');
	gaEmail.addParam('sysparm_name', 'get_email');
	gaEmail.addParam('sysparm_user', newValue);
	gaEmail.getXMLAnswer(_handleResponse);

	function _handleResponse(response) {
		var answer = response;
		g_form.setValue('recruiter_email', answer);
	}

}

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

4 REPLIES 4

Kunal Varkhede
Tera Guru
Hi JJG,
 
You can not use GlideRecord in Catalog client script. instead of that use Script include to get the value from user table and In OnChange catalog client script use glideAjax api to set that value in your custom field.
 
Please Mark Correct answer if it help you.
Thanks and Regards,
Kunal.

 

Try this code

 

Script include- To get user Email from sys_user table and return it to catalog onChange Client script

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

	getEmail:function ()
	{ 
	var caller_name = this.getParameter('sysparm_value');//Caller name
	var gr = new GlideRecord('sys_user');
	gr.addQuery(gr.name,caller_name);
	gr.query();
	while(gr.next()) {
	return gr.email;
	}
	},
    type: 'GetEmail'
});

 

Catalog OnChange Client Script:

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	var ga =new GlideAjax('GetEmail');
	ga.addParam('sysparm_name','getEmail');
	ga.addParam('sysparm_value',newValue);
	ga.getXML(callback);
	function callback(response)
	{
		var answer = response.responseXML.documentElement.getAttribute("answer");
		g_form.setValue('recruiter_email',answer);
	}
}

 

Please Mark correct/helpful answer if it help you.

Thanks and Regards,

Kunal.

Mark Roethof
Tera Patron
Tera Patron

Hi JJG,

If I remember correctly, a few weeks back we've setup a Script Include (getUserPropertiesAJAX) with some user functions. Are you still using that and is it for the same scoped app?

If so, you could expand the Script Include slightly with:

get_email : function() {
	var grUser = new GlideRecord('sys_user');

	if(grUser.get(this.getParameter('sysparm_user'))) {
		return grUser.getValue('email');
	}
},

Then in the Catalog Client Script, use:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

	if(isLoading) {
		return;
	}

	if(newValue === '') {
		g_form.clearValue('recruiter_email');
	}

	var gaEmail = new GlideAjax('getUserPropertiesAJAX');
	gaEmail.addParam('sysparm_name', 'get_email');
	gaEmail.addParam('sysparm_user', newValue);
	gaEmail.getXMLAnswer(_handleResponse);

	function _handleResponse(response) {
		var answer = response;
		g_form.setValue('recruiter_email', answer);
	}

}

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

You're the man!