Hide choices in record producer varible based system loged in user's company

attanhes
Tera Guru

Hi,
I have a requirement to limit choices on record producer variable based on system lodged in user's company. So I have created a catalog client script for that, but i could not find better way to showoption method in client script. Hence I used g_form.removeOption but this is not practical as this is has lots of choices in this field and also list is growing. Can someone please help me out on below coding instead of g_form.removeOption?

find_real_file.png

function onLoad() {
var user = new GlideRecord('sys_user');
user.addQuery('sys_id', "=", g_user.userID);
user.query();
	// Customer A only should see Finance and Software
if(user.next() &&  user.getValue('company')=='323502194f9542008e8472a6f310c724'){
	g_form.removeOption('u_customer_service_selection','Data');
	g_form.removeOption('u_customer_service_selection','Email');
	g_form.removeOption('u_customer_service_selection','Maps');
	g_form.removeOption('u_customer_service_selection','Reporting');
	
	
}	
	// Customer B only should see data, Finance, Maps
	else if (user.getValue('company')=='18254a554f9542008e8472a6f310c7f5'){
	g_form.removeOption('u_customer_service_selection','Email');
	g_form.removeOption('u_customer_service_selection','Reporting');
	g_form.removeOption('u_customer_service_selection','Software');

	
}	
}
1 ACCEPTED SOLUTION

Hi,

I'd recommend using:

g_form.clearOptions('field_name');

This way they're all removed in one shot and you simply populate only what you need.

g_form.addOption(<fieldName>, <choiceValue>, <choiceLabel>, <targetIndex>);

See this link for some assistance: https://servicenowguru.com/scripting/client-scripts-scripting/removing-disabling-choice-list-options...

Either way, you're adding or removing choices from the select-box dynamically based on their company.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

4 REPLIES 4

Allen Andreas
Administrator
Administrator

Hi,

For starters, you don't want to use GlideRecord in client script as that is not best practice.

Ideally, you'd use GlideAjax to query the server and retrieve that information and bring that back to the client. Here's a cheat sheet to assist you: https://community.servicenow.com/community?id=community_article&sys_id=9f7ce2e1dbd0dbc01dcaf3231f961...

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Thanks Allen. I could use GlideAjax to get user details.

Is there any other method that i can use instead of g_form.removeOption to limit the choices?

 

 

Hi,

I'd recommend using:

g_form.clearOptions('field_name');

This way they're all removed in one shot and you simply populate only what you need.

g_form.addOption(<fieldName>, <choiceValue>, <choiceLabel>, <targetIndex>);

See this link for some assistance: https://servicenowguru.com/scripting/client-scripts-scripting/removing-disabling-choice-list-options...

Either way, you're adding or removing choices from the select-box dynamically based on their company.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Thanks for your quick response. I will try this.