+ Start a Discussion

Need Urgent Help!!!!!!!! Trigger on Email Message



I have seconadry contact email on my case. So when any email is sent out from within Salesforce (using "Send an Email" button), the secondary contact email must also be included in the email. I wrote a trigger on EmailMessage object to update the CcAddress with the secondary contact email address. It is inserting the email address in the to filed but is not send email to secondary conatct. Here is my code.



trigger SeconadryConInfoOnEmails on EmailMessage (before insert,before update) {
    Set<Id> parentCase = new Set<Id>();
    Map<id,Case> mapCase = new Map<Id,Case>();
    for(EmailMessage t : Trigger.New){
    List<Case> lstCase = [select Id,Sec_Contact__c,Con_Email__c,Con_Phone__c from Case where Id in :parentCase];
    for(case c: lstCase){
    for(EmailMessage m :  Trigger.New){
                if(m.CcAddress ==NULL){
                    m.CcAddress = mapCase.get(m.ParentId).Con_Email__c;

                    m.CcAddress = m.CcAddress+';'+mapCase.get(m.ParentId).Con_Email__c;




Can anyone help me to solev my issue.

Thanks in advance.


I guess your trigger must be only updating the record of EmailMessage which is being created for the activity and not the email which has been sent. So you are able to see the address updated in the Email message record but not on the email which was sent.


Thanks for your reply.

But when we click on send then only a task will be created. If i am not wrong a task will be created when Email Message record is inserted. That is the reason I wrote before insert trigger on Email Message.


I side with eprasu in thinking that the trigger will only fire on insert of the Activity record, and not necessarily interact with the actual email being fired at all.


A way to confirm would be to look at the debug logs and see exactly when your trigger fires in relation to the creation of the email.


So if it is indeed happening the way eprasu and I are thinking of, you might need a custom button that points to the standard email page but autopopulates the additionalTo field with your extra values through the URL.


I created a custom button but is there a way to hide the "Send an Email" button from the Emails related list. I am able to create a custom button on task and able to populate the custom field's email address in the Cc field. But if I put that button on the Activity history related list I am not able to provide the links "reply" and "reply to all" that are available on the Emails related list.


Any ideas how to achieve this.


thanks in advance