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

Pat Surtan
Tera Expert

Hi Harun,

 

I used your scripts but it isn't working. What am I doing wrong?

 

Script include:

Client callable = true

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

 

Client script:

var ga = new GlideAjax("getManagerAjax");
gr.addParam("sysparm_name", "getManagerAjax");
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);
});
}

Hi could you post me the entire script from your script include?

If it is this what you posted then there is a typo in your script include at the very end

getManagerAjax: function () {
var requestedFor = this.getParameter("sysparm_requestedFor");
var gr = new GlideRecord("sys_user");
gr.get(requestedFor);
return gr.manager.toString();
}; <--- youve put a semi colon here, there needs to be a comma, so it needs to be as below

getManagerAjax: 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 is my script incldue:

 

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

Please post the full code of your client script and your script include, you did not seem to read my comments.

This is exactly how your script include code should look like and names should be like this also. In the script you post you are missing some code and also have a typo.

find_real_file.png

This is how the code in the client script should look like exactly, just the Field Name and Table name on the client script form need to be adjusted because you did not mention on which table this is or change of which field should trigger it

find_real_file.png

Hi Harun,

 

Attached are 3 screenshots of my code. One of the screenshots will show the error I received.

 

Update:

I see why I was getting the error. I udpated the code from "gr" to "ga". No error but the manager field did not pull up when a requested for user is selected.

 

Here is a bit more information about both fields.

 

Requested for is reference field to sys user table, active is true. The manager field is set up the same way. Should the manager field be a string field instead of reference?