flow or workflow: dot walk user.manager until manager is member of certain group

Les1
Tera Guru

objective: in order to find the right approver, need to dot walk the requested_for.manager path until manager is a member of a particular group, and then use that manager for the approval.  i'm open to this being a script in workflow but it seems like i should be able to make a "is an exec" subflow and put into a catalog flow loop to check,  so that it is re-usable 

 

so i'm envisioning a catalog item flow:

flow start

do the following until approval user is an exec:

     dot walk requested_for.manager.manager

           subflow to check if the current manager is part of group Exec

set approver

<..do whatever other item flow stuff ..> 

flow end

 

can anyone lend insight though how to accomplish the dot walking up the manager chain, then input to the subflow to verify if part of group = exec?

1 ACCEPTED SOLUTION

Either one, but you'll need to account for the possibility that it returns undefined.

e.g., for a workflow activity you'd change the 4th line to

answer = FindApprover(user, group);

and delete the gs.info line.

If there's a fall-back for an approver not found you can do something like:

var approvalUser = FindApprover(user, group);

if (approvalUser) {
    answer = approvalUser;
}
else {
    answer = 'fall_back_user';
}

 

You can also turn it into a script include and call it from wherever you want 🙂

View solution in original post

6 REPLIES 6

_ChrisHelming
Tera Guru

Have a function call itself. It'll return undefined if comes across a user who doesn't have a manager while going up the chain.

var user = current.requested_for.sys_id;
var group = 'Database';

var approvalUser = FindApprover(user, group);
gs.info(approvalUser);

function FindApprover(user_sys_id, groupName) {
    if (!gs.getUser().getUserByID(user_sys_id).isMemberOf(groupName)) {
        var user = new GlideRecord('sys_user');
        user.get(user_sys_id);
        if (user.getValue('manager')) {
            return FindApprover(user.getValue('manager'), groupName);
        }
        else {
        	return;
        }
    }
    else {
      return user_sys_id;
    }
}

that looks pretty interesting, where am i utilizing that code? Would that just go into a Workflow Approval - User activity in the advance script ?   

Or put it in a Flow within Ask for Approval as a scripted action?

 

Either one, but you'll need to account for the possibility that it returns undefined.

e.g., for a workflow activity you'd change the 4th line to

answer = FindApprover(user, group);

and delete the gs.info line.

If there's a fall-back for an approver not found you can do something like:

var approvalUser = FindApprover(user, group);

if (approvalUser) {
    answer = approvalUser;
}
else {
    answer = 'fall_back_user';
}

 

You can also turn it into a script include and call it from wherever you want 🙂

Wow, thanks Chris! i think your code is really gonna do what i was needing to fulfill. i just had to tweak the forst 'var user = '    to be a catalog item variable we are using as the 'requested for' instead of your suggested your suggested current.requsested_for.sys_id because we are using old cat-items that were before servicenow release an oob var type of  "requested for"  .. 

but after making that change it looks like initial testing is working well. like you said i need to account for undefined, as well as if for some horrid reason that the approver is INACTIVE (ugh, it happens).   by default most of our items have a 'manager approver' so if the user found by the script you helped me with is the same as the requested for's mgr, then that'll mean double approval fun for that mgr ha! so i'm sure i'll get asked to deal with that as well.

 

thanks greatly for providing your assistance!