Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Get Manager name based an selected user

ar1
Kilo Sage

Hi team,

i am trying to set Manager field based on the requested for filed both are reference type..

Any help would be appreciated...........

 

Thanks in advance  

Below is the script ::

 

Client Script :

 

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

}

var userID = g_form.getValue('requested_for');
alert(userID);      // it's returning sys_id of the user

var user = new GlideAjax('GetManagerName');
user.addParam('sysparm_name','GetManagerName');
user.addParam('sysparm_requsr',userID);
user.getXML(callbackFunction);

function callbackFunction(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('manager',answer);
}
}

 

Script Include :

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

GetManagerName: function(){
var requsr = this.getParameter('sysparm_requsr');
var req = new GlideRecord('sys_user');

req.addQuery('sys_id',requsr);
req.query();
while(req.next())
{
var mangr = g_form.getValue('manager');
return mangr;
}
},

type: 'GetManagerName'
});

10 REPLIES 10

Allen Andreas
Tera Patron

Hi,

You really didn't have to do all that, you can use getReference, like so:

function onChange(control, oldValue, newValue, isLoading) {
    g_form.getReference('requested_for', doAlert); // doAlert is our callback function
}

function doAlert(user) { // reference is passed into callback as first arguments
   g_form.setValue('manager', user.manager);
}

Please mark reply as Helpful/Correct.

Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi,

 

I am using this below code 

 

You need a client-callable script include: 

var GetManagerName = Class.create();
GetManagerName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getManagerName: function(){
var userRecord = new GlideRecord('sys_user');
userRecord.get(this.getParameter('sysparm_userID'));
return userRecord.manager.getDisplayValue();
},
type: 'GetManagerName'
});

 

And onChange client script for "requested_for" field:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading && !g_form.isNewRecord() || newValue == '' || newValue == oldValue) {
return;
}
var getManagerName = new GlideAjax('GetManagerName');
getManagerName.addParam('sysparm_name', 'getManagerName');
getManagerName.addParam('sysparm_userID', g_form.getValue('requested_for'));
getManagerName.getXML(populateManagerName);

function populateManagerName(response){
var nameFromScriptInclude = response.responseXML.documentElement.getAttribute('answer');
g_form.clearValue('manager_name');
g_form.setValue('manager_name',nameFromScriptInclude);
}
}

Note : Please mark my answer correct / helpful so this question will not be wipped out from the community . 

 

Note: I don't beg for points.

 

 

Hello Pavan,

 

thanks for the reply..

 i tried what you suggested above but it'snot working..

 

And my requirement is :

 

we're trying to set the manager's name on the user's form...we  have the same two fields on the user form whenever  we change the requested for [reference type] user name on the form it's also change manager's name on the form.

Hi Alien,

 

Thanks for your immediate response, 

 

It's working perfectly on SP Page....

 

 

But my Requirement is :

 

we're trying to set the manager's name on the user's form...we  have the same two fields on the user form whenever  we change the requested for [reference type] user name on the form it's also change manager's name on the form.