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.

How to send approval after finding active manager recursively ??

Manikantahere
Tera Contributor

I need to send dynamic approval to the requestor manager if requestor manager is inactive I need to check with his manager if he also in active need to check with his manager this should happen until I found active manager and approval should trigger.

any logic help?

6 REPLIES 6

Brad Bowman
Kilo Patron
Kilo Patron

You can do this in a Workflow Approval - User activity script like this:

var mgr = current.opened_by.manager; //requestor's manager
//var mgr = current.variables.variable_name.manager; //using a reference variable to determine requestor 
var mgrAct = false;

if (mgr.active){
	answer.push(mgr.toString()); //requestor's manager is active, so generate the approval and exit script
	mgrAct = true;
}

while (!mgrAct) {
	var mgrGr = new GlideRecord('sys_user');
	if (mgrGr.get(mgr)) {
		if (mgrGr.manager.active) {
			answer.push(mgrGr.manager.toString()); //this manager is active, so generate the approval and exit script
			mgrAct = true;
		} else {
			mgr = mgrGr.manager.toString(); //run the GlideRecord again, retrieving this user
		}
	}
}

Chaitanya ILCR
Mega Patron

Hi @Manikantahere ,
try this out

function findActiveManager(userID) {
    var gr = new GlideRecord('sys_user');
    if (gr.get(userID)) {
        while (gr.getValue('manager') && !gr.manager.active) {
            if (!gr.get(gr.getValue('manager'))) {
                return ''; // returns empty string if the user doesn't have manager
            }
        }
        return gr.getValue('manager') ? gr.getValue('manager') : ''; //returns the active manager if exists else returs empty value
    }
    return ''; // if the passed sysid of the user doesn't exist resturns the empty string
}
gs.print(findActiveManager('put d UserID here'));

ChaitanyaILCR_0-1730050356071.png

 

Please mark the answer as helpful and correct if helped.

Regards,

Chaitanya