How to get Group record from sys_user_group table with available sys_id

madamski
Kilo Contributor

I have a group Sys Id available to me in my code but not the group object, which I need.

 

How do I build out a query to get that group's object, from the sys_user_group table if I have that sys_id? I tried the following but it didn't work

var group_rec;

    var glide = new GlideRecord('sys_user_group');
        glide.addQuery('group', record.assignment_group);
        glide.query();

    while (glide.next()) {
      group_rec = glide.group
    }

// group_rec = NULL
6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

so you have the sys_id of the record and you need glide record object for that

sample script here: gr is that object; you can use that to get value from that group record such as name of group, manager, group email etc

var gr = new GlideRecord('sys_user_group');

gr.addQuery('sys_id', 'sysId'); // give the sys_id here

gr.query();

if(gr.next()){

gs.info('gr is the object');

}

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

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

How to compare manager of requested_by with assignment group manager .need to check if both the managers are same or not.Thanks you!

Alikutty A
Tera Sage

Hi,

You can try this out.

var group_rec;
var glide = new GlideRecord('sys_user_group');
glide.addQuery('group', record.assignment_group);
glide.query();

while (glide.next()) {
  group_rec = glide.group;
  var group = new GlideRecord('sys_user_group');
  if(group.get(group_rec)){
    //group object can be accessed here by dotwalkin on group variable
     gs.info("Name: "+group.name);
  } 

}

 

ARG645
Tera Guru

Simply Query the sys_user_group table as below, if all you wanted was a Group Object by a sys_id of the Group. 

var groupGr = new GlideRecord('sys_user_group');
groupGr.get(record.assignment_group);
ig(groupGr){
  var group_rec = groupGr;
}