Client script to make fields read only if user belongs to assignment group.

Balaram7
Kilo Sage

Hi all,

 

We have a requirement to make all fields read only on incident except State and Worknotes.

This should happen when user from the current assignment group opens the form.

if Assigned user is the current logged in user, then need to make state and worknotes only editable on the form. Remaining fields like contact type, email, category and sub category, ci, service needs to be read only.

Please help me with the client script to acheive this.

 

 

 

Thank you,

Balaram.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Balaram 

You can use client script for this

function onLoad(){

	if(g_user.userID == g_form.getValue('assigned_to')){
		// make all fields readonly
		var fields = g_form.getEditableFields();
		for (var x = 0; x < fields.length; x++) {
			g_form.setReadOnly(fields[x], true);
		}

		// make state and work notes editable
		g_form.setReadOnly('state', false);
		g_form.setReadOnly('work_notes', false);
	}
}

Regards
Ankur

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

View solution in original post

11 REPLIES 11

Allen Andreas
Administrator
Administrator

Hello,

This is best handled by using ACLs versus client scripts.

You'd want to review your "write" ACLs for the incident table and see what is allowing them to be able to write today and then consider changing that so not everyone can write to it freely. Then, you'll want to work on field specific ACLs to only allow them to write to them if they're a member of the assignment group and only for 'x' fields (not all).

Here's documentation on how to create ACLs: https://docs.servicenow.com/bundle/paris-platform-administration/page/administer/contextual-security...

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


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

sachin_namjoshi
Kilo Patron
Kilo Patron

 

You will need client script and business to make fields read only based on group membership

Please follow the below steps AS EXAMPLE to get this configured:

 

 

1) Write a Display Business Rule on the Incident Table as shown below:

 

 

 

find_real_file.png

 

 

 

find_real_file.png

 

 

 

In the above Screen shot Replace the Group "CAB Approval" with your Group Name

 

 

 

2) Once the Business Rule has been configured Write an On Load Client Script On the Incident Table to make the field Mandate if the Logged in User is a part of the required Group as shown below:

 

 

 

find_real_file.png

 

 

 

In the Above screen shot Replace "description" with your required field Names.

 

 

Regards,

Sachin

I am trying to complete similar requirements. I need to mark 'state' and 'assignment group' as read only if the logged in user has the Mule-Hide Regional Directors group. I tried to accomplish this using the information you provided. I am wondering what I am doing wrong that would make it not work? Thank you

 

When to run is set as 'display'

melsarrazin_0-1732306325113.png

melsarrazin_1-1732306652609.png

 

Hi,

Can you add logging to your business rule to check that value there?

Is the group name spelled correctly as verified within the instance?


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