How to add a check delegate is true condition to an UI Action?

ChuanYanF
Tera Guru

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();
    }
};
UI Action Condition:
(gs.hasRole('sn_risk_advanced.Boost - BU HOD') ||  (new  global.DelegationUtil().isUserOrDelegate)) && (current.state == 1) && (!current.isNewRecord()) && current.canRead() && current.u_submitted == true​
19 REPLIES 19

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());

 

 

Your feedback makes the community stronger! If you found this helpful, marking it as the correct answer helps others.
Stay awesome,
Roshnee Dash

HI Roshnee, may I know where is the client callable checkbox? I dont see it on my SI screen here.

ChuanYanF_0-1748954025101.png

 

As you can see in the below image

RoshneeDash_0-1748957646800.png

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.

Your feedback makes the community stronger! If you found this helpful, marking it as the correct answer helps others.
Stay awesome,
Roshnee Dash

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? 

Can you please share the screenshot of your ui action and si

Your feedback makes the community stronger! If you found this helpful, marking it as the correct answer helps others.
Stay awesome,
Roshnee Dash