+ Start a Discussion
Jim Parker 7Jim Parker 7 

Case Feed Email Alert

Hello - trying to send out an email to the case creator anytime someone adds a "post" to a case on the feed view.  I am however struggling.  I am trying to send out an email based on an email template that already exists.  Few issues that I am having - what object should I set the trigger on (FeedItem?), how do I limit the trigger to only be "posts" on Cases, how do I extract the message out of the post, how do I send an email based on an email template, and how do I include the post text in the email template?  Gave it a start but got lost...  Any coding help would be greatly appreciated. 
 
trigger FeedItemAddedCase on FeedItem (after insert) {
	Set<ID> feedIds = new Set<ID>();
    for (FeedItem n:Trigger.new){
        feedIds.add(n.parentID);
    }
    List<Case> caseList = new List<Case>([Select id, CaseNumber, Subject, Case_Creator_Employee_Email__c from Case where ID in:feedIds limit 1]);
    EmailTemplate templateId = [Select id from EmailTemplate where name = 'Case Note Alert - Internal'];
    
    for(Case c:caseList){
        Messaging.reserveSingleEmailCapacity(1);
    	Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    	String[] toAddresses; 
        toAddresses = new String [] {c.Case_Creator_Employee_Email__c};
        mail.setToAddresses(toAddresses);
   		mail.setReplyTo('supportresponse@test.com');
    	mail.setSenderDisplayName('Test Bank');
    	mail.setSubject('' + case.Id);
        Messaging.sendEmail(new Messaging.Email[] { mail } , true);
    }



}

 
sukanya banekarsukanya banekar
Hi Jim,

Try with the below code 
trigger FeedItemAddedCase on FeedItem (after insert) {
	Set<ID> feedIds = new Set<ID>();
    for (FeedItem n:Trigger.new){
        feedIds.add(n.parentID);
    }
    
    EmailTemplate templateId = [Select id from EmailTemplate where name = 'Case Note Alert - Internal'];
    List<Messaging.SingleEmailMessage> lstEmail = new List<Messaging.SingleEmailMessage>();
    for(Case c:[Select id, 
					    CaseNumber, 
						Subject, 
						Case_Creator_Employee_Email__c 
				   from Case 
				   where ID in:feedIds 
				   limit 1]){
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
		String[] toAddresses;
		mail.setTemplateID(templateId.Id); 
		mail.setSaveAsActivity(false);
		toAddresses = new String [] {c.Case_Creator_Employee_Email__c};	
		mail.setToAddresses(toAddresses);
		mail.setReplyTo('supportresponse@test.com');
		mail.setSenderDisplayName('Test Bank');
		mail.setSubject('' + case.Id);
		lstEmail.add(mail);
    }
	if(lstEmail <> NULL && !lstEmail.isEmpty()){
		 Messaging.sendEmail(lstEmail);
	}
}

Let me know if this helps you.

Thanks,
Sukanya Banekar