How to obtain VP for a user in sys_user table

dennisandrison
Kilo Expert

HI 

 

I have a user defined field on my sys_user table.  The field is called VP.  I need to populate this field with the VP names for all my users.  I do have VP or Vice President in the title field in sys user.  Does anyone have any ideas how I can populate the VP field for my users?  The VP could be 5 or 7 layers deep when dot walking to the user.manager.manager.manager etc

1 ACCEPTED SOLUTION

dennisandrison
Kilo Expert

HI 

 

Thanks for your help  I got the below code to work 

 

(function executeRule(current, previous /*null when async*/)
{

var manager_sys_id=current.sys_id;
var managertitle = current.title;
var managerid=current.sys_id;
gs.addInfoMessage('++++user sysid is: ' + manager_sys_id);
gs.addInfoMessage('++++user title is: ' + managertitle);
gs.addInfoMessage('++++user managerid is: ' + managerid);
findVP(manager_sys_id);

function findVP(manager_sys_id)
{
var user = new GlideRecord('sys_user');
user.get(manager_sys_id);
gs.addInfoMessage('++++Manager found is++++'+user.name+'+++++with title+++'+user.title + '++++++user id :' + user.sys_id + 'indexof :' +user.title.indexOf('VP'));

if (user.title.indexOf('VP')==-1)
// gs.addInfoMessage('++++ENTER VP CODE ++++' + '++++++user id :' + user.sys_id);
findVP(user.manager);

if (user.title.indexOf('VP')==0)
{
gs.addInfoMessage('++++enter the name of VP++++' + user.name);
current.u_vp_name=user.name;
}

}
})(current, previous);

View solution in original post

13 REPLIES 13

SanjivMeher
Kilo Patron
Kilo Patron

You can write a function to find it out. For ex

 

var iuser = new GlideRecord(sys_user);
iuser.addQuery('active','true');
iuser.query();

while (iuser.next())
{
iuser.u_vp = findVP(iuser.manager);
iuser.update();
}

function findVP(manager_sys_id)
{
var user = new GlideRecord('sys_user');
user.get(manager_sys_id)
if (user.title.indexOf('VP')==-1)
{
findVP(user.manager);
}
else
{
return(user.sys_id);
}
}

 


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

dennisandrison
Kilo Expert

HI 

 

Thanks i tried this as a client script onload and this did not populate the variable.  Do you have any ideas on why?

You can't use it in a Client script. It can be used in a Business Rule.

If you want to use it in a client script, you will have to add the script to script include and call it from client script using glideajax.

 

You may need to change the script a little to make it work for GlideAjax


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

dennisandrison
Kilo Expert

thanks added as a BR here are the properties this still did not work when I updated the sys user record

 

when to run  Before insert, update, query

order 100

no filters

no actions

advance

(function executeRule(current, previous /*null when async*/) {

var iuser = new GlideRecord(sys_user);
iuser.addQuery('active','true');
iuser.query();

while (iuser.next())
{
iuser.u_vp_name = findVP(iuser.manager);   ///my exact field name is u_vp_name
iuser.update();
}

function findVP(manager_sys_id)
{
var user = new GlideRecord('sys_user');
user.get(manager_sys_id);
if (user.title.indexOf('VP')==-1)
{
findVP(user.manager);
}
else
{
return(user.sys_id);
}
}

})(curren