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 08:04 PM
Sure here is the SI screenshot with the script
var DelegationUtil = Class.create();
DelegationUtil.prototype = {
initialize: function() {},
isUserOrDelegate: function(originalUserId) {
if (!originalUserId) return false; // Handle null/undefined
var currentUserId = gs.getUserID();
if (currentUserId == originalUserId)
return true;
var gr = new GlideRecord('sys_user_delegate');
gr.addQuery('delegate', currentUserId);
gr.addQuery('user', originalUserId);
gr.addQuery('starts', '<=', gs.nowDateTime());
gr.addQuery('ends', '>=', gs.nowDateTime());
gr.query();
return gr.hasNext();
}
};
This is the Ui Action Condition and its script
Condition:
(gs.hasRole('sn_risk_advanced.Boost - BU HOD') || (new global.DelegationUtil().isUserOrDelegate(current.assigned_to))) && (current.state == 1) && (!current.isNewRecord()) && current.canRead() && current.u_submitted == true
Action Script:
validateFields();
function validateFields() {
if (gs.nil(current.assigned_to) && gs.nil(current.assignment_group)) {
gs.addErrorMessage(gs.getMessage("Owner/Owning group is required to analyze risk events."));
current.setAbortAction(true);
} else {
current.state = 2;
current.substate = "";
current.update();
action.setRedirectURL(current);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2025 01:33 AM - edited 06-04-2025 01:44 AM
As I can see in your si GlideAjax anablled is not checked also.
To debug the issue, you can add an alert() in the Client Script and a gs.info() in the Script Include to verify whether the code execution is reaching those points. This will help determine if the logic is being triggered correctly.
If the UI Action is not executing as expected, try breaking it down into individual conditions and test each one separately. You can do this by adding alerts to check the values returned at different stages, ensuring you identify where the problem lies.
Stay awesome,
Roshnee Dash
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2025 02:06 AM
Hi Roshnee, from the previous replies I can see that you asked me to ensure that this script include is not client callable, that is why I unchecked the glideajax field. So am I supposed to enable it?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2025 02:53 AM
Hi @ChuanYanF ,
Sorry my sentence give other meaning to you actually I meant to ask check either that is checked or not. It should be checked.
Stay awesome,
Roshnee Dash
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2025 05:53 AM
you are not passing the user sysId to that script include function
For example: I am sending current.assigned_to, you can send your field value
(gs.hasRole('sn_risk_advanced.Boost - BU HOD') ||
(new global.DelegationUtil().isUserOrDelegate(current.assigned_to))) &&
(current.state == 1) &&
(!current.isNewRecord()) &&
current.canRead() &&
current.u_submitted == true
Script Include is corrected, active field is not there on delegate table
var DelegationUtil = Class.create();
DelegationUtil.prototype = {
initialize: function() {},
isUserOrDelegate: function(originalUserId) {
if (!originalUserId) return false; // Handle null/undefined
var currentUserId = gs.getUserID();
if (currentUserId == originalUserId)
return true;
var gr = new GlideRecord('sys_user_delegate');
gr.addQuery('delegate', currentUserId);
gr.addQuery('user', originalUserId);
gr.addQuery('starts', '<=', gs.nowDateTime());
gr.addQuery('ends', '>=', gs.nowDateTime());
gr.query();
return gr.hasNext();
}
};
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