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

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.

@Nayan Dhamane

 

You did it again! Worked like a charm, thank you!