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.

Restrict Choice List Options based on Group

Sam Ogden
Tera Guru

Hi All,

 

We have a choice list field on the incident form of 'pending state'  this has 5 options.  We want to add a 6th option to the list but we only want our SM Release team to be able to select this option, how can we achieve this ?

Thanks

Sam

1 ACCEPTED SOLUTION

Tushar Sharma2
Kilo Guru

Hey Sam,

Good News for you. I worked on your code and was able to restrict the choice.

1. Add below code in UI HTML Page.

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

<script>
addLoadEvent( function()
{
  onLoad();
});

</script>
	
<g:evaluate>
 var types = GlideScriptChoiceList.getChoiceList('incident', 'u_pending_type');
</g:evaluate>
	<style>
		.hide_this
		{
		display:none;
		}
	</style>
	
	<div style="padding-bottom: 10px;">
	<span style="float: left;padding-left: 10px;">Pending Type : </span>

		<select id="pendingType" style="margin-left: 20px;" onChange = "show_3rdparty_fields()">
			<j:forEach var="jvar_choice" items="${types}">                   
			   <option value="${jvar_choice.value}">${jvar_choice.label}</option>
			 </j:forEach>
		</select><br/> 
	<br/>
	<div class ="hide_this" id = 'partysupplier'>
	<span style="float: left;padding-left: 10px;">3rd Party Supplier : </span>
	<g:ui_reference name="supplier" id="supplier" table="core_company" query="manufacturer=true^ORvendor=true"/>
	</div>
	<br/>
	<div class='hide_this' id = 'partyreference'><span style="float: left;padding-left: 10px;">3rd Party Reference : </span>
	<input type = "text" id = 'pref' style ="padding-right: 5px; width: 55%;"/></div>
</div>

	<div style="padding-left: 10px;">
		<span>Additional Comments (Customer Visible)</span>
   <div>
			<textarea rows="4" cols="50" id="comments" style="margin-top: 5px;">
			</textarea>
	</div>
	</div>	
	<div style="padding-left: 10px;">
		
		<input type="button" value="Submit" onClick="return submit()" style="float:left;margin-right: 15px;"/>
		<input type="button" value="Cancel" onClick="return cancel()"/>
	</div>
</j:jelly>

 

2.Add below onLoad function into UI Page Client Script to restrict choice list options based on role.

function onLoad()
 {
   //Type appropriate comment here, and begin script below
	//Remove option of "Pending Release Baord" from anyone except Release Management
      var isRelMan = g_user.hasRole('release_admin');
      if (!isRelMan)
      {
       var opt=gel("pendingType");
       opt.options[6].disabled = true;
      }
      else
      {
        return;
      }
}



function submit()
{
	var pendingType = gel('pendingType').value;
	var comments = gel('comments').value;
	
	var thirdsupplier = $j('#supplier').val();
	var thirdreference = $j('#pref').val();
	if(pendingType != '' && comments.trim() != '')
		{
		g_form.setValue('u_pending_type',pendingType);
		g_form.setValue('comments',comments);
		g_form.setValue('incident.correlation_display',thirdsupplier);
		g_form.setValue('incident.correlation_id',thirdreference);
		gsftSubmit(null, g_form.getFormElement(), 'setpending');
	}
	else
		{
		alert('Please fill the details');
	}
}

function show_3rdparty_fields()
{
	var pending_type = $j("#pendingType option:selected").val();
	
	if(pending_type == 'Pending Other Party')
		{
		$j('#partysupplier').show();
		$j('#partyreference').show();
	}
	else{
		$j('#partysupplier').hide();
		$j('#partyreference').hide();
	}
	
}


function cancel()
{
	g_form.setValue('u_pending_type','');
	g_form.setValue('comments','');
	GlideDialogWindow.get().destroy();
}

 

Let me know if this works for you.

 

Hit Like or Correct on the impact of response.

-Tushar

 

View solution in original post

16 REPLIES 16

Shweta KHAJAPUR
Tera Guru

Hi,

To hide and show option there is a method by name removeOption(). This method will hide options based on your condition.

 

Regards,

Shweta K

Hi Shweta,

 

Thanks for the above.  How would I best incorporate this? would it be best as an on load client script on the form to check if the user is a member of a certain group, if not then remove option?

Thanks

Sam

Yes, it is the best.

Hi,

Yes, use above code in onLoad() client script.

 

Regards,

Shweta K