Script Include assignment group based on department

Fotina Galieb
Tera Contributor

Hello All,

 

I am junior in development and I got stuck in one script include, my goal is to assign sc_req_item and sctask to the assignment group which is based on user's department. For example Dep1, dep2, dep3. And then only people from these concrete assignment groups can make updates on fields which were read-only for end user who created this request. For example user ordered a table, but only users from assignment groups can edit information what kind of table, color and material. I understand i have to assign ticket first, then check group members and then make these fields editable only for these people. But to put it into code and make it work became difficult. Could you please assist with your experience? Should I use properties instead of sys_ids of department? How do I add that field "color_table" can be edited by users from these groups? I plan to call this script include from UI policy.

Thank you so much in advance!!!

 

var AssignGroup = Class.create();
AssignGroup.prototype = {
    initialize: function() {
    },

   
    assignGroupBasedOnDep: function(reqItem, taskRecord) {
        var userDepartment = this.getUserDepartment();

        var assignmentGroup = '';

        // Assign to a group based on department
        switch (userDepartment) {
            case 'dep1':  
                assignmentGroup = '123456783j4f9def1234567890abcdef';
                break;
            case 'dep2':  
                assignmentGroup = 'a567891234567890abcdef1234567890';
                break;
            case 'dep3':  
                assignmentGroup = '7890abcdokd484567890abcdef123456';
                break;
            default:
                assignmentGroup = '';
        }

     
        if (assignmentGroup) {
            reqItem.assignment_group = assignmentGroup;
            reqItem.update();  

            taskRecord.assignment_group = assignmentGroup;
            taskRecord.update();  
        }
    },


    getUserDepartment: function() {
        var grUser = new GlideRecord('sys_user');
        if (grUser.get(gs.getUserID())) {
            var department = grUser.department;
            if (department) {
                return department.name;
            }
        }
        return '';
    },

    isUserMemberOfGroup: function(groupSysId) {
        var grUserGroup = new GlideRecord('sys_user_grmember');
        grUserGroup.addQuery('user', gs.getUserID());
        grUserGroup.addQuery('group', groupSysId);
        grUserGroup.query();
        return grUserGroup.next();
    },

    type: 'AssignGroup'
};
1 REPLY 1

Murthy Ch
Giga Sage

Hello @Fotina Galieb 

The fields which you're talking about are the variables of the catalog item?

If yes, you can use the below logic to make variables read-only (or) editable based on your logic:

g_form.setVariablesReadOnly(true); //it makes all variables read-only

 

Thanks,
Murthy