Business rule for delegate approval

Swati36
Tera Contributor

Dear fam,

 

We have a requirement to create a business rule on the approval table such that if the user is a VIP user then the respective request approvals should get triggered to their Executive Delegate User ( this is a custom field we have created on user table ). But if there is no executive delegate user updated for VIP user or if the executive delegate user is inactive then in such cases the approval should be triggered to same VIP user itself.

 

I am trying to achieve this by writing a Before Insert BR on Approvals table. This is the code I have come up with so far but need help on this please - 

=================================

(function executeRule(current, previous /*null when async*/) {

var delegateUser = new GlideRecord('sys_user_delegate');
delegateUser.addQuery('user', current.approver);
delegateUser.query();
while(delegateUser.next())
{
var count = delegateUser.getRowCount();
if(delegateUser.delegate.active == 'false' || count<1)
{
// trigger approval to VIP user
}
else
{
current.approver = approver.u_executive_delegate;
}
}

})(current, previous);

=======================

 

Thank you! 🙂

1 ACCEPTED SOLUTION

Laszlo Balla
Mega Sage
Mega Sage

It sounds to me that you are only interested in whether there is an active executive delegate or not, which is a field on the user table, so I don't understand why the sys_user_delegate table is relevant.

To achieve what you wish, you just do something like this:

var execDelegate, delegateActive;

execDelegate = current.approver.u_executive_delegate;


if(execDelegate) {
  delegateActive = (current.approver.u_executive_delegate.active == true); // this will make this variable a boolean
}

if(delegateActive) {
 current.setValue('approver',execDelegate);
}

View solution in original post

4 REPLIES 4

Sai Kumar B
Mega Sage
Mega Sage

@Swati36 

When you say a user is VIP is he an approver on the approval record? If yes use the following code

(function executeRule(current, previous /*null when async*/) {
//If current approver is VIP, then set delegated user as approver
if(current.approver.vip && current.approver.u_executive_delegate.active) //If the current approver is VIP and the Executive delegate user is active
{
current.setValue('approver', current.approver.u_executive_delegate;
} 
})(current, previous);

 

Laszlo Balla
Mega Sage
Mega Sage

It sounds to me that you are only interested in whether there is an active executive delegate or not, which is a field on the user table, so I don't understand why the sys_user_delegate table is relevant.

To achieve what you wish, you just do something like this:

var execDelegate, delegateActive;

execDelegate = current.approver.u_executive_delegate;


if(execDelegate) {
  delegateActive = (current.approver.u_executive_delegate.active == true); // this will make this variable a boolean
}

if(delegateActive) {
 current.setValue('approver',execDelegate);
}

Heyy Laszlo,

 

I dont know what I was thinking by trying to use sys_user_delegate table! 
Your code worked perfectly. Thank you! 

J_31
Kilo Sage

(function executeRule(current, previous /*null when async*/) {

var delegateUser = new GlideRecord('sys_user_delegate');
delegateUser.addQuery('user', current.approver);
delegateUser.query();
if(delegateUser.next()) {
if(delegateUser.delegate.active && delegateUser.delegate.u_executive_delegate) {
current.approver = delegateUser.delegate.u_executive_delegate;
} else {
// trigger approval to VIP user
}
} else {
// trigger approval to VIP user
}

})(current, previous);