- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2023 07:05 AM
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! 🙂
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2023 08:01 AM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2023 07:32 AM - edited 02-23-2023 07:37 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2023 08:01 AM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2023 12:00 AM
Heyy Laszlo,
I dont know what I was thinking by trying to use sys_user_delegate table!
Your code worked perfectly. Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2023 09:07 AM
(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);