ccajohnson
Kilo Sage

I did something similar whereby they wanted to add each person to the approval listing and move up the hierarchy until the appropriate conditions were found. Taking your existing script, I added the logic to get the Manager from the user, then check to see if they have the correct title, then if they do not, move up the hierarchy. See if this works for you:

answer = []; //used to build the list of approvers
var reqUser = current.opened_by.sys_id;
findMgr(reqUser);
//finds the manager of the user 
function findMgr(usrID) {
    var usrObj = new GlideRecord('sys_user');
    if (usrObj.get(usrID)) {
        //does the user has a manager
        if (!usrObj.manager.nil()) {
            var usrMgr = usrObj.manager; //get the current users manager
            checkMgr(usrMgr); //run the check manager function
        }
    }
}

//checks the manager for appropriate approval rites
function checkMgr(mgrID) {
    var mgrCheck = new GlideRecord('sys_user');
    if (mgrCheck.get(mgrID)) {
        //is the manager the Director
        if (mgrCheck.title.toString().toLowerCase().indexOf('director') > -1) {
            answer.push(mgrCheck.sys_id.toString()); //add to the list
        } else {
            var usrID = mgrCheck.sys_id; //get the sysID of the current Manager record
            findMgr(usrID); //run the find manager function
        }
    }
}

View solution in original post