Assignment Group Can Only be used by Those in the Group

Wasdom_Kung
Tera Guru

Hello, 

 

I have been asked to 'lock down' an assignment group, this group can be viewed from the assignment group field on the incident form (We already have this set to only display specific groups that match 'types' set in the dictionary).

I am looking to implement the following: If someone is in the group (say Group 1) then check if this user is also in that group on submission of the incident, if not clear the field and display an error. If they are in the group on submission, keep populated and save incident. 

 

How could I achieve this? Or if someone has done something similar, that'd be great!

 

Thanks,

1 ACCEPTED SOLUTION

SunilKumar_P
Giga Sage

Hi @Wasdom_Kung, If you want to restrict the incident creation when the logged in user is not part of the assignment group, you can create a Before Insert Business Rule.

(function executeRule(current, previous /*null when async*/ ) {

    if (!gs.getUser().isMemberOf(current.assignment_group)) {
        gs.addErrorMessage("You are not an member of Assignemt group.");
        current.setAbortAction(true);
    }

})(current, previous);

 

Regards,

Sunil

View solution in original post

11 REPLIES 11

This has worked - is there a way to update it so that if the user has the admin role, it will work for them? 

Edit: Ignore me, I added !gs.role('admin') to the condition above the script.


Edit 2: Upon activation, this is preventing any group from being assigned to, not just the group mentioned, my version of the script which I've updated the group name to is below:

// Condition: !gs.hasRole('admin')

(function executeRule(current, previous /*null when async*/ ) {

    // Check if the current user is a member of NHSL-SN-ServiceDeskEnquiries
    var currentUser = gs.getUser();
    var groupMembership = new GlideRecord('sys_user_grmember');
    groupMembership.addQuery('group', 'NHSL-SN-ServiceDeskEnquiries'); // Adjust the group name as needed
    groupMembership.addQuery('user', currentUser.getID());
    groupMembership.query();

    // If the current user is not a member of NHSL-SN-ServiceDeskEnquiries, clear the assignment group field and display an error
    if (!groupMembership.hasNext()) {
        current.assignment_group.clearValue();
        current.assignment_group.setDisplayValue('');
        gs.addErrorMessage('You are not authorised to assign to this group.');
    }
})(current, previous);

 


Thanks!

Jyoti Jadhav9
Tera Guru

Hi @Wasdom_Kung ,

 

1. You can use below onChange client script and script include.

1. OnChange Client Script:

Type : onChange

Field Name: Assignment Group

Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
//Type appropriate comment here, and begin script below
   var sysid = g_user.userID; //get current user sysid
   var grpid = g_form.getValue("assignment_group"); //get group sysid
   if(grpid != ""){
    var ga = new GlideAjax('global.CheckuserGroup'); //script include name
    ga.addParam('sysparm_name', 'getgroup'); //function name
    ga.addParam('sysparm_name_sysid', sysid); //passing sysid to user
    ga.addParam('sysparm_name_grpid', grpid); //passing sysid to group
    ga.getXML(getGroup);
   }
    function getGroup(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        if (answer == 'false') {
            alert('You are Not Part of group');
            //g_form.addErrorMessage("You are Not Part of group"); // use as per your requirement
            g_form.setValue("assignment_group","");
        } else if(answer == 'true'){
            alert('You are Part of group');
        }
    }
}

Screenshot:

JyotiJadhav9_0-1707401586142.png

2. Script Include:

var CheckuserGroup = Class.create();
CheckuserGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getgroup:function(){
        var usersysid = this.getParameter('sysparm_name_sysid');//getting usersysid from client
        var groupsysid = this.getParameter('sysparm_name_grpid');
        if(groupsysid != ""){
        var mem = new GlideRecord("sys_user_grmember");
        mem.addQuery('user', usersysid); //filtering current user
        mem.addQuery('group', groupsysid); //add group sysid
        mem.query();
        if (mem.next()) {
            return true;
        } else {
            return false;
        }
        }
    },

    type: 'CheckuserGroup'
});
Screenshot:
JyotiJadhav9_1-1707401661027.png

Please hit the like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

 

Thanks & Regards

Jyoti Jadhav