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 04:49 AM
Hi @ChuanYanF
Try once like this
In the SI:
Ensure Client Callable is unchecked since this is meant for server-side usage.
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(); // Ensure only active records
gr.addQuery('delegate', currentUserId);
gr.addQuery('user', originalUserId);
gr.addQuery('starts', '<=', gs.nowDateTime());
gr.addQuery('ends', '>=', gs.nowDateTime());
gr.query();
if (!gr.next()) { // Explicitly check if delegation exists
return false;
}
return true;
}
};
Your Code: Checks if there are delegation records using gr.hasNext() but doesn’t explicitly verify if any delegation exists before returning true. If the delegate record is deleted, your script might still return true incorrectly.
Suggestion: Adds if (!gr.next()) { return false; } to ensure a valid record actually exists before granting permission.
Handle Deleted Delegation Records via Script Include Logic:
If a delegation record gets deleted, the button should disappear. You might need a check like this before returning gr.hasNext():
- Might work fine, but gs.nowDateTime() could cause unexpected issues depending on datetime storage format in the system. But gs.now() ensures consistent datetime comparisons.
gr.addQuery('starts', '<=', gs.now());
gr.addQuery('ends', '>=', gs.now());
Stay awesome,
Roshnee Dash
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2025 05:33 AM
HI Roshnee, may I know where is the client callable checkbox? I dont see it on my SI screen here.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2025 06:38 AM - edited 06-03-2025 06:50 AM
As you can see in the below image
It looks like the Client Callable checkbox has been renamed in instance, which is common in newer ServiceNow versions like Yokohama.
and also please recheck/compare the ui action condition provided by me or by @Ankur Bawiskar .
in your ui action condition you are not calling the function correctly
(gs.hasRole('sn_risk_advanced.Boost - BU HOD') || (new global.DelegationUtil().isUserOrDelegate)) && (current.state == 1) && (!current.isNewRecord()) && current.canRead() && current.u_submitted == true
new global.ScriptIncludeName().functionName(parameters)
inyour case () is missing while calling the funcion. Even if you are not sending any parameter you should use it.
Stay awesome,
Roshnee Dash
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2025 06:15 PM
Hi Roshnee, thanks for the clarification but I have also tried to implement the steps following u and ankur's guide but apparently it still does not work for me. May I know what is the underlying problem here?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2025 07:47 PM
Can you please share the screenshot of your ui action and si
Stay awesome,
Roshnee Dash