How to join User, GroupMember and Group in SOQL?

3.7K    Asked by JoshuaSmith in Salesforce , Asked on Jul 3, 2021

How can I return the User.Email and Group. Name from SOQL? I want the group names and the email addresses of their members from a query like:

SELECT User.Id, User.Email, Group. Name from User WHERE Id IN (SELECT UserOrGroupId FROM GroupMember WHERE Group.Name IN ('Group A', 'Group B'))

Obviously, my query is incorrect but I can't see how.

Answered by Hien Castanon

It is conceptual people as you're trying to bring User records but in them a Group field that doesn't actually exist. GroupMember object contains a row for each User in the group. Each record contains (FK UserOrGroupId into User), and (FK GroupId into Group). It seems that my grasp of concepts is OK, but its not so easy to get the information from the two related objects. The Group is really the drivng object for the results set I want - I want the email addresses of users in the groups of interest. After having obtained the users, if you were to obtain the group to which the user belongs, you can achieve it through different paths: The first is to make one query for each group, that way you will have different lists of users, having each list the users of that specific group:

    List groupAUsers = [SELECT User.Id, User.Email FROMUser WHERE Id IN (SELECT UserOrGroupId FROM GroupMember WHERE Group.Name = 'GROUP A)]`

 The second option, and the desired one in case you want to perform this with many groups, is to first query for the Users and map them by Id. Afterward, query GroupMembers filtered by the previous user list. Then just play with your data as desired.

// Get the users. User[] users = [SELECT User.Id, User.Email FROM User WHERE Id IN (SELECT UserOrGroupId FROM GroupMember WHERE Group.Name IN ('Group A', 'Group B'))]; // Get the Ids. Id[] userIds = new List(new Map(users).keySet()); // And now thet the groupmembers. GroupMember[] groupMembers = [SELECT Group.Name, UserOrGroupId FROM GroupMember WHERE UserOrGroupId IN :users];

Your Answer

Answer (1)

In Salesforce, to query data across related objects such as User, GroupMember, and Group, you can use a combination of nested queries and relationship queries in SOQL (Salesforce Object Query Language).


Here’s how these objects are related:

User: Represents an individual user.

Group: Represents a collection of users (public groups, roles, etc.).

GroupMember: Represents a member of a group.

The GroupMember object links User and Group objects through its UserOrGroupId field, which can reference either a User or another Group.

Example Query to Join User, GroupMember, and Group

To retrieve users who are members of a specific group, you can use the following SOQL query:

SELECT Id, Name, (SELECT UserOrGroupId FROM GroupMembers) 
FROM Group
WHERE Type = 'Regular' AND Name = 'Specific Group Name'

In this example:

The outer query retrieves Group records.

The subquery retrieves GroupMember records related to each group.

However, if you need to retrieve detailed user information for group members, you should join these objects appropriately. Here’s a more detailed example:

Retrieve Users with their Associated Group Information

SELECT 
    Id,
    Name,
    (SELECT
        Group.Id,
        Group.Name
     FROM
        GroupMembers
     WHERE
        Group.Type = 'Regular')FROM
    User

In this example:

The outer query retrieves User records.

The subquery retrieves GroupMember records for each user and fetches related Group details.

If you want to retrieve users of a specific group by its name, you can use the following:

SELECT 
    User.Id,
    User.Name,
    Group.Name
FROM
    GroupMember
WHERE
    Group.Name = 'Specific Group Name'

In this query:

  • The outer query retrieves GroupMember records.
  • The related User and Group information is fetched directly.

Detailed Example with Field Selection

Here’s a more complete example where you join User, GroupMember, and Group to get specific fields:

Example: Retrieve User Information for Members of a Specific Group

SELECT 
    User.Id,
    User.Name,
    User.Email,
    Group.Id,
    Group.Name
FROM
    GroupMember
WHERE
    Group.Type = 'Regular' AND
    Group.Name = 'Specific Group Name'

In this query:

  • We select Id, Name, and Email from User.
  • We also select Id and Name from Group.
  • The GroupMember object is used to join these objects.

Advanced Example with Filtering

Example: Filter Users in a Specific Group

SELECT 
    Id,
    Name,
    (SELECT
        UserOrGroupId,
        Group.Type,
        Group.Name
     FROM
        GroupMembers
     WHERE
        Group.Name = 'Specific Group Name' AND
        Group.Type = 'Regular')
FROM
    User
WHERE
    Id IN (
        SELECT
            UserOrGroupId
        FROM
            GroupMember
        WHERE
            Group.Name = 'Specific Group Name' AND
            Group.Type = 'Regular'
    )

In this example:

  • The outer query retrieves User records filtered by a subquery.
  • The subquery retrieves GroupMember records to filter User records that are part of the specified group.
  • An additional nested subquery retrieves detailed Group information for each GroupMember.

Summary

These examples demonstrate how to effectively join User, GroupMember, and Group objects in SOQL to retrieve comprehensive and related data from Salesforce. Adjust the queries based on your specific requirements and data model.















6 Months

Interviews

Parent Categories