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
Robert Wambold 10Robert Wambold 10 

CaseComment Trigger generates email with previous Comment instead of Current.

Happy Holidays!

I created a CaseComment Trigger to send an email to the Case Owner when a new CaseComment has been added.

I created a custom field Case_Community_Comment__c on the Case object to hold the most recent CaseComment and utilize a workflow rule to keep it current.

My issue is... my trigger program sends the email before Case_Community_Comment__c has been updated with the most recent CaseComment.

Anyone willing to take look?

Thanks.

Robert

trigger SendCaseCommentEmail on CaseComment (after update) {

  {
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    for(Casecomment cc: Trigger.new)
    {
        case cs=new case(id=cc.ParentId);
        contact co=new contact(id=cs.ContactId);
        user us=new user(id=cs.OwnerId); 
      //if(us.ProfileId == '00e0y000001jD95') // Almac Community Login User  
        if(cc.IsPublished == True){
            // Messaging.SingleEmailMessage message1 = new Messaging.SingleEmailMessage();
            mail.setSubject(cs.Subject);
            mail.setToAddresses(new list<string>{'Robert.Wambold@AlmacGroup.com'}); // CaseOwner Email Address
            //mail.setToAddresses(new list<string>{us.Email}); // send email to Case Owner
            mail.settemplateid('00Xc0000000Nimi'); 
            mail.setTargetObjectId('003c000001Mz1vOAAR'); // ContactId
            mail.whatid= cs.id;
            // mailmsglist.add(message1);
           
        } 
       
    }
    
//  Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> (mailmsglist); 
//  Messaging.SendEmailResult[] results = Messaging.sendEmail(messages); 
    Messaging.SendEmailResult[] results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});  
    if(results.size()>0){
    if (results[0].success) {
            System.debug('The email was sent successfully.');
            } else {
            System.debug('The email failed to send: '
            + results[0].errors[0].message);
            }
    }
  }
}
 

Email Template

Best Answer chosen by Robert Wambold 10
Robert Wambold 10Robert Wambold 10

Solved...the order of execution was the culprit. The Trigger fires before my WorkFlow, thus making the current CaseComment unavailable.

Th solution was to move the update to case to the Trigger.

All Answers

Robert Wambold 10Robert Wambold 10
*** I failed to mention the CaseComment i was experimenting with before insert, after insert, before update, and after update... none makes a difference as far as Case_Comment_Community_C displaying previous CaseComment.CommentBody.
Robert Wambold 10Robert Wambold 10

Solved...the order of execution was the culprit. The Trigger fires before my WorkFlow, thus making the current CaseComment unavailable.

Th solution was to move the update to case to the Trigger.

This was selected as the best answer