Client script to bring manager name into a catalog item - tweak to this script?

Sam10
Tera Expert

Hi Guys,

 

I'm trying to populate manager name of an employee in a catalog item. This form once loaded will contain logged in user's name and the manager name should display in the "hiring_manager" field. If it's changed at any point then the field should clear and bring the new user's manager name. 

 

I have got this right with the client script below but only if I change the "hiring_manager" field to a simple text field. It is originally a reference field and I can't change it as it impacts to the workflow of the catalog item. Once I change the hiring manager field back to "reference", script stops working. 

 

Please advise. I'm not from a dev background so any help is appreciated. 

 

-------------------------------------------------------------------------------------------------------

 

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

//Populate Manager Name
var userMobilega = new GlideAjax('XXUserDetailsAjax');
userMobilega.addParam('sysparm_name', "getManager");
userMobilega.addParam('sysparm_requestor', g_form.getValue("requested_for_set"));
userMobilega.getXMLAnswer(handleResponseManager);

function handleResponseManager(response) {
// alert(response);
if (response) {
g_form.setValue("hiring_manager", response);
}
}
}

 

 

 

**XXUserDetailsAjax - this is a function written by our vendor with nested functions to bring various different fields from sys_user record. 

1 ACCEPTED SOLUTION

Add this new function to the script include

 

//Get User's Manager sysid
getManagerSysId: function() {
var user = this.getParameter('sysparm_requestor');
var userGR = new GlideRecord("sys_user");
userGR.addQuery("sys_id", user);
userGR.query();
if (userGR.next()) {
return userGR.getValue('manager');
}

},

 

 And your client script updated to

 

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

//Populate Manager Name
var userMobilega = new GlideAjax('XXUserDetailsAjax');
userMobilega.addParam('sysparm_name', "getManagerSysId");
userMobilega.addParam('sysparm_requestor', g_form.getValue("requested_for_set"));
userMobilega.getXMLAnswer(handleResponseManager);

function handleResponseManager(response) {
// alert(response);
if (response) {
g_form.setValue("hiring_manager", response);
}
}
}

 


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

View solution in original post

8 REPLIES 8

Kartik Choudha1
Tera Guru

Hey Sam,

 

At client end when you receive response from server through glideAjax, we receives the response in XML format.

And the 'answer' variable contains the returned values.

Instead of passing the response directly in setValue() method.

 

In handleResponseManager method write below line-

 

var data = response.responseXML.documentElement.getAttribute("answer");

 

Try alert(data) and check the returned response.

 

Note - For 'reference field' you can try passing the 'sys_id' of user as an argument in setValue()

 

Check below links for reference-

https://www.servicenow.com/community/itom-forum/getting-sys-id-in-client-script/m-p/1024948

https://www.servicenow.com/community/it-service-management-forum/how-to-pass-sys-ids-from-script-inc...

 

Hope this helps!

Regards,

Kartik Choudhary

 

 

SanjivMeher
Kilo Patron
Kilo Patron

You may need to modify the script include XXUserDetailsAjax to return the sysid of the manager instead of name.

So either you have to modify getManager() function. If this function is already used at other places, create another function in the script include getManagerSysid() to return the sysid and use it in your client script.


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

Hi Sanjiv, 

 

Thanks for the answer. I'm new to the scripting space so do you mind modifying the script for me if it's not too much of a hassle. 

@Sam10 Could you please share the script in the function getManager in the script include


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