Hide choice options based on role in a field in related list view

janindiadoc_1
Tera Expert

Hi,

I have got a requirement to hide some of the options in the "Status" field of a table based on certain roles in backend form. I was able to achieve that by using an onload client script and hide the options based on the role. This table data is with the status field is shown as an entry in the related list view and there if I expand the dropdown options, all values are coming up.

 

Is there way to hide the multiple choice options from the field in the related list view as well?

 

Thanks

1 ACCEPTED SOLUTION

Hi @janindiadoc_1 There is no way to remove choices from list layout, what you can do is you can use onCellEdit client script like below

Create OnCellEdit Client Script:

select field - State

script:

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
var saveAndClose = true;

//In Place of "3" add your choice value


if ((newValue == 3) && (g_user.hasRole('admin'))) {
saveAndClose = true; // allow to select choice

} else {
alert('Only admin can select this choice');
saveAndClose = false; // donot allow to select choice
}

callback(saveAndClose);
}

Regards
Harish

View solution in original post

13 REPLIES 13

Hi @janindiadoc_1 I just tested g_form.addErrorMessage() donot work in onCellEdit client script due to limitation, the alternate is to use before update Business rule

Regards
Harish

Thanks Harish. It worked.

Hello @janindiadoc_1 

Can you please let me know how you achieved the requirement to hide the choices from the state field in list view. Can you please provide the code and what you created?

 

thanks,

Atul

Sumanth16
Kilo Patron

Hi @janindiadoc_1 ,

 

your script include should be client callable so that you can use Ajax

OR

you create display business rule and use this script

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var gr = new GlideRecord("RelatedListTable");
	gr.addQuery("u_number", current.sys_id); // give correct field name which refers the parent table
	gr.setLimit();
	gr.query();
	g_scratchpad.hasRecord = gr.hasNext();

})(current, previous);

onLoad client script

function onLoad(){
	if(g_scratchpad.hasRecord.toString() == 'false')
		g_form.removeOption('Choice Field Name', 'ChoiceOption');

 Please refer below thread:

https://www.servicenow.com/community/itsm-forum/remove-choice-list-option-based-on-related-list-on-f...

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda