Find user delegates

Mike Hill1
Giga Contributor

I am working on building a business rule.   A portion of it needs to check a user to see if they have delegates and if they do I need to run a portion of code against each of the delegates to grant each delegate the same role.  

 

Fairly new to scripting and appreciate any assistance.

 

I have the user already so just need a way to find their delegates and then reference each one for running code against them.   One way might be to check the delegate table for any entries where the user in question is listed in the delegate table under the user column.   If they are, I can pull the delegate there and then do the role addition to each delegate found for this user.  

 

Other ideas welcome as well as any code examples to get me started.

 

Thank you.

1 ACCEPTED SOLUTION

Mike Hill1
Giga Contributor

I was able to use some of Morgan's code in my solution.   Thank you!  


The variable "currentTime" is something I calculate above this snippet in conjuction with the getDateTime() function.   This allows me to query records from the delegate table where the user matches the user I am working with.  



var gr3 = new GlideRecord('sys_user_delegate');   // create new variable pulling from the delegate table


gr3.addQuery('user', current.approver);   // finds entries where the current approver is listed.


gr3.addQuery('ends', '>=', currentTime);


gr3.addQuery('starts', '<=', currentTime);


gr3.query();   // query is run


View solution in original post

3 REPLIES 3

rgm276
Mega Guru

Hey Mike


this is not a business rule, and I am not sure if you mean "reports to" as delegate


but here is a UI Macro that we use in our CMS self-service portal to checks org structure


i.e. this runs a sys_user query to see if the current user is list as the manager for anyone else


<!--~~~~~TEST FOR MANAGER~~~~-->


      <g:evaluate>


              var myAssc = new GlideRecord('sys_user');


              myAssc.addQuery('manager', gs.getUserID());


              myAssc.setLimit(1);


              myAssc.query();


      </g:evaluate>


  <j:if test="${myAssc.next()}">


                  then do something for this person who is a manager


</j:if>


Does this help?




var now = gs.now();/* Not 100% sure this doesn't have a time zone offset issue; I look for delegates as whole day chunks though */



var gr3 = new GlideRecord('sys_user_delegate');


gr3.addQuery('user.user_name', assignee).addOrCondition('user', assignee);


                  //The variable assignee in my case could be a user reference (sys_id) or username so I check both


gr3.addQuery('starts', '<=', now);


gr3.addQuery('ends', '>=', now);


gr3.query();


if (gr3.hasNext()) {


      assignee = 'jdoe'; //In my case, I wanted a 2nd person to be assigned, not necessarily the person delegated


     


}


Mike Hill1
Giga Contributor

I was able to use some of Morgan's code in my solution.   Thank you!  


The variable "currentTime" is something I calculate above this snippet in conjuction with the getDateTime() function.   This allows me to query records from the delegate table where the user matches the user I am working with.  



var gr3 = new GlideRecord('sys_user_delegate');   // create new variable pulling from the delegate table


gr3.addQuery('user', current.approver);   // finds entries where the current approver is listed.


gr3.addQuery('ends', '>=', currentTime);


gr3.addQuery('starts', '<=', currentTime);


gr3.query();   // query is run