2

i have a custom text field which allow user to enter email address, separate by ':' if multiple email address and a trigger on case to send an email to those email addresses entered by customer. but at the end the trigger not only send email to those email address in the text field, it also send email to the case's contact.

here is my trigger, how can i exclude the case's contact from the recipient?

trigger EscalateEmail on Case (after Insert,after update){
            case[] c = trigger.new;

            if(c[0].Status == 'Escalated')
            {

                if ( c[0].Escalate_Email__c !=null)
                 {
                     string templateName = 'Case Email Escalation';
                     String[] emailAddr = c[0].Escalate_Email__c.split(';');
                     EmailTemplate e = [select Id,Name,Subject,body from EmailTemplate where name like :templateName+'%'];
                     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                     List<Messaging.SendEmailResult> results = new list<Messaging.SendEmailResult>();
                     if(emailAddr.size() > 0){

                             System.Debug('Split emailAddr>>>>'+ emailAddr );

                             mail.setToAddresses(emailAddr );

                             mail.setTargetObjectId( c[0].ContactId);
                             mail.setWhatId(c[0].Id);

                             mail.setTemplateId(e.Id);

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

                                     if (!results.get(0).isSuccess())                      
                                        System.debug('>>>>>>>>>>>>>> Error generated when sending mail - ' + results.get(0).getErrors()[0].getMessage());

                              } catch(Exception ex) {       

                              }

                     }

                }     

        }

}

2 Answers 2

1

The email is also sent to the Case contact because of this

mail.setTargetObjectId( c[0].ContactId);

The question is: Do you really need to use the template specified ? If yes, you may need to consider creating a fake contact or user (e.g a free chatter user) in your system which will be spammed with all emails sent via this functionality (not nice...)

If you don't need the template, you can very easily skip the above command and only specify the toAddresses.

2
  • Yes, we do need to use the template specified. That's why i have to specify the setTargetObjectId(). I was thinking that if i create a temp contact during sending this email and delete it after sending out my email will there be any consequence? is it good way? Commented Jan 28, 2014 at 2:15
  • This might also work, but I am not 100% sure... Creating a contact might also fire triggers if there are any so I wouldn't suggest this. But in any case give it a try and see how it works. I have implemented the fake chatter user a couple of times in different projects and works fine till now
    – ManSpan
    Commented Jan 28, 2014 at 9:06
0

You need to use Messaging.MassMailMessage instead of Messaging.SingleEmailMessage. Thereafter you will need to change the target object method: remove a method call setTargetObjectId and use setTargetObjectIds.

See more about difference: http://www.infallibletechie.com/2012/11/what-is-difference-between.html

1
  • Can't. i don't want to email to my contact, leads or user. i wanted to email to custom email address fill up in my custom field Escalate_Email__c in my case Commented Jan 27, 2014 at 10:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .