Advanced reference qualifier for group membership

Morten Steenbac
Giga Expert

I have a reference field on sys_user - 'Default Assignment Group'.

I want the lookup list to be filtered so you can only pick groups that the user is member of.

Can anyone give me some input on how to do the script include.

So far my starting point looks like this, but I'm not sure how to asign the current sys_user object to var a.

Anyone?

function u_backfillAssignmentGroup() {

    var gp = ' ';

    var a = current;

    //return everything if the assigned_to value is empty

    if(!a)

          return;

   

    //sys_user_grmember has the user to group relationship

    var grp = new GlideRecord('sys_user_grmember');

    grp.addQuery('user',a);

    grp.query();

    while(grp.next()) {

          if (gp.length > 0) {

                //build a comma separated string of groups if there is more than one

                gp += (',' + grp.group);

          }

          else {

                gp = grp.group;

          }

    }

    // return Groups where assigned to is in those groups we use IN for lists

    return 'sys_idIN' + gp;

}

1 ACCEPTED SOLUTION

dvp
Mega Sage
Mega Sage

In line 2 use


var a = current.sys_id;


View solution in original post

6 REPLIES 6

Inactive_Us1474
Giga Guru

Instead of current, user gs.getUserByID(); , it will work.


Ankur Bawiskar
Tera Patron
Tera Patron

Hi Morte,



Use the below code for reference qualifer



function u_backfillAssignmentGroup() {  


var userSysId = gs.getUserID();


var groupSysIdArray = [];


var gr = new GlideRecord('sys_user_grmember');


gr.addQuery('user',userSysId);


gr.query();


while(gr.next()){


  groupSysIdArray.push(gr.group.toString());


}


return "sys_idIN" + groupSysIdArray.join(',').toString();  


}



Call the function correctly through reference qualifier. It should return all groups which current logged in user is member of




Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

dvp
Mega Sage
Mega Sage

In line 2 use


var a = current.sys_id;


Thanks - most simple solution, and it worked:)