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 

Use Parent User Id in Trigger

I'm trying to create a trigger to send an email notification to the original creator of a case anytime a Case Comment is added.  I've tried everything and I can not get the syntax correct.  Can anyone offer any suggestions.  I keep getting an error with the query statement stating unexpected token 'cc'

 

The code I have is:

 

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

 
for(CaseComment cc:[select parent.createdby.email from cc where id = cc record id]){
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    String[] toAddresses = new String[] {cc};
     }  
           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 UserID:';
      
           for (CaseComment u : Trigger.new) {
          msg = msg + u.CreatedBy;
         }

mail.setHtmlBody(msg);

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


}

Best Answer chosen by Admin (Salesforce Developers) 
waylonatcimwaylonatcim

Maybe something like:

 

 

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

for(CaseComment curCC : Trigger.new){
	for(CaseComment cc:[select parent.createdby.email from CaseComment where id = :curCC.Id]){
	    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
	    String[] toAddresses = new String[] {cc.parent.createdby.email};
	     }  
	           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 UserID:';
	      
	           for (CaseComment u : Trigger.new) {
	          msg = msg + u.CreatedBy;
	         }
	
	mail.setHtmlBody(msg);
	
	Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
		
	}
}

 

I didn't dive into your logic at all, just fixed some errors in how you are calling things.  Are you creating this in Eclipse?

If not, I would recommend it, as it points out some obvious errors and also offers some suggestions.