function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ankit Kalsara 6Ankit Kalsara 6 

How to send email to Queue members using Messaging.sendEmail

Hi team,

I am trying to send email using Messaging.sendEmail() method using Apex trigger on Case object.

The Owner of the Case object could be single owner or a Queue. How can I send the email address to all the members of the queue when Case owner is a Queue?
ShirishaShirisha (Salesforce Developers) 
Hi Ankit,

Greetings!

You can create the list to add all the emails of the members in the queue and then use the Messaging.sendEmail() to send to the list.

Please refer the below sample code:
 
List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
for(Default_Case_Queue__c casedefaultSetting : Default_Case_Queue__c.getAll().values())  {
     if(casedefaultSetting.Fleet_PLC__c.equals(equip.Fleet_Owner_PLC__c) && mapQNameWithIds.containsKey(casedefaultSetting.queueName) ){
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
           List<String> sendTo = new List<String>();
           sendTo.addAll(emailMap.get(casedefaultSetting.queueName));
           // Assign the addresses for the To and CC lists to the                      mail object.
           mail.setToAddresses(sendTo);


            // Specify the subject line for your email address.
            mail.setSubject('New Case Updated : ' + case.Id);

            // Set to True if you want to BCC yourself on the email.
            mail.setBccSender(false);


          // Specify the text content of the email.
          mail.setPlainTextBody('Your Case: ' + case.Id +' has been updated.');

           mail.setHtmlBody('Your case:<b> ' + case.Id +' </b>has been updated.<p>'+
     'To view your case <a      href=https://***yourInstance***.salesforce.com/'+case.Id+'>click here.</a>');
                   emailList .add(mail );

                    }
                } 
// Send the email you have created.
Messaging.sendEmail(emailList ); //keep this line out of for loop
Reference:https://salesforce.stackexchange.com/questions/129171/send-email-to-queue-members-using-apex

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri


 
Ankit Kalsara 6Ankit Kalsara 6
Hi Shirisha,

I did check the reference link you provided earlier. Thanks for sharing.

However the system allows lot of flexibility while adding queue members in the queue. For example, in my org, the Queue is made of users and groups. The group eventually contains other members in it.

Thus I was looking for some flexible solution. In future if someone makes adjustment to the original queue, my code should be able to handle it!

Regards,
Ankit