Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Hide UI Action button based on field value on Load after saving.

VullankiL
Tera Contributor

There is a ui action button Input Required (input_required) with some other conditions that needed. hence, they used onclick functions in script field.
Now my requirement is that I am using these Input Required button in change_request table.

It has one field requested_by (reference field). 
if the user in the requested_by field is part of Software Group, then i want to hide the input_required button. It have to work when "onLoad" not onChange. 
How can I achieve this using client script or Script include or both. what is the answer? 
Please help me with this.

6 REPLIES 6

Satishkumar B
Giga Sage
Giga Sage

Hi @VullankiL 

Script Include:

  • Make sure the Script Include is marked as Client Callable.
  • The isUserInSoftwareGroup method should accurately check the user's group membership.

 

 

 

var CheckSoftwareGroup = Class.create();
CheckSoftwareGroup.prototype = {
    initialize: function() {},
    
    isUserInSoftwareGroup: function(userId) {
        var userGroup = new GlideRecord('sys_user_grmember');
        userGroup.addQuery('user', userId);
        userGroup.addQuery('group.name', 'Software Group'); // Replace with the exact name
        userGroup.query();
        return userGroup.hasNext();
    }
};

 

 

Client Script:

 

  • Verify that requested_by correctly retrieves the user reference.
  • Ensure the GlideAjax call to the Script Include is correctly set up.
  • Confirm that the button ID used in g_form.getControl('input_required') is correct and corresponds to the UI action button.

 

 

function onLoad() {
    var requestedBy = g_form.getReference('requested_by', function(user) {
        if (user) {
            var ga = new GlideAjax('CheckSoftwareGroup');
            ga.addParam('sys_id', user.sys_id);
            ga.getXMLAnswer(function(response) {
                if (response === 'true') {
                    var button = g_form.getControl('input_required');
                    if (button) {
                        button.style.display = 'none';
                    }
                }
            });
        }
    });
}
​

 

……………………………………………………………………………………………………

Please Mark it helpful 👍and Accept Solution✔️!! If this helps you to understand. 

Still the input required button is not hiding when user in requested_by field is part of software group. i don't know where it is going wrong..