We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

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

Voona Rohila
Giga Patron

HI @Anupriya_11

Replace field_name with actual field name of the choice field.

 

 g_form.removeOption('field_name', 'Help'); //replace field_name with actual field name

 

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
5x ServiceNow MVP

Anand Kumar P
Tera Patron

hi @Anupriya_11 

 

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);
g_form.removeOption('actual_field_backendname' ,'Help');// Replace service with actual field backend name if its OOB Service field backend name is business_service if its custom then you have something like u_service , and Help with actual option value
 
}
}

 

 

If my response helped, please mark it as the accepted solution and give a thumbs up👍.
Thanks,
Anand

Ankur Bawiskar
Tera Patron

@Anupriya_11 

it's a reference field so you cannot use removeOption(). it's a sysId

Can you please confirm field from where you wish to remove is a choice or reference type?

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

Hi @Ankur Bawiskar  , Its a reference type.