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
mroarkmroark 

'New Comment' alert email not being sent on new CaseComment created by a trigger

Hello,

 

I have a new custom object called a 'Case Action Item' related to the Case object in a master-detail relationship.  The requirement is that whenever a new Case Action Item is created, a new CaseComment must be added to the parent Case, and this CaseComment should trigger an alert to the Contact assigned to the Case.

 

The code for the trigger is below, and it functions fine except for one detail.  The email to the Contact is not being sent.

 

I've marked the CaseComment as 'Published' and set the DMLOptions to trigger the AutoResponseEmails.  What am I missing?

 

Any assistance would be appreciated.

 

 

trigger addNewCaseComment on Case_Action_Item__c (after insert, after update) {
List<CaseComment> arrCaseComments = new List<CaseComment> ();
Map<Id,Case> mapRelatedCases = new Map<Id,Case>();
	
for (Case_Action_Item__c cai : Trigger.new)
{
	if (cai.Description__c == 'Initial Request')
		continue;
	if (Trigger.IsUpdate)
	{
		Case_Action_Item__c originalCAI = Trigger.oldMap.get(cai.Id);
		if (cai.Category__c == originalCAI.Category__c && cai.Description__c == originalCAI.Description__c)
			continue; 
	}
	mapRelatedCases.put(cai.Case__c,null);
}
if (mapRelatedCases.size() == 0)
	return;
mapRelatedCases = new Map<Id,Case>([Select Id, AccountId, Account.Name, CaseNumber, Subject from Case where Id in :mapRelatedCases.keySet()]);
for (Case_Action_Item__c cai : Trigger.new)
{
	if (cai.Description__c == 'Initial Request')
		continue;
	if (Trigger.IsUpdate)
	{
		Case_Action_Item__c originalCAI = Trigger.oldMap.get(cai.Id);
		if (cai.Category__c == originalCAI.Category__c && cai.Description__c == originalCAI.Description__c)
			continue; 
	}
	if (cai.Case__c == null || !mapRelatedCases.containsKey(cai.Case__c))
		continue;
		
	Case relatedCase = mapRelatedCases.get(cai.Case__c);
	CaseComment newCaseComment = new CaseComment(ParentId=cai.Case__c);
	newCaseComment.CommentBody = '<Comment body is set based on requirements>';
	newCaseComment.IsPublished = true;
	arrCaseComments.add(newCaseComment);
}

if (arrCaseComments.size() > 0)
{ Database.DMLOptions dlo = new Database.DMLOptions(); dlo.EmailHeader.triggerAutoResponseEmail = true; dlo.optAllOrNone = false;
	Database.SaveResult[] arrResults = Database.insert(arrCaseComments, dlo);
    // Process the results, and send an email notifying IT if any errors were encountered.
    classHelperMethods.processResults(arrResults, 'addNewCaseComment', 'Case');
}

}

 

Navatar_DbSupNavatar_DbSup

Hi,


For sending an email you have to use SingleEmailMessage Methods inside the Class and call that method from trigger in which set the contact email id as toAddress of SingleEmailMessage.

 

Please go through the below code as reference:
///////////////// Apex Class////////////////////////
public class MailerUtils {

public static void sendMail(string message) {

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'me@email1.com','you@email2.com'};
mail.setToAddresses(toAddresses);

mail.setSubject('My Subject');

mail.setUseSignature(false);
mail.setHtmlBody(message);

// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

}

public static testMethod void testSendMail() {
sendMail('This is my email message');
}

}
///////////////////////// Trigger /////////////////////////
trigger TestEmail on Contact (after insert) {

for (Contact c : Trigger.new) {
MailerUtils.sendMail('Welcome ' + c.Name);
}

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

mroarkmroark

S Jain,

 

Actually, I'm not trying to build and send a custom email message in this trigger. 

 

I want the trigger to trigger the automatic alert which occurs when a CaseComment is created. 

 

My issue is that this does not seem to be working, even when I set the DMLOptions for the 'Database.insert' method to send automatic emails.

 

mroark

mkaufmanmkaufman

I'm experiencing the exact same issue.  I have code that compiles a list of CaseComment records, sets the EmailHeader dml options to true, and then uses the database method to insert the list with the dml parameter.

 

The external CaseComment Notification email is NOT automatically sent to the Case Contact, like it should be, but the internal CaseComment Notification email is sent to the Case Owner.


Has anyone reported this to support yet?