Help with script to set assignment group

Amelie G
Kilo Expert

Hello,

I need a bit of help with my script. This is an on load client script on the incident table where I need to check if the user that raises a new record is part of any itil groups and if he is (the user will only be part of 1 itil group, not more), then set the assignment group to that.

Now bear in mind I'm new to this and this what I've come up with:

function onLoad() {

    var usr = g_form.getValue('caller_id'); //get sys id of user
    var gr = new GlideRecord('sys_group_has_role');
    gr.addQuery('role', 'itil'); //pull groups that have itil role
    gr.query();

    while (gr.next()) {
        var grpID = []; // I want the groups to be put into an array
        grpID.push(gr);
        jslog('Groups with itil ' + grpID);

        var mbr = new GlideRecord('sys_user_grmember');
        mbr.addQuery('user', usr);
        mbr.addQuery('group', grpID[0]); //here I want to check if user is part of the first itil group for example
        mbr.query();

        if (mbr.next()) {
           jslog('User part of itil group ' + grpID[0]);
           g_form.setValue('assignment_group', grpID[0]); //if user is part of itil group, set that group as assignment group - I know this is wrong though but I can't see any logs so not sure what type of data grpID is
        }
	//else .. here I need to iterate through the rest of the groups
    }

}

Unfortunately, I can see the logs that I've put there so I'm sure a lot of things are wrong with my logic here. If there is a simpler way to do this, please advise.

Thanks

10 REPLIES 10

oh yes you're right, forgot! 😄 but yeah, my script should provide a good reference