The CreatorCon Call for Content is officially open! Get started here.

How to query a group type from a Glide list field and add role to group.

Anderson_R
Tera Contributor

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. 

Anderson_R_1-1692714513700.png


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? 

Anderson_R_0-1692714463595.png

 

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
    var groupGr = new GlideRecord('sys_user_group');
    groupGr.addQuery('u_plant', plantCode);
groupGr.addQuery('type', 'IN', 'dpw clerk');
    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);
2 ACCEPTED SOLUTIONS

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');

 

 

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

View solution in original post

Hello @Anderson_R ,

just instead of role name use role sys_id. It will definitely solve your issue.

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

View solution in original post

6 REPLIES 6

umaaggarwal
Giga Guru

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');

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. 

 

Anderson_R_0-1692717845924.png

 



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');

 

 

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

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):

Anderson_R_0-1692728275365.png

 

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);