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 an option in reference field based on user role on record producer

Anupriya_11
Tera Contributor

Hi Team , I have a requirement to hide one option(ex: A)  in reference field based on user role, for non-ITIL users option should not be visible on the form. Could you please help me on how to achieve this. 

I have tried the on load script but its not working.

function onLoad() {
   //Type appropriate comment here, and begin script below
  var service= g_form.getValue('u_services');
  var role =g_user.hasRole('itil');
   //if (!g_user.hasRole('itil')){
    if(role == false){
    alert('service'+ service);
// Get the multiple choice field element (replace 'field_name' with the actual field name)
g_form.removeOption(service,'Help');
 
}
}
 
Thanks
11 REPLIES 11

@Anupriya_11 

then you should use advanced ref qualifier to filter it

something like this

javascript: var query = ''; if(!gs.hasRole('itil')) query = 'sys_idNOT INsysId1'; query;

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

@Ankur Bawiskar , i have checked  there is one reference qualifier already, could you please help , how can i add the above ref qualifier mentioned by you . PFB screenshot. Thanks

 

Anupriya_11_0-1734437728038.png

 

@Anupriya_11 

you have a script include function called

share that function code here and also the complete ref qualifier here and not screenshot

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

@Ankur Bawiskar , here is script include and ref Qualifier

 

Ref qualifier: javascript:'u_active=true^u_for_gefco=false^'+new incidentServerUtils().getService(current.variables.caller_id);

 

script Include:

var incidentServerUtils = Class.create();
incidentServerUtils.prototype = {
    getService: function(cur) {
        //  var GlideCatg = new GlideRecord('u_categories');
        //  GlideCatg.addQuery('u_assignment_group',)  

        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', cur);
        gr.query();
        if (gr.next()) {
            var assigneeLoc = gr.location.country;
         //   gs.log(assigneeLoc + "KV666");

        }
        if(assigneeLoc != ''){
        var GlideCatg = new GlideRecord('u_categories');
        GlideCatg.addQuery('u_location', assigneeLoc);
        GlideCatg.query();
        if (GlideCatg.next()) {


            return 'u_location=' + assigneeLoc + '^ORu_location=';
        }
else {
//         gs.log("TEST555");

return 'u_location=' + 'Global' + '^ORu_location=';
}
        }
            else {
            return 'u_location=' + 'Global' + '^ORu_location=';
        }
    },
Thanks


isPublic:function(){return true;},
    type: 'incidentServerUtils'
};

@Anupriya_11 

update as this

var incidentServerUtils = Class.create();
incidentServerUtils.prototype = {
    getService: function(cur) {
        //  var GlideCatg = new GlideRecord('u_categories');
        //  GlideCatg.addQuery('u_assignment_group',)  

        var query = '';

        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', cur);
        gr.query();
        if (gr.next()) {
            var assigneeLoc = gr.location.country;
        }
        if (assigneeLoc != '') {
            var GlideCatg = new GlideRecord('u_categories');
            GlideCatg.addQuery('u_location', assigneeLoc);
            GlideCatg.query();
            if (GlideCatg.next()) {
                query = 'u_location=' + assigneeLoc + '^ORu_location=';
            } else {
                query = 'u_location=' + 'Global' + '^ORu_location=';
            }
        } else {
            query = 'u_location=' + 'Global' + '^ORu_location=';
        }

        if (!gs.hasRole('itil'))
            query = query + 'sys_idNOT INsysId1';
        return query;
    },

    isPublic: function() {
        return true;
    },
    type: 'incidentServerUtils'
};

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