Make catalog variables editable for a particular group memebers

roomawakar
Tera Contributor

Hi All,

I have a requirement to make the RITM variables editable for a particular group members once the RITM is in 1st stage of approval.

 

for example: 1st stage of approval goes to 'Business Solution' group, so the members of that group should have access to edit the variables on the RITM form.

 

I have written a client callable script include to check the group membership. If it return true, variables should be editable over RITM form but the variables are not editable. 

 

Script Include:

 

var canEditRITMVariables = Class.create();

canEditRITMVariables.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    // Client-callable function

    canEditRITMVariables: function() {

        var userSysId = this.getParameter('sysparm_user');

        var groupSysId = this.getParameter('sysparm_group');

 

        if (this.isUserInGroup(userSysId, groupSysId)) {

            return 'true'; // user can edit

        }

        return 'false'; // restrict editing

    },

 

    // Helper: check group membership

    isUserInGroup: function(userSysId, groupSysId) {

        var grMember = new GlideRecord('sys_user_grmember');

        grMember.addQuery('user', userSysId);

        grMember.addQuery('group', groupSysId);

        grMember.query();

        return grMember.hasNext();

    },

 

    type: 'canEditRITMVariables'

});

 

 

 

Client Script:

 

function onLoad() {

    //Type appropriate comment here, and begin script below

 

    if (g_form.getValue("cat_item") == "d025b6ff870ce950fa770d47cebb3566") {

        var ga = new GlideAjax('canEditRITMVariables');

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

        ga.addParam('sysparm_user', g_user.userID);

        ga.addParam('sysparm_group', '56f1f8240f9d82004fb50f8ce1050e2f'); // replace with group sys_id 56f1f8240f9d82004fb50f8ce1050e2f

 

        ga.getXMLAnswer(function(response) {

            var canEdit = response === 'true';

            if (!canEdit) {

                g_form.setVariablesReadOnly(false);

            }

        });

    }

}

 

Can someone plz help on this why this code is not working. I have ran the code in the scripts-background and the result is coming as true still the variables are non-editable for the users.

 

Thansk,

 

10 REPLIES 10

kaushal_snow
Giga Sage

Hi @roomawakar ,

 

You can try out this approach..

 

1. Create a Before Display Business Rule on the RITM Table

Table: sc_req_item (or whichever RITM table you're using)

 

 

(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', gs.getUserID());
gr.addQuery('group', '56f1f8240f9d82004fb50f8ce1050e2f'); // Business Solution group Sys ID
gr.query();
g_scratchpad.canEditVars = gr.hasNext();
})(current, previous);

 


This checks if the current user is in the required group and invokes canEditVars in g_scratchpad.

 

2. Client Script (onLoad) to make Variable Read Only

Table: sc_req_item

 

 

function onLoad() {
if (g_scratchpad.canEditVars) {
// Loop through all variables and make them editable:
var vars = g_form.getEditableFields();
// If no helper exists, use the variable pool
for (var v in g_form.variable_pool) {
g_form.setReadOnly('variables.' + v, false);
}
}
}

 

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.