+ Start a Discussion
Noor FazliNoor Fazli 

How to find the email addresses of users in a Public group

Hi,

We need to send email to all the members of a public group.  How to query emails of all the users in a public group in Apex controller. so far I have done this. Not sure how the object user can be used to get the "email of all users" in a public group.

group g = [SELECT id FROM group WHERE name = 'IT Testers'];
System.debug('g:' + g);

for (GroupMember gm : [Select UserOrGroupId from GroupMember where GroupId =: g.id]){ 
 System.debug('gm.UserOrGroupId:' + gm.UserOrGroupId); }



}
 
Best Answer chosen by Noor Fazli
sandeep sankhlasandeep sankhla
Hi Noor,

You can simply collect all these UserOrGroupId in a set<String> and then Query on User where Id IN: Set...

This will solve you issue..

Please implement the formula field as I mentioned above and implement the same and let me know if it works for you..

Thanks,
Sandeep

All Answers

sandeep sankhlasandeep sankhla
Hi Noor,

You need to first Query on GroupMember and get all userIds...then you need to query on user to get the emails..

But user email is not querable so in this case you need to create a formula field on user which will store the user email address and then that custom field you can query from user ..

Please implement this and let me know if this solves your issue..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
 
Noor FazliNoor Fazli

thanks Sandeep for your reply. I'm able to get the UserOrGroupId. I don't seem to understand the hierarchy between GroupMember and User? Which field is used to link them together?

sandeep sankhlasandeep sankhla
Hi Noor,

You can simply collect all these UserOrGroupId in a set<String> and then Query on User where Id IN: Set...

This will solve you issue..

Please implement the formula field as I mentioned above and implement the same and let me know if it works for you..

Thanks,
Sandeep
This was selected as the best answer
Piotr MęcnarowskiPiotr Męcnarowski
I have users in public group, I sent a messages to public group. One user did not receive mail . When I do Test Deliverability I have information- 16 test messages have been sent to this email address. I open receved mail I I only don't have this user email adress.
Object was imported from Sandbox
Where is the problem??