Make Assignment read only based on the form selected

DSV22
Tera Contributor

Hi

 

Want to make the Assignment group read only when a catalog item selected and for admin it should be editable

 

can anyone help me with this 

11 REPLIES 11

Sandeep Rajput
Tera Patron
Tera Patron

@DSV22 On your Assignment Group variable, in the permissions tab, simply add admin role in the write and create roles. This field will be non editable for admins.

 

Screenshot 2024-09-13 at 5.17.18 PM.png

Please mark my response helpful and accepted solution if it addresses your question.

Badrinarayan
Tera Guru

Hi @DSV22 ,

 

You want to ensure that:

  • The "Assignment group" field on a catalog item form is read-only for users who are not admins.
  • The "Assignment group" field is editable for users with the admin role.

Steps to Implement:

  1. Ensure the Assignment Group Field is Not Mandatory:

    • First, make sure that the "Assignment group" field is not set as mandatory if it's not supposed to be required for everyone.
  2. Create a Catalog Client Script:

    • You need to create a Catalog Client Script that will handle the visibility and editability of the "Assignment group" field based on the user’s role.
  3. Write the Client Script:

    • Go to ServiceNow and navigate to the catalog item where you want to apply this script.
    • Create a new Catalog Client Script and use the following code:
     
    Screenshot 2024-09-13 at 5.16.57 PM.png

 

function onLoad() {
   //Type appropriate comment here, and begin script below
   if(g_user.hasRole('admin'))
   {
	g_form.setReadOnly('assignment_group',false);  //assignment_group is the Backend Value of Variable Assignment Group
	alert('true');
   }
   else{
	g_form.setReadOnly('assignment_group',true);
	alert('you are not a admin');
   }
}​

 

  • Explanation:
    • g_user.hasRole('admin'): Checks if the current user has the admin role.
    • g_form.setReadOnly('assignment_group', false): Makes the "Assignment group" field editable.
    • g_form.setReadOnly('assignment_group', true): Makes the "Assignment group" field read-only.
    • Alerts: Provide a visual confirmation of whether the field is editable or read-only based on the user’s role.
       
  • Test the Script:

Make sure to test the script by logging in with different user roles (admin and non-admin) to confirm that the "Assignment group" field behaves as expected.

 

It is Working Fine with my PDI , 

 

 

Please Accept the Solution , and Mark it as Helpful  ,

 

Thanks Regards ,

Badrinarayan