How to add a check delegate is true condition to an UI Action?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2025 02:48 AM
Dear experts,
Currently I am toggling with the delegate functions on the latest yokohama fix and for some ui action I have certain conditions only for certain role, I tried to create a script include to call in the condition field for the ui action as below, but the button will still appear after the delegate assigned time is already passed or the delegate record is being deleted. How should I approach this manner? Is my script has some improvements to undergo?
Script include:
var DelegationUtil = Class.create();
DelegationUtil.prototype = {
initialize: function() {},
isUserOrDelegate: function(originalUserId) {
var currentUserId = gs.getUserID();
if (currentUserId == originalUserId)
return true;
var gr = new GlideRecord('sys_user_delegate');
gr.addActiveQuery();
gr.addQuery('delegate', currentUserId);
gr.addQuery('user', originalUserId);
gr.addQuery('starts', '<=', gs.nowDateTime());
gr.addQuery('ends', '>=', gs.nowDateTime());
gr.query();
return gr.hasNext();
}
};
(gs.hasRole('sn_risk_advanced.Boost - BU HOD') || (new global.DelegationUtil().isUserOrDelegate)) && (current.state == 1) && (!current.isNewRecord()) && current.canRead() && current.u_submitted == true
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2025 06:17 PM
Hi Ankur, I tried to use your solution but it still does not work for me, I have checked all the things including the naming the codes, the scripts and I made sure there is a delegate record for the assigned_to user. But apparently it still does not work for me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2025 06:54 AM
did you debug that script include function?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2025 06:30 PM
Hi Ankur,
I have debugged it and this is my script
var DelegationUtil = Class.create();
DelegationUtil.prototype = {
initialize: function() {},
isUserOrDelegate: function(originalUserId) {
if (!originalUserId) {
gs.debug("[DelegationUtil] originalUserId is null or undefined.");
return false;
}
var currentUserId = gs.getUserID();
gs.debug("[DelegationUtil] Checking delegation for current user: " + currentUserId + " against original user: " + originalUserId);
if (currentUserId == originalUserId) {
gs.debug("[DelegationUtil] Current user is the original user.");
return true;
}
var gr = new GlideRecord('sys_user_delegate');
gr.addActiveQuery(); // Only active records
gr.addQuery('delegate', currentUserId);
gr.addQuery('user', originalUserId);
gr.addQuery('starts', '<=', gs.nowDateTime());
gr.addQuery('ends', '>=', gs.nowDateTime());
gr.query();
var hasDelegation = gr.hasNext();
gs.debug("[DelegationUtil] Delegate record found: " + hasDelegation);
return hasDelegation;
}
};
But the debug log only shows this which is referencing to my correst user sys_id but it does not reflect that it has found a delegation, may I know why so?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2025 08:25 PM
delegate table doesn't have active field so addActiveQuery() won't work.
I had removed it from my earlier script I shared
Did you check if that user has a valid delegate falling between those date/times.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2025 08:29 PM
Hi @ChuanYanF ,
try this
Script Include:
var DelegationUtil = Class.create();
DelegationUtil.prototype = {
initialize: function () {},
isUserOrDelegate: function (originalUserId) {
var currentUserId = gs.getUserID();
if (!originalUserId || currentUserId == originalUserId)
return true;
var gr = new GlideRecord('sys_user_delegate');
gr.addActiveQuery();
gr.addQuery('delegate', currentUserId);
gr.addQuery('user', originalUserId);
gr.addQuery('starts', '<=', gs.nowDateTime());
gr.addQuery('ends', '>=', gs.nowDateTime());
gr.query();
return gr.hasNext();
}
};
UI Action Condition
(gs.hasRole('sn_risk_advanced.Boost - BU HOD') ||
new global.DelegationUtil().isUserOrDelegate(current.u_submitted_by)) &&
current.state == 1 &&
!current.isNewRecord() &&
current.canRead() &&
current.u_submitted == true