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

Try below script and add it as onbefore BR

 

current.u_vp_name = findVP(current.manager);   ///my exact field name is u_vp_name

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

thanks removed the existing code and add the above as an before insert, update, query BR and still did not work  any other ideas

XCool.....Lets add some debug statement. Can you confirm, you have this BR on sys_user taboe and it is a before update BR

 

current.u_vp_name = findVP(current.manager);   ///my exact field name is u_vp_name

function findVP(manager_sys_id)
{

gs.addInfoMessage('++++inside VP++++');
var user = new GlideRecord('sys_user');
user.get(manager_sys_id);

gs.addInfoMessage('++++Manager found is++++'+user.name+'+++++with title+++'+user.title);
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

yes BR is before on insert, update or query on  Sys_user table 

 

here is the debug

++++inside VP++++
Info Message
++++Manager found is++++undefined+++++with title+++undefined
 
The user selected is me and I have a VP defined

dennisandrison
Kilo Expert

so I got this code to work however, the current.u_vp_name is setting to the first manager that is return not the vp name that is return any ideas on how to fix that?

 

 

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



var manager_sys_id=gs.getUserID();
gs.addInfoMessage('++++user sysid P++++' + manager_sys_id);
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);
if (user.title.indexOf('VP')==-1)
{


findVP(user.manager);
gs.addInfoMessage('++++VPPPPPP found is++++'+user.name+'+++++with title+++'+user.title);
current.u_vp_name=findVP(user.manager.name);
gs.addInfoMessage('++++++++'+user.manager.name+'+++++with title+++'+user.title);
}
else
{
return(user.sys_id);
}
}
})(current, previous);