onload client script to check if the logged in user is par of which all groups

Community Alums
Not applicable

Hi Experts

 

My requirement is I need to hide few fields in the scoped application form extended on task table. 

the form is only accessible for the below group 

group1 'xyz' 

group2 'efg'

group3 'mnp'

group4'abc'

the form has three sections top section middle section and lower section 

the top section should be accessible for only for group1 

if the user is only part of group1 then all other sections should be readonly.

like wise the middle section should be accessible for only group2 

if the user is only part of group2 then all other sections should be readonly 

same with group3 and group4 

but if the user is part of all the group or part of two or three groups then relevant sections should be accessible 

for example if user is part of group1 and group2 then the user should acccess both top and middle vice versa

I tried with Glide Ajax in client script by calling the groups from script include

but it not working correctly.

the script include is not check for all the groups the user belongs for.  it comes out of the loop if any of the group it matches.   

My script include

var CheckuserGroups = Class.create();
CheckuserGroups.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getgroup1: function() {
        var usersysid = this.getParameter('sysparm_name_sysid'); // getting usersysid from client
        var groupsToCheck = ['6f0596dadbe8901004882b24ca9619ad', 'fe17dfb41b3d25509ebb64a2b24bcb08', '5a16e9331b3cf8d0e3cadd77b04bcb0a', 'c52f8ac5db0d9410b9e09334ca96195e'];

        for (var i = 0; i < groupsToCheck.length; i++) {
            var mem = new GlideRecord("sys_user_grmember");
            mem.addQuery('user', usersysid); // filtering current user
            mem.addQuery('group', groupsToCheck[i]);
            mem.query();

            while (mem.next()) {
                return mem.getValue('group'); // return the display value (group name)
            }
        }

        return null; // or any other value to indicate that the user is not a member of any of the specified groups
    },

    type: 'CheckuserGroups'
});
});

 

My client script

function onLoad() {
var sysid = g_user.userID; //get current user sysid
    var ga = new GlideAjax('CheckuserGroups'); //script include name
    ga.addParam('sysparm_name', 'gegroup1'); //function name
    ga.addParam('sysparm_name_sysid', sysid); //passing sysid to server
    ga.getXMLAnswer(getGroup1);
function getGroup1(response) {
        //set form1 fields as editable for members of WIT-IDT Support when task is work in progress
        if ((response == 'fe17dfb41b3d25509ebb64a2b24bcb08') && (g_form.getValue('state') == '610')||(g_form.getValue('state') == '620')||(g_form.getValue('state') == '630')) {
        g_form.setReadOnly('u_escalation_response', true);
        g_form.setReadOnly('u_response_reason', true);
        g_form.setReadOnly('u_resp_ext_reason', true);
        }
if ((response == '6f0596dadbe8901004882b24ca9619ad') && (g_form.getValue('state') == '610')){
        g_form.setReadOnly('u_response_reason', false);
        g_form.setReadOnly('u_resp_ext_reason', false);
        g_form.setReadOnly('u_escalation_response', false);
}
 if ((response == '6f0596dadbe8901004882b24ca9619ad') && (g_form.getValue('state') == '630'|| g_form.getValue('state') == '620')){
        g_form.setReadOnly('u_manager_review',false);
        g_form.setReadOnly('u_review_reason',false);
 }      
 if ((response == '5a16e9331b3cf8d0e3cadd77b04bcb0a') && (g_form.getValue('state') == '610')){
        g_form.setReadOnly('u_response_reason', false);
        g_form.setReadOnly('u_resp_ext_reason', false);
        g_form.setReadOnly('u_escalation_response', false);
}
if ((response == '5a16e9331b3cf8d0e3cadd77b04bcb0a') && (g_form.getValue('state') == '630'|| g_form.getValue('state') == '620')){
        g_form.setReadOnly('u_manager_review',false);
        g_form.setReadOnly('u_review_reason',false);
 }
 if ((response == 'c52f8ac5db0d9410b9e09334ca96195e') && (g_form.getValue('state') == '610')){
        g_form.setReadOnly('u_response_reason', true);
        g_form.setReadOnly('u_resp_ext_reason', true);
        g_form.setReadOnly('u_escalation_response', true);
}
if ((response == 'c52f8ac5db0d9410b9e09334ca96195e') && (g_form.getValue('state') == '630'|| g_form.getValue('state') == '620')){
        g_form.setReadOnly('u_manager_review',false);
        g_form.setReadOnly('u_review_reason',false);
 }      



        }
   
}

 

 

 

 

 

4 REPLIES 4

The Machine
Kilo Sage

I'd try creating a new view and a view rule.  In your view rule you can use your script to check which group and then if a member, force that view.  Lastly, you can apply UI Policies / Client Scripts at the view level, so you can make your fields read only for that view specifically if that is your goal.

Good luck.

Community Alums
Not applicable

Hi Guru,

Thank you for the response. I did try exploring the view rules but unfortunately it will only allow to hide the fields not make read only.  So this option is not working for my issue. I did try to call the array from the script include but not sure how to check the array response from the client script.  I did try calling response from script include by using the operators contains or includes but nothing is working

  

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @Community Alums 

It seems like the issue with your script is that it's returning from the function as soon as it finds a match in the loop. This behavior causes it to only check the first group and then exit the loop. To address this, you can modify your script to check all groups and then decide the appropriate action based on the user's membership in each group.

Here's the modified script include code:

 

 

var CheckuserGroups = Class.create();
CheckuserGroups.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getGroups: function() {
        var usersysid = this.getParameter('sysparm_name_sysid'); 
        var groupsToCheck = ['6f0596dadbe8901004882b24ca9619ad', 'fe17dfb41b3d25509ebb64a2b24bcb08', '5a16e9331b3cf8d0e3cadd77b04bcb0a', 'c52f8ac5db0d9410b9e09334ca96195e'];
        var userGroups = [];

        for (var i = 0; i < groupsToCheck.length; i++) {
            var mem = new GlideRecord("sys_user_grmember");
            mem.addQuery('user', usersysid); 
            mem.addQuery('group', groupsToCheck[i]);
            mem.query();

            while (mem.next()) {
                userGroups.push(mem.getValue('group')); 
            }
        }

        return userGroups;
    },

    type: 'CheckuserGroups'
});

 

 

 

 

Now, the getGroups function will return an array containing all the groups the user belongs to. With this modification, you can adapt your client script accordingly to handle all scenarios. Here's an updated version of your client script:

 

 

 

 

function onLoad() {
    var sysid = g_user.userID; 
    var ga = new GlideAjax('CheckuserGroups'); 
    ga.addParam('sysparm_name', 'getGroups'); 
    ga.addParam('sysparm_name_sysid', sysid); 
    ga.getXMLAnswer(getUserGroups);

    function getUserGroups(response) {
        var userGroups = response.split(',');
        var currentState = g_form.getValue('state');


        if (userGroups.includes('fe17dfb41b3d25509ebb64a2b24bcb08')) {
            // Group1 - xyz
            if (currentState == '610' || currentState == '620' || currentState == '630') {
                g_form.setReadOnly('u_escalation_response', true);
                g_form.setReadOnly('u_response_reason', true);
                g_form.setReadOnly('u_resp_ext_reason', true);
            }
        }
        if (userGroups.includes('6f0596dadbe8901004882b24ca9619ad')) {
            // Group2 - efg
            if (currentState == '610') {
                g_form.setReadOnly('u_response_reason', false);
                g_form.setReadOnly('u_resp_ext_reason', false);
                g_form.setReadOnly('u_escalation_response', false);
            }
            if (currentState == '620' || currentState == '630') {
                g_form.setReadOnly('u_manager_review', false);
                g_form.setReadOnly('u_review_reason', false);
            }
        }
        if (userGroups.includes('5a16e9331b3cf8d0e3cadd77b04bcb0a')) {
            // Group3 - mnp
            if (currentState == '610' || currentState == '620' || currentState == '630') {
                g_form.setReadOnly('u_response_reason', false);
                g_form.setReadOnly('u_resp_ext_reason', false);
                g_form.setReadOnly('u_escalation_response', false);
            }
        }
        if (userGroups.includes('c52f8ac5db0d9410b9e09334ca96195e')) {
            // Group4 - abc
            if (currentState == '610') {
                g_form.setReadOnly('u_response_reason', true);
                g_form.setReadOnly('u_resp_ext_reason', true);
                g_form.setReadOnly('u_escalation_response', true);
            }
            if (currentState == '620' || currentState == '630') {
                g_form.setReadOnly('u_manager_review', false);
                g_form.setReadOnly('u_review_reason', false);
            }
        }
    }
}

 

Please mark as helpful & accepted solution if it suffice

 

BR, Sohith

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

Community Alums
Not applicable

Hi Sohithanjan G

Thank you for sharing the script. not sure but the includes method is not working in the client script at my end.  

The below script is working at my end 

MyScriptInclude:

 

// Set the prototype on the global scope

//global.MyScriptInclude = Class.create();

//MyScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {

var MyScriptInclude = Class.create();

MyScriptInclude.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {  

 

    checkUserGroups: function() {

        var userId = this.getParameter('sysparm_user_id');

        var group1 = ['fddfdfdsfdsfdsfdsfdsfdsfdsfdsfsf'];

        var group2 = ['fsdfdsedsfefsdfwesdfefefwefefeww'];

        var group3 = ['sdgdsfdsffsdfsdfsdfsfsdfsdfsdfff'];

        var group4 = ['sdsefsefweweewwefewrwerweewewrew'];

        var resultGroup1 = this.isUserInGroupArray(userId, group1);

        var resultGroup2 = this.isUserInGroupArray(userId, group2);

        var resultGroup3 = this.isUserInGroupArray(userId, group3);

        var resultGroup4 = this.isUserInGroupArray(userId, group4);  

        var result = '';

 

        if (resultGroup1) {

            result += 'Group1';

        }

 

        if (resultGroup2) {

            result += 'Group2';

        }

 

        if (resultGroup3) {

            result += 'Group3';

        }

        if (resultGroup4) {

            result += 'Group4';

        }

 

        gs.info('Debug: ResultGroup1: ' + resultGroup1);

        gs.info('Debug: ResultGroup2: ' + resultGroup2);

        gs.info('Debug: ResultGroup3: ' + resultGroup3);

        gs.info('Debug: ResultGroup4: ' + resultGroup4);

 

        return result;

    },

 

    isUserInGroupArray: function(userId, groupArray) {

        gs.info('Debug: Checking user ' + userId + ' in groups ' + JSON.stringify(groupArray));

 

        var grMember = new GlideRecord('sys_user_grmember');

        grMember.addQuery('user', userId);

        grMember.addQuery('group', 'IN', groupArray);

        grMember.query();

 

        gs.info('Debug: Query executed. Records found: ' + grMember.getRowCount());

        return grMember.hasNext();

    }

});

Clientscriptnew:

// OnLoad Client Script

 

function onLoad() {

var userId = g_user.userID;

    // Call the server script include using GlideAjax

    var ga = new GlideAjax('MyScriptInclude'); // Replace 'MyScriptInclude' with the actual name of your script include

    ga.addParam('sysparm_name', 'checkUserGroups');

    ga.addParam('sysparm_user_id', userId);

    ga.getXMLAnswer(onSuccess); // Assuming the server script returns a value

 

    function onSuccess(response) {

//alert('User Groups: ' + response);

if ((response == 'Group1')  && (g_form.getValue('state') == '610')

        ||(g_form.getValue('state') == '620')||(g_form.getValue('state') == '630')) {

        g_form.setReadOnly('u_escalation_response', true);

        g_form.setReadOnly('u_response_reason', true);

        g_form.setReadOnly('u_resp_ext_reason', true);

        g_form.setReadOnly('u_manager_review',true);

        g_form.setReadOnly('u_review_reason',true);

        }