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
matermortsmatermorts 

Dynamic email recipient list based on Case owner (queue)

Hi all,
I have a child object of Case. When I create a record in this child object, I need to send an email alert to the owner of the parent case record. It's no problem if the case owner is an individual. I can use a declarative email alert for that. But if the case owner is a queue, how can I dynamically send the alert to all queue members?
Best Answer chosen by matermorts
Raj VakatiRaj Vakati
You can able to do it a couple of ways .. 

1 . 
https://salesforce.stackexchange.com/questions/129171/send-email-to-queue-members-using-apex
If( Case.OwnerId =='005'){

//Case Owner is User so send email to user

}
else{
// Get Queue members

 // ANd there Email and Sent it 
 


}



2 . Database.DmlOption

 
Case newCase = new Case();
newCase.Status = 'New';
insert newCase;

Group grp = [select Id from Group where Name='Case Queue' and Type = 'Queue'];
newCase.OwnerId = grp.Id;
 
Database.DmlOptions options = new Database.DmlOptions();
options.emailHeader.triggerUserEmail = true;
Database.update(newCase, options);


 

All Answers

Raj VakatiRaj Vakati
You can able to do it a couple of ways .. 

1 . 
https://salesforce.stackexchange.com/questions/129171/send-email-to-queue-members-using-apex
If( Case.OwnerId =='005'){

//Case Owner is User so send email to user

}
else{
// Get Queue members

 // ANd there Email and Sent it 
 


}



2 . Database.DmlOption

 
Case newCase = new Case();
newCase.Status = 'New';
insert newCase;

Group grp = [select Id from Group where Name='Case Queue' and Type = 'Queue'];
newCase.OwnerId = grp.Id;
 
Database.DmlOptions options = new Database.DmlOptions();
options.emailHeader.triggerUserEmail = true;
Database.update(newCase, options);


 
This was selected as the best answer
matermortsmatermorts
Hi Raj,
Thank you for the tip. I ultimately decided to implement something similar to your first suggestion. It works perfectly.
public class CrossOrgCommunicationService
{
	public static void sendCrossOrgCommNotice(List<Comment_History__c> cocList)
	{
		String caseNumber = null;
		String queuePrefix = '00G';
		String queueId = null;
		String userRecipientId = null;
		List<User> recipients;
		EmailTemplate emailTemplate = [Select Id, Name From EmailTemplate Where DeveloperName = 'New_Cross_Org_Communication_Notice'];
		
		for(Comment_History__c coc : cocList)
		{
			caseNumber = coc.Case__r.CaseNumber;
			if(coc.CreatedById == coc.Case__r.CreatedById)
			{
				if (String.valueOf(coc.Case__r.OwnerId).left(3) == queuePrefix)
				{
					queueId = coc.Case__r.OwnerId;
				} else {
					userRecipientId = coc.Case__r.OwnerId;
				}
			} else {
				userRecipientId = coc.Case__r.CreatedById;
			}
		}
		
		if(userRecipientId != null)
		{
			recipients = [Select Id, Email From User Where Id = :userRecipientId];
		}
		
		if(queueId != null)
		{
			List<Id> groupRecipientIds = new List<Id>();
			Group grp = [Select Id, Name, (Select UserOrGroupId From GroupMembers) From Group Where Id = :queueId];
			for(GroupMember grpMem : grp.GroupMembers)
			{
				groupRecipientIds.add(grpMem.UserOrGroupId);
			}
			
			if(groupRecipientIds != null && groupRecipientIds.size() > 0)
			{
				recipients = [Select Id, Email From User Where Id In :groupRecipientIds];
			}
		}
		
		if(recipients != null && recipients.size() > 0)
		{
			EmailHelper.sendCrossOrgCommNotice(recipients, emailTemplate.Id, caseNumber);
		}
	}
}