How to Auto Populate Manager in catalog item request

krishna115
Tera Contributor

I crated "Demo Catalog Project" Catalog Item and Some Variables in that item. If user selects any Requester(sys_user). Then Manager field (single line text) should be auto populated with requester Manager. 

I created below catalog OnChange client script but auto populating only sys_id of Manager.

Please correct my script.

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

 var user = g_form.getValue('requester');
 var mgr = new GlideRecord("sys_user");
 mgr.addQuery("sys_id", user);
 mgr.addQuery();
 while(mgr.next())
 {
 g_form.setValue("manager", mgr.manager);


   }

}

10 REPLIES 10

Mark Roethof
Tera Patron
Tera Patron

Hi there,

If you are on Quebec, no need to script this anymore. Read about it in this article I wrote:
Catalog Data Lookup Definition on any table, eliminating Catalog Client Scripting

If not yet on Quebec, Client Script with GlideAjax + Script Include is the best answer. Some explanation and examples:
Client Side Scripting: Go for GlideAjax (with getXMLAnswer)!

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

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

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you cannot dot walk 2 levels to get manager name and it is not recommended to use GlideRecord in Client Side

so you need to use GlideAJax approach

script include:  it should be client callable

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

	getManager:function() {

		var user = this.getParameter('sysparm_requster');
		var mgr = new GlideRecord("sys_user");
		mgr.addQuery("sys_id", user);
		mgr.addQuery();
		if(mgr.next()){
			return mgr.manager.name;
		}

	} ,

	type: 'UserDetails'
});

Client Script:

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

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

	var ga = new GlideAjax('UserDetails');
	ga.addParam('sysparm_name', "getManager");
	ga.addParam('sysparm_requster', g_form.getValue('requester'));
	ga.getXMLAnswer(function(answer){
		if(answer != ''){
			g_form.setValue("manager", answer);
		}
	});
	//Type appropriate comment here, and begin script below

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi,

Your script is not fulfill my requirement.

Thankyou.

Hi,

So as per your requirement I have shared the script to get manager name based on the user you selected

so what did not work as expected.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader