I want make multiple fields read only for multiple user groups

pvv1045330
Tera Contributor

Hi All,

 

I want make multiple fields read-only for multiple groups on change table, when change state is all except new state.

 

How can I make fields read only and what is best practice.

 

Thanks,   

7 REPLIES 7

Dr Atul G- LNG
Tera Patron
Tera Patron

 Hi @pvv1045330 

 

A similar issue

 

https://www.servicenow.com/community/developer-forum/how-to-make-several-fields-read-only-when-state...

 

https://www.servicenow.com/community/developer-forum/field-is-read-only-for-everyone-except-2-groups...

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Amit Gujarathi
Giga Sage
Giga Sage

HI @pvv1045330 ,
I trust you are doing great.
Please find the below client script for the same :

// Client Script to make fields read-only
(function onLoad() {
    // Check if the user is in the required groups
    if (isUserInGroups(['change_manager', 'itil'])) {
        // Check if the state is not 'New'
        var changeState = g_form.getValue('state');
        if (changeState !== 'new') {
            // Make fields read-only
            g_form.setReadOnly('description', true);
            g_form.setReadOnly('category', true);
        }
    }
})();

// Function to check if the user is in any of the specified groups
function isUserInGroups(groups) {
    for (var i = 0; i < groups.length; i++) {
        if (g_user.hasRole(groups[i])) {
            return true;
        }
    }
    return false;
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



AndersBGS
Tera Patron
Tera Patron

Hi @pvv1045330 ,

 

A start recommendation would be to start define:

 

  • When should fields be read-only? Under which circumstances, which groups/users etc.
  • When should fields NOT be read-only? Under which circumstances, which groups/users etc.

Based on if you provide specific descriptions and questions to above, it will be much easier to help you.

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

Best regards

Anders  

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

Hi @AndersBGS ,

 

  • When should fields be read-only? Under which circumstances, which groups/users etc.

            a) when change state is assess, authorize, scheduled, implementation, review, closed, canceled.

                 all fields should be read only except Work notes, Assignment group, Assign to etc.

Group names: service desk, IT support group, etc.

 

  • When should fields NOT be read-only? Under which circumstances, which groups/users etc.

          Change state is New all fields should editable form all user.

 

Thanks,