Hiding drop down field selection

lonesoac01
Giga Guru

Hello all,

 

   I am interested in hiding a specific value in the close_code dropdown field on the change_request form.  The conditions are:

  1. Must have a related INC in the "Incidents Caused By Change" related list.
  2. If the user has the change_manager role then they can see the drop down value.

I have tried Client Scripts and UI Policies to no avail.  Anyone else have any other ideas?

 

Thank you.

1 ACCEPTED SOLUTION

@lonesoac01thanks, I modified the client script slightly (removed all the logs and error handling btw).

function onLoad() {
    var currentUser = g_user.userID;
    var currentSysId = g_form.getUniqueValue();
    var ga = new GlideAjax('global.CheckRelatedRecordAndGroup');
    ga.addParam('sysparm_name', 'getRelatedINCs');
    ga.addParam('ChangeSYSID', currentSysId);
    ga.addParam('UserSYSID', currentUser);
    ga.getXMLAnswer(handleResponse);

    function handleResponse(response) {
        try {
            var res = JSON.parse(response);
            // Check if there is a related incident record and if the user is not a change manager
            if (res.RelatedINCRecord === true && res.UsrIsChgMgr === false) {
                // Hide specific drop-down option in the close_code field
                hideCloseCodeOption('successful');
            } else {}
        } catch (e) {}
    }

    function hideCloseCodeOption(optionValue) {
        g_form.removeOption('close_code', optionValue);
    }
}

JamesChun_0-1717532638259.png

 

FYI, you might want to use a sys_id instead of the name when querying the group.

 

Cheers

View solution in original post

5 REPLIES 5

James Chun
Kilo Patron

Hi @lonesoac01,

 

You should be able to achieve this via a Client Script. 

Can you share what you have tried?

 

Cheers

lonesoac01
Giga Guru

Client Script:

function onLoad() {
    var currentUser = g_user.userID;
    var currentSysId = g_form.getUniqueValue();
    var ga = new GlideAjax('global.CheckRelatedRecordAndGroup');
    ga.addParam('sysparm_name', 'getRelatedINCs');
    ga.addParam('ChangeSYSID', currentSysId);
    ga.addParam('UserSYSID', currentUser);
    ga.getXMLAnswer(handleResponse);

    function handleResponse(response) {
        try {
            var res = JSON.parse(response);

            console.log('Response received:', res);

            // Check if there is a related incident record and if the user is not a change manager
            if (res.RelatedINCRecord === 'true' && res.UsrIsChgMgr === 'false') {
                console.log('Conditions met, hiding the specific option.');
                // Hide specific drop-down option in the close_code field
                hideCloseCodeOption('abc');
                alert('The selection should not be there!');
            } else {
                console.log('Conditions not met.');
                alert('bleh');
            }
        } catch (e) {
            console.error('Error parsing response: ', e);
        }
    }

    function hideCloseCodeOption(optionValue) {
        var field = g_form.getControl('close_code');
        if (field) {
            var $field = $(field);
            var $options = $field.find('option');
            console.log('Field options:', $options);
            var optionFound = false;
            $options.each(function() {
                console.log('Checking option:', this.value);
                if (this.value === optionValue) {
                    optionFound = true;
                    console.log('Found option to hide:', this.value);
                    $(this).hide();
                    console.log('Option hidden:', optionValue);
                }
            });
            if (!optionFound) {
                console.log('Option not found:', optionValue);
            }
        } else {
            console.error('Field close_code not found');
        }
    }
}

 

Script Include:

var CheckRelatedRecordAndGroup = Class.create();
CheckRelatedRecordAndGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getRelatedINCs: function() {
        var result = {
            RelatedINCRecord: false,
            UsrIsChgMgr: false
        };

        var changeRequestSysId = this.getParameter('ChangeSYSID');
        var userSysId = this.getParameter('UserSYSID');

        // Check for related incident records
        var incidentGR = new GlideRecord('incident');
        incidentGR.addQuery('caused_by', changeRequestSysId);
        incidentGR.setLimit(1); // Limit to one record for efficiency
        incidentGR.query();
        if (incidentGR.next()) {
            result.RelatedINCRecord = true;
        }

        // Check if user is part of 'Change Management' group
        var groupMemberGR = new GlideRecord('sys_user_grmember');
        groupMemberGR.addQuery('group.name', 'Change Management');
        groupMemberGR.addQuery('user', userSysId);
        groupMemberGR.setLimit(1); // Limit to one record for efficiency
        groupMemberGR.query();
        if (groupMemberGR.next()) {
            result.UsrIsChgMgr = true;
        }

        return JSON.stringify(result);
    },

    type: 'CheckRelatedRecordAndGroup'
});

@lonesoac01thanks, I modified the client script slightly (removed all the logs and error handling btw).

function onLoad() {
    var currentUser = g_user.userID;
    var currentSysId = g_form.getUniqueValue();
    var ga = new GlideAjax('global.CheckRelatedRecordAndGroup');
    ga.addParam('sysparm_name', 'getRelatedINCs');
    ga.addParam('ChangeSYSID', currentSysId);
    ga.addParam('UserSYSID', currentUser);
    ga.getXMLAnswer(handleResponse);

    function handleResponse(response) {
        try {
            var res = JSON.parse(response);
            // Check if there is a related incident record and if the user is not a change manager
            if (res.RelatedINCRecord === true && res.UsrIsChgMgr === false) {
                // Hide specific drop-down option in the close_code field
                hideCloseCodeOption('successful');
            } else {}
        } catch (e) {}
    }

    function hideCloseCodeOption(optionValue) {
        g_form.removeOption('close_code', optionValue);
    }
}

JamesChun_0-1717532638259.png

 

FYI, you might want to use a sys_id instead of the name when querying the group.

 

Cheers

I implemented the code you gave me and it worked like a charm!