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.

How to hide dropdown options depending on condition

Dhanya4
Tera Contributor

Hi all,

I need to hide request state field options for non-admin users on conditions.

If request state not 'approved', hide 'approved' and 'rejected'.

If rrequest state is 'pending approval', hide 'close_complete'

 

Help needed with the scripting please.

Thanks

3 REPLIES 3

Harsh Vardhan
Giga Patron

you should try with onChange() client script on state field.

 

Sample script. make sure you have mentioned the correct state value .

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// 	if (isLoading || newValue === '') {
// 		return;
// 	}
	
	//Type appropriate comment here, and begin script below
	
	if(!(g_user.hasRole('admin')) && (g_form.getValue('state') != 'approved'))
		
	{
		
		g_form.addOption('state','close_complete','Close Complete');
		
		g_form.removeOption('state','approved');
		
		g_form.removeOption('state','rejected');
		
		
	}
	
	else if(!(g_user.hasRole('admin')) && (g_form.getValue('state') != 'pending approval'))
		
	{
		
		g_form.removeOption('state','close_complete');
		
		g_form.addOption('state','approved','Approved');
		
		g_form.addOption('state','rejected','Rejected');
		
	}
	
	
}

 

 

 

please refer the below doc links for more information about the glide form function and g_user

g_user function details

g_form function details

 

Hi Harshvardhan,

1.  I don't want anyone (except admin) to be able to change state at any stage ie:before 'onChange' also.(if the state is 'draft', approved and rejected should be hidden).  Should I use 'onLoad'?

2.  In the second half of the script, my requirement is when the state is 'pending approval', approved and rejected should be visible and close_complete  should be hidden.

else if(!(g_user.hasRole('admin')) && (g_form.getValue('state') != 'pending approval'))

When I changed the condition state=='pending approval', approved and rejected are still hidden.

Could you please help me getting it right.

Thank you