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 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.

@ChuanYanF 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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?

ChuanYanF_0-1749087031455.png

 

@ChuanYanF 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Community Alums
Not applicable

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