SOQL Query on Group and GroupMember in Salesforce

In Salesforce, the Group object stores multiple data types, including public groups, queues, roles, etc. We can see and retrieve all the data type values from the group object using the SOQL query.

In this Salesforce tutorial, I will explain how to query Salesforce Group and Group Members in a SOQL query for different scenarios.

SOQL query for Group in Salesforce

The Group object in Salesforce represents a collection of users, which could be a public group, queue, or role. This object is often used to manage and organize users for sharing rules or queue functionality.

Using SOQL, we can query the field details such as ID, Name, DeveloperName, Type, RelatedId, and CreatedDate.

Now, we will execute a SOQL query to get the details from the object ‘queue.

SOQL query:

SELECT Id, Name, DeveloperName, Type, RelatedId
FROM Group
WHERE Type = 'Queue'

Output:

SOQL query for group object in Salesforce

After executing the above query, we will get the details of the Queue records.

SOQL query for GroupMemeber Object in Salesforce

In Salesforce, the GroupMember object links users (or other groups) to a specific group. It helps identify members of public groups or queues.

Now, we will see how to execute the SOQL query to retrieve all members of a specific group, whose GroupId is identified.

SOQL query:

SELECT Id, GroupId, UserOrGroupId
FROM GroupMember
WHERE GroupId = '00GJ3000000VCJxMAO'

Output:

Query on GroupMember Object using SOQL in Salesforce

In the output, we will see the list of users or group IDs retrieved. This way, we can query the GroupMemeber object in Salesforce.

Query Group with GroupMember using SOQL in Salesforce

To query the group records with their group members, we will take an example where we will query the queues with the queue members.

SOQL query:

SELECT Id, GroupId, Group.Name, UserOrGroupId FROM GroupMember WHERE Group.Type = 'Queue'

Output:

SOQL query to get group records with group members

In the above SOQL query output, we can see the list of queue IDs and names with the IDs of queue members.

Query All Group with GroupMember, using SOQL in Salesforce

To query all public groups with IDs of public group members, we can execute the SOQL query in the following way.

SOQL query:

SELECT Id, GroupId, Group.Name, UserOrGroupId FROM GroupMember WHERE Group.Type = 'Regular'

Output:

Query All public groups with members in Salesforce

In the above SOQL query, we retrieved the records of all public records along with the IDs of all group members.

Query All inactive users in a queue or public group in Salesforce

Using SOQL, we can also query all inactive users of both public groups and the queues, and for that, we will execute the SOQL query below.

SOQL query:

SELECT Id, GroupId, Group.Name, Group.Type, UserOrGroupId FROM GroupMember WHERE UserOrGroupId IN (SELECT Id FROM User WHERE IsActive = false) ORDER BY Group.Name

Output:

Query inactive users from groups in Salesforce

This way, we can query the records of inactive users from the public groups and queues in Salesforce.

Get Group and GroupMember query in Salesforce Apex Code

Now, we will see an example of fetching and processing Group and GroupMember data in Apex code.

Let’s take an example where we have an Apex class that retrieves members of a specified queue and logs the results.

Apex class:

public class GroupMemberProcessor {
    public static void fetchQueueMembers(String queueName) {
        List<Group> queues = [
            SELECT Id, Name FROM Group WHERE Name = :queueName AND Type = 'Queue'
        ];

        if (!queues.isEmpty()) {
            List<GroupMember> members = [
                SELECT UserOrGroupId FROM GroupMember WHERE GroupId = :queues[0].Id
            ];

            System.debug('Members of the queue: ' + members);
        } else {
            System.debug('Queue not found.');
        }
    }
}

Call the method with the anonymous method:

GroupMemberProcessor.fetchQueueMembers('cold leads');

While calling the class through an anonymous name, replace the queue name with the name of the queue for which you want to query the group and the group members’ ID.

Output:

Query group member in Salesforce Apex Class

This way, we can query the Group members and groups in the Salesforce Apex code by following the above method.

Conclusion

In Salesforce, the Group and GroupMember objects help to manage users through public groups, queues, and roles. In this Salesforce tutorial, we have learned how, by using SOQL, we can easily retrieve details like group types, members, and inactive users.

We also learned how to fetch and process group data in the Apex class using the SOQL.

You may also like to read:


Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.