Hide Form Section Based on Users Role in Service Operations Workspace

Ajith A Pillai
Tera Contributor

Hi All,

 

I have a requirement that to hide the several form sections in Service Operations Workspace based on the user role.

For e.g., if a user has asset read-only role he/she should be able to view these sections only - asset, ODC, lab details.

If a user has asset role he/she should be able view and edit entire tabs which is available in hardware table. 

 

How to achieve this?

 

Thanks 

Ajith Pillai.

3 REPLIES 3

Saloni Suthar
Mega Sage
Mega Sage

Hi @Ajith A Pillai ,

 

You can create onload client script to hide the section based on the logged-in user's role.

 

https://www.servicenow.com/community/developer-forum/hiding-form-section-for-users-without-role-or-i...

 


If my response helped you, please click on "Accept as solution" and mark it as helpful.
- Saloni

Ankur Bawiskar
Tera Patron
Tera Patron

@Ajith A Pillai 

simply run your onLoad client script on workspace view and use this to check role and hide section

something like this but please enhance

function onLoad() {
    if (g_user.hasRoleExactly('role A')) {
        // hide sections
        g_form.setSectionDisplay('sectionname', false);
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Shivalika
Mega Sage

Hello @Ajith A Pillai 

 

Create a new On load Client scripts and in the view Field - write the backend end of service operations workspace view. 

 

In the script part add below script customised to your roles and section names. 

 

    // Define the roles for which sections should be hidden

    var restrictedRoles = ['itil', 'employee']; // Modify roles as needed

 

    // Define the sections (tabs) to hide

    var sectionsToHide = ['section_name_1', 'section_name_2']; // Update with actual section names

 

    // Check if the user has any of the restricted roles

    for (var i = 0; i < restrictedRoles.length; i++) {

        if (gUser.hasRole(restrictedRoles[i])) {

            hideSections(sectionsToHide);

            break; // Stop checking once a matching role is found

        }

    }

 

    function hideSections(sections) {

        sections.forEach(function(section) {

            gForm.setSectionDisplay(section, false);

        });

    }

 

})

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwN

eEISQCY