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.

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

Kieran Anson
Kilo Patron

One option is a display rule to add a value to the scratchpad, and then an onLoad script.

(function executeRule(current, previous /*null when async*/ ) {

    var relatedIncidentCount = new global.GlideQuery('incident')
        .where('caused_by', current.getUniqueValue())
        .count();
    g_scratchpad.hasIncidentCausedByChange = Boolean(relatedIncidentCount);

})(current, previous);
function onLoad() {
   
   if(!g_user.hasRole('change_manager') || g_scratchpad.hasIncidentCausedByChange == false)
	g_form.removeOption('close_code' , 'option_value_here')
   
}