- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2018 04:10 PM
hi all,
I'm looking run a GlideRecord query where I have a user's sys_id, and I check the [sys_user_grmember] table to see if the user is in a group. But then I also want to check whether that group is of a certain type, by using an addJoinQuery to the [sys_user_group] table to check the "type" field. I pulled some info from servicenow Docs, but unclear how to use it in this context.
Is it possible to use addJoinQuery in this context? One thing that I wonder about whether it's a problem if my field on the [sys_user_grmember] table is a reference field, and the field that matches on the [sys_user_group] field is a string field...will this prevent addJoinQuery from working? OR...Anyone see what's missing in my script? thanks!
//lookup for assignment group
var grp = new GlideRecord('sys_user_grmember');
var grpSQ = grp.addJoinQuery('sys_user_group');//to check the 'type' of the group
//where the gr user from above is the user of the group
grp.addQuery('user',gr.sys_id);//this is the sys_id I've gotten from an earlier GlideRecord
//add the condition that the group is one of 3 types; itsm, hr, or erp_sm
grpSQ.addCondition('typeLIKEitsm');
grp.query();
if (grp.next()) {
current.assignment_group = grp.group;
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2021 07:50 AM
I don't think you need to use the addJoinQuery() method for this. Note that addQuery() supports dot-walking. You should, therefore, be able to achieve what you want with a simple query like this:
var grp = new GlideRecord('sys_user_grmember');
grp.addQuery('user', '<sys_id_of_the_user>');
grp.addQuery('group.type', 'CONTAINS', '<sys_id_of_itsm_type>');
grp.query();
if (grp.next()) {
current.assignment_group = grp.group;
}
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2018 12:57 AM
H Patrick,
This query will probably give you the first record in the sys_user_grmember table. your next query basically checks for all the groups where type is itsm, however this query should happen when the grmember table is actually queried. Hence i dont think join query is actually possible here.
I have tried something which could work for you
var gr = new GlideRecord("sys_user_grmember");
gr.addQuery('user', userID); //variable for the users sys id which you are getting from an earlier query
gr.query();
while(gr.next()){ // will give a list of user-group metric
var group = new GlideRecord("sys_user_group");
group.addQuery('name', gr.group.name); // OR you can also use group.addQuery('sys_id', gr.group); as gr.group will give you the sys id
group.addQuery('u_type', 'type1');
group.query();
while(group.next()) {
gs.print('group name is '+group.name);
}
}
i have tried in my personal instance which works
I hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2021 07:50 AM
I don't think you need to use the addJoinQuery() method for this. Note that addQuery() supports dot-walking. You should, therefore, be able to achieve what you want with a simple query like this:
var grp = new GlideRecord('sys_user_grmember');
grp.addQuery('user', '<sys_id_of_the_user>');
grp.addQuery('group.type', 'CONTAINS', '<sys_id_of_itsm_type>');
grp.query();
if (grp.next()) {
current.assignment_group = grp.group;
}
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2021 07:59 AM
3 years ago?
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2021 08:20 AM
Might be helpful for somebody else 3 years from now.
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/