Make a section read-only for some users

hamidouche
Kilo Expert

Hi,

I have 10 sections inside a form that I want to make READONLY (I must not hide the section) for some users with a specific role only.

For example: I have UserA with role A: he can only edit section 1,2,3,4,5 but the rest will be readonly for him

But for UserB with roleB, he can edit sections 1,2,3  and the rest will be readonly, etc  you get the idea

I know I cannot use UI policy to do this for the whole section but for fields only. The thing is: the sections have over 100 fields each. 

Can I use ACL* for this? if yes how?

Can I use a client script. In this case I have to run g_form.setReadOnly() on all the fields.

 

Thanks

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

Hi,

Unfortunately, an ACL won't assist here. There isn't a way to easily make an entire section read-only.

You'd have to use a client script and grab their role using something like:

g_user.hasRole('itil') and go from there.

Keep in mind, any fields that are mandatory and are exposed on load, won't be able to be read-only (I believe).

So you'd have to balance the mandatory requirements as well. May take some work there.

So you'd want to check their role, setMandatory false, then readOnly true...etc.

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


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

View solution in original post

6 REPLIES 6

Prateek kumar
Mega Sage

ACL is the best way to do it.

But you would have to create as many ACL's depending on how many fields you have in that section.


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

Aitor Cabello
Tera Expert

Hi,

 

I’ve seen that if you create a client script, loop through all the variables in the section, and then set them to read-only, it seems to work.

 
Client script (onLoad):



    var ga = new GlideAjax('XXXXXXXX');
    ga.addParam('sysparm_name', 'getVariableSection');
    ga.getXML(setReadOnlyVarSection);

    function setReadOnlyVarSection(response) {

        //var variablesArray = JSON.parse(answer);

        var answer = response.responseXML.documentElement.getAttribute("answer");

        var variablesArray = JSON.parse(answer);

        g_form.addInfoMessage(answer);
        g_form.addInfoMessage(variablesArray);

        variablesArray.forEach(function(varName) {
            g_form.setReadOnly(varName, true);
        });
    }
 
Script include:
 
    getVariableSection: function() {

        var variables = [];

        var grSUE = new GlideRecord('sys_ui_element');
        grSUE.addEncodedQuery("sys_ui_section=XXXXXXXXXX^element!=.split^ORelement=NULL^element!=.end_split^ORelement=NULL^element!=.begin_split^ORelement=NULL");
        grSUE.query();

        while (grSUE.next()) {
            variables.push(grSUE.getValue('element'));
        }

        return JSON.stringify(variables);
    },
 
Thanks in advance.
 
Regards.