Get manager for requested for field

Pat Surtan
Tera Expert

Hi,

 

I want to show/get the manager for the requested for field within the Now platform side. The manager field name is u_manager. I am using this client script but it is not working.

 

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

   //Type appropriate comment here, and begin script below
       var usr = g_form.getReference('requested_for',callBack);
}
function callBack(usr){
        g_form.setValue('manager',usr.manager);
}

 

Please help. Thanks in advance.

1 ACCEPTED SOLUTION

No the manager field should stay as a reference to the sys_user table. But I see the mistake. In your client script line 8 

ga.addParam("sysparm_name", "getManager");

This sysparm_name parameter ALWAYS needs to be the same value as your function name in the script include which in this case for you is called "getManagerAjax". So update that line to this

ga.addParam("sysparm_name", "getManagerAjax");

Additionally line 9 you can update to be

ga.addParam("sysparm_requestedFor", newValue);

And thats all.

This is how a Ajax structure generally looks like

var ga = new GlideAjax("Script Include Name goes here");
ga.addParam("sysparm_name", "name of the function you want to call from the above include goes here");
ga.addParam("sysparm_whateverVariableNameYouWantToSendAsParameter", "then the value you want to send in the left defined variable");
ga.getXMLAnswer(function(answer){
//logic you want to do with the answer your script include function returns
});

Hopefully this will help you in the future 🙂

View solution in original post

11 REPLIES 11

Ethan Davies
Mega Sage
Mega Sage

Hi @Pat Surtan ,

You mentioned that your field name is u_manager but that is not the field you are trying to set in your setValue, it should be:

function callBack(usr){
        g_form.setValue('u_manager',usr.manager);
}

Hi Ethan,

 

I made the change you provided but it still doesn't work. Anything else you can think of?

harun_isakovic
Mega Guru

Hi create an AJAX call, do not use GetReference it is bad practice, same for dot walking into fields on client side. I suggest you do the below.

You can find more details about it here
https://community.servicenow.com/community?id=community_article&sys_id=c918c3b8db968c5013b5fb2439961918

 

But here is an example of what you can do below.

Create a client script

var ga = new GlideAjax("ExampleInclude");
gr.addParam("sysparm_name", "exampleFunction");
gr.addParam("sysparm_requestedFor", g_form.getValue("requested_for"));
gr.getXMLAnswer(function(answer){
//then here put the answer variable into whatever field you need for example
g_form.setValue("u_manager", answer);
});

Create a client callable script include 

Name: ExampleInclude
Client callable: True(ticked)

exampleFunction: function () {
var requestedFor = this.getParameter("sysparm_requestedFor");
var gr = new GlideRecord("sys_user");
gr.get(requestedFor);
return gr.manager.toString();
},

Please mark helpful/correct if it indeed was.

Hi Harun,


This seems like a bit much just to get the manager for the name in the Requested for field. Is there a simpler way to do this?