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
SeanSeeSeanSee 

How can I set ToAddress to send an email notification to the Creator of a Parent Record

Hi All,

 

I need to send an email notification to the creator of a Case anytime a new Case Comment is added.  So far I have only been able to send an email to a static email address.  Is there a way to insert the email address of the User that created the Parent Case?  So far I have:

 

trigger NotifyCaseCreator on CaseComment (after delete, after insert, after undelete,
after update) {

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    String[] toAddresses = new String[] {'me@mydomain.com'};
    
        mail.setToAddresses(toAddresses);
        mail.setSubject('A New Case Comment Has Been Added');
        string msg = 'A Comment has been added to a Case that you had created.  The Comment was added by:';

    for (CaseComment u : Trigger.new) {
    msg = msg + u.CreatedById;
}

mail.setHtmlBody(msg);

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });


    }

hisrinuhisrinu

You have to write a query as below and set the email id of the user record as the to address.

 

Here is the query

 

select parent.createdby.email from casecomment where id = casecomment record id

 

 

SeanSeeSeanSee

Thank you... One last question.  I'm new to Apex and not very familiar with query's.  What is the proper way to insert the query into the code so that it will recognize the email adress and send.

 

I tried replacing the email address witht he query but I see that is incorrect.

hisrinuhisrinu

You can get help from APEX guide. Here is the url Apex Guide