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:

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:

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:

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:

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.NameOutput:

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:

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:
- HAVING Clause in SOQL
- OFFSET Clause in SOQL
- Query Tasks, Notes, Attachments by Object Type using SOQL in Salesforce
- Create a For Loop to Iterate Through a SOQL Query in Salesforce
- Validate CRUD permission before SOQL/DML operation in Salesforce
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.