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
Jeff WaalkesJeff Waalkes 

Blank Email Subject Line on Incoming Emails

I am working on setting up a trigger for incoming emails that create cases. I need to check if the email subject field is blank or "null" and put a placeholder text within the incoming email subject line that will then be the case subject line and will have case SLA's tied to those cases. 

I have tried this Apex before trigger: 
trigger AssociateEmail on EmailMessage (before insert) {

 
    for(EmailMessage eMsg : Trigger.new) {
        
               if(eMsg.Incoming && (eMsg.subject == '' || eMsg.subject ==  null)){
               
                 eMsg.subject = 'Add your subject here';
               }       
       
    }

 
}
But this has not placed a placeholder subject on the case and I still have no SLA's tied to the case when no case subject line is set. 

What am I missing to accomplish this?
AnkaiahAnkaiah (Salesforce Developers) 
Hi Jeff,

You need to write a trigger on case object. try with below.
trigger IncomingEmailSubjectUpdate123 on Case (before insert) {
    
    for(case em:trigger.new){        
        If(em.origin =='Email' && (em.Subject=='' || em.Subject==Null)){
            em.Subject = 'add your subject here';
        }
    }

}

If this helps, Please mark it as best answer.

thanks!!
 
Jeff WaalkesJeff Waalkes
Hey, Ankaiah, 

Thanks for the reply, if I am getting a compile error trigger name exists on a different sObject. Would this mean I need to tie a different trigger or object to the code? 

Thanks 
AnkaiahAnkaiah (Salesforce Developers) 
That means, you have existing trigger with the same name, changes trigger name will resolve the issue.

Make sure you need to write trigger on case object.

If this helps, Please mark it as best answer.

Thanks!!