Skip level approval to a manager with VIP tag on catalog approval tasks

kiran065
Tera Expert

hello,

Generally we have an option to send the approval task to the manager or to the higher levels by dot walking. (sys_user.manager.manager.manager). But my requirement is to approval has to dot walk till the higher level of manager has a VIP tag or not. If the hierarchy has the VIP tag then the approval needs to go to him. So automatically the approval task has to look for the preceding manager with a VIP tag and trigger to approval email to him.

I did write try a script in the approval task which is not working. Could someone help to correct the script or if there is any other possible way to do it. Thanks.

answer = []; {

        if ('sys_user.manager.vip', true) {

                    return true;

        }else if('sys_user.manager.manager.vip', true) {

                  return true;

        }else if ('sys_user.manager.manager.manager.vip', true) {

                  return true;

        } else if('sys_user.manager.manager.manager.manager.vip', true) {

                  return true;

        } else if ('sys_user.manager.manager.manager.manager.manager.vip', true) {

                  return true;

        } else {

                  return false;

    }

}

answer.push();

1 ACCEPTED SOLUTION

I was able to modify my previous code to suit your requirements.


What this function does is check up the heiarchy from the user record up a given number of times.


If the manager in question has the VIP flag set, it adds it to the approval array.


If the heiarchy has been traversed and no VIP is found, the highest manager is added to the approval array.



Here is what you can do:


1.   Create a Script Include to hold the code. I do this for several reasons:


      -     It allows you to re-use the code in different workflows without having to copy/paste.


      -     It allows you to modify the code in one place, rather than checking out several workflows to make a change.


      -     It allows you to add other common functions that relate to approvals for the same reasons above.


      Here are the details for the script include:


Name: approvalUtils


Application: Global


Accessible from: All application scopes.


Description: Custom Script Include containing common Approval functions.


Script:


var approvalUtils = Class.create();


approvalUtils.prototype = {


      initialize: function() {


      },



      getVipApprover: function(usrID, tCount) {


              var usrObj = new GlideRecord('sys_user');


              usrObj.get(usrID);


              var appArray = [];


              var mgrObj = new GlideRecord('sys_user');


              var cCount = 0;


              while (cCount < tCount) {


                      mgrObj.get(usrObj.manager);


                      if (mgrObj.vip) {


                              appArray.push(mgrObj.sys_id.toString());


                              break;


                      } else {


                              usrObj = mgrObj;


                              cCount++;


                              if (cCount == tCount) {


                                      appArray.push(mgrObj.sys_id.toString());


                              }


                      }


              }


              return appArray;


      },



      type: 'approvalUtils'


};



2.   Change your Approval workflow activity to call the function from the script include.


      -     There are two inputs to the function: usrID, tCount


              usrID:   corresponds with the reference field that points to the User [sys_user] table. For my example, I will be using the Requested for from the Requested Item record


              tCount: corresponds with the number of tiers to go up from the original user. Your original logic had 5, but you can add more or less.


      -     Here is the syntax you will use in your workflow activity:



var sInc = new approvalUtils();


answer = sInc.getVipApprover(current.request.requested_for, 5);



Let me know if you have any questions or need anything explained further.


View solution in original post

4 REPLIES 4

ccajohnson
Kilo Sage

I am not sure if you are having the person with VIP approve, or the person below the person with VIP approve.



I did something similar whereby the person who was approving had to have some field checked in their user record, otherwise it would traverse up the tree to check the next person. Let us know what the criteria is for your approval logic so we can narrow our solution accordingly.


Hi Chris,



Thanks for the reply. Every sys_user record has VIP field with True or False. Not all the managers are tagged with VIP flag. So based on the hierarchy next level might not have the vip flag. As we move ahead the Director and above are tagged with VIP flag turned on. So when the approval triggers it has to look next levels and only when the VIP flag is true that person should receive the approval email.



Please let me know if I made myself clear. Thanks


I was able to modify my previous code to suit your requirements.


What this function does is check up the heiarchy from the user record up a given number of times.


If the manager in question has the VIP flag set, it adds it to the approval array.


If the heiarchy has been traversed and no VIP is found, the highest manager is added to the approval array.



Here is what you can do:


1.   Create a Script Include to hold the code. I do this for several reasons:


      -     It allows you to re-use the code in different workflows without having to copy/paste.


      -     It allows you to modify the code in one place, rather than checking out several workflows to make a change.


      -     It allows you to add other common functions that relate to approvals for the same reasons above.


      Here are the details for the script include:


Name: approvalUtils


Application: Global


Accessible from: All application scopes.


Description: Custom Script Include containing common Approval functions.


Script:


var approvalUtils = Class.create();


approvalUtils.prototype = {


      initialize: function() {


      },



      getVipApprover: function(usrID, tCount) {


              var usrObj = new GlideRecord('sys_user');


              usrObj.get(usrID);


              var appArray = [];


              var mgrObj = new GlideRecord('sys_user');


              var cCount = 0;


              while (cCount < tCount) {


                      mgrObj.get(usrObj.manager);


                      if (mgrObj.vip) {


                              appArray.push(mgrObj.sys_id.toString());


                              break;


                      } else {


                              usrObj = mgrObj;


                              cCount++;


                              if (cCount == tCount) {


                                      appArray.push(mgrObj.sys_id.toString());


                              }


                      }


              }


              return appArray;


      },



      type: 'approvalUtils'


};



2.   Change your Approval workflow activity to call the function from the script include.


      -     There are two inputs to the function: usrID, tCount


              usrID:   corresponds with the reference field that points to the User [sys_user] table. For my example, I will be using the Requested for from the Requested Item record


              tCount: corresponds with the number of tiers to go up from the original user. Your original logic had 5, but you can add more or less.


      -     Here is the syntax you will use in your workflow activity:



var sInc = new approvalUtils();


answer = sInc.getVipApprover(current.request.requested_for, 5);



Let me know if you have any questions or need anything explained further.


Thanks a lot Chris. The idea of creating a script include so that we can re-use it is brilliant. Let me try this and you made it very easy for me to take it from here. I Owe you a big time .