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

Sure here is the SI screenshot with the script

ChuanYanF_0-1749006160792.png

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

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.

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, 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?

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.

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

Ankur Bawiskar
Tera Patron
Tera Patron

@ChuanYanF 

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.

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