- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2023 07:32 AM
Hello,
Looking for some help on the script below. I need to find groups that contain the group type "dpw clerk" from a glide_list field. What I currently have, does not pick up the group type.
Also, while the specified role is being added to the queried groups (groups found based off matching plant code since group type query isn't working) the role name is not displayed on the "role" tab at the bottom of the group record. How can I have the role name display on the group record?
Current Script:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2023 08:41 AM
Hello @Anderson_R ,
The field type is glide_list type thus it store sys_id of record stored and to query it like the below:
groupGr.addEncodedQuery('typeLIKEsys_id_of_the_type');
Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2023 11:27 AM
Hello @Anderson_R ,
just instead of role name use role sys_id. It will definitely solve your issue.
Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2023 07:59 AM
Hi,
Could you please put a log statement like below
gs.log('the count is '+ groupGR.getRowCount())
groupGr.addQuery('type', 'IN', 'dpw clerk');
in this line are dpw and clerk two different values or only one ?
if only one, you need not to use operator. You can say
Could you please put a log statement like below
gs.log('the count is '+ groupGR.getRowCount())
groupGr.addQuery('type', 'dpw clerk');
if these are 2 different values then it has to be like below
Could you please put a log statement like below
gs.log('the count is '+ groupGR.getRowCount())
groupGr.addQuery('type', 'IN', 'dpw,clerk');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2023 08:24 AM - edited 08-22-2023 08:27 AM
Hi Umaaggarwal,
Thank you for trying to help me!
"dpw clerk' is one value. Some groups on the sys_user_group table contain more than one type. For example, their group type can be "dpw clerk" and "wm_dispatch". Not sure if that makes a difference. Unfortunately, this line did not work:
groupGr.addQuery('type', 'dpw clerk');
The log count was "0" but I know for a fact that there is 1 group on the sys_user_group table that meets the query criteria.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2023 08:41 AM
Hello @Anderson_R ,
The field type is glide_list type thus it store sys_id of record stored and to query it like the below:
groupGr.addEncodedQuery('typeLIKEsys_id_of_the_type');
Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2023 11:11 AM - edited 08-22-2023 11:19 AM
Hi nayan4,
Thank you, the addEncodedQuery worked! The business rule is filtering down to the records I want it to, but the specified role is not being added to the role tab of the queried groups. Can you assist with this part?
The role is being added, but it looks like this on the group record (name of role does not display):
Current Script:
(function executeRule(current, previous /*, g*/ ) {
var plantCode = current.u_plant_code;
// Query the sys_user_group table to find groups with the same plant code and type 'dpw clerk'
var groupGr = new GlideRecord('sys_user_group');
groupGr.addQuery('u_plant', plantCode);
groupGr.addEncodedQuery('typeLIKE298d6e38dbdbd0507bd95425f39619a6');
groupGr.query();
// Loop through the found groups and add the role
while (groupGr.next()) {
var groupId = groupGr.getValue('sys_id');
// Add role to sys_group_has_role
var groupHasRole = new GlideRecord('sys_group_has_role');
groupHasRole.initialize();
groupHasRole.group = groupId;
groupHasRole.role = 'sn_fieldservice.dpw_clerk'; // Role name
groupHasRole.insert();
}
})(current, previous);