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
prasanth sfdcprasanth sfdc 

EmailMessage object trigger not working. Please help

I have trigger the below simple trigger to add BCC address dynamically, but its not firing to email. It is showing in the debug log about the added email address . Please help to solve this. 
 
trigger EmailMessage_Trigger on EmailMessage (before insert) {
    
       // EmailMessage_Trigger_Helper.emailMessage(Trigger.New);
     
     for(EmailMessage emsg : Trigger.New){
         if (emsg.Incoming==false && emsg.Status == '3') 
         {
             emsg.BccAddress ='saiprasanth123@gmail.com';  
             emsg.CcAddress ='saiprasanth1234@gmail.com';
           system.debug('coming sai inside:'+emsg.CcAddress+'....'+emsg.FromAddress);

         }
           system.debug('coming sai outside:'+emsg);
     }
}


Thanks,
Sai.
Nand Kishore 7Nand Kishore 7
Hi Team,

When I send email from the Case record using "Send email" Action button. I have written the trigger on EmailMessage to add the cc addresses.

    trigger Trigger1 on EmailMessage (before insert) {
   
    if(Trigger.isbefore){
    
           List<EmailMessage> listMsg = new List<EmailMessage>(trigger.New);
           for(EmailMessage msg : listMsg)
           {
                 String str = 'abcde@gmail.com,abcde@customdomain.com';
                 msg.CcAddress = str;
           }
    }
}

But some time, my email is sent to the ccddress and some time it doesn't send to the cc address.

Could anyone help me on this.

Thank You in advance.
prasanth sfdcprasanth sfdc
Hi nanad, Make the string cc as array of list and then put the emails into it.
Nand Kishore 7Nand Kishore 7
Hi, 

We can not assign the array in the ccAddress.

I wrote below code:
String [] arrayOfProducts = new List<String>();

//Adding elements in Array
arrayOfProducts.add('abcde@gmail.com');
arrayOfProducts.add('gef@gmail.com');

msg.CcAddress =arrayOfProducts;

Output - Code will not compile and says "Illegal assignment from List<String> to String".

Thanks,
prasanth sfdcprasanth sfdc
Array should be new array or list should be new list. Change the below code to new code String [] arrayOfProducts = new List(); List arrayOfProducts = new List();
Nand Kishore 7Nand Kishore 7
Hi,

When I wrote like this:
List arrayOfProducts = new List();

It throws compile time exception i.e "Unexpected token 'List'."

Then I updated it to like below.
 List<String> arrayOfProducts = new List<String>();
        arrayOfProducts.add('abc@gmail.com');
        msg.CcAddress = arrayOfProducts;

It throws compile time exception ie. "Illegal assignment from List<String> to String" so due to this, code did not saved in the developer console.

Thanks,
prasanth sfdcprasanth sfdc
Seems to be error not from this line. Please post complete code. I will check
Nand Kishore 7Nand Kishore 7
trigger MessageTrigger on EmailMessage (before insert) {
    
    if(Trigger.isbefore){
        
        List<EmailMessage> messages = new List<EmailMessage>(trigger.New);
        for(EmailMessage emailMessage1 : messages)
        {     
            List<String> arrayOfProducts = new List<String>();
            arrayOfProducts.add('abc@gmail.com');
             arrayOfProducts.add('abc1234@gmail.com');
            emailMessage1.CcAddress = arrayOfProducts;       
        }
    }
}
 
Babak BehraveshBabak Behravesh
That's exactly my issue. The EmailMessage trigger on after insert does trigger sometimes but does not trigger mostly. Did you find any solutions for that?