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

Jan Cernocky
Tera Guru

Hi Amelie,

haven't you created similar question few days back? I remember working on it but when I wanted to paste my answer the question was deleted 😕

First important thing - do you really want to use onLoad script? I would suggest to run BEFORE INSERT rule - so when someone creates a record, it will do the assignment immediately, you don't need to go to the record and let the onLoad script run.

Hi Jan, yes that was me. Apologies, but I had to re-post the question from my other account. I wasn't sure how to create this functionality to be honest, it started as a BR then changed it to a client script. I'll have a look at what you suggested, thank you!

Dan H
Tera Guru

Hi,

 

As Allen has mentioned, you should really use GlideAjax here and not GlideRecord. 

- You can also use gs.log() instead of jslog and go to "system logs -> All" to see your logs. [edit: dont use gs in client script] 

- Like Jan has said, probably best not to do this in a client script at all, but a business rule.

 

Any how, I've made some changes to your script that I think will satisfy your requirement, I've not tested it however, but it should give you an idea of the logic to do this, which you can then refactor into a GlideAjax/Business rule.

 

function onLoad() {

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

    var grpID = []; 
    while (hasRoleGR.next()) {
        grpID.push(hasRoleGR);
        gs.log('Groups with itil ' + grpID);
	
    }

    checkGroupMember(grpID);

    function checkGroupMember(list){
        for(i = 0; i < list.length; i++){
        var mbr = new GlideRecord('sys_user_grmember');
        mbr.addQuery('user', usr);
        mbr.addQuery('group', grpID[i].group); 
        mbr.query();

        if (mbr.next()) {
           gs.log('User part of itil group ' + grpID[i].group);
           g_form.setValue('assignment_group', grpID[i].group); 
        }
    }
    }

}

Please mark my answer as helpful/correct based on impact

Thank you Dan, I tried with gs.log but I was getting errors that gs should not be used in client script. This script started out as a business rule before adjusting it to a client script. I'll try it again with your updated one.

Hi,

Correct. gs.log or any gs (glidesystem) equivalent is not to be used in client script and gs.log even in server script is not recommended because it doesn't work in scoped apps. You would use gs.info, for example, instead.

Either way, I would recommend reviewing my post above and giving it an attempt on your own end.

I also covered assignment rules (as was also recommended in your duplicate question post) and/or business rule.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!