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 

I need to set a before trigger on incoming emails that sets a value to an email that has a null subject line. What is the proper apex code to set this trigger

I need to set a before trigger on incoming emails that sets a value to an email that has a null subject line. What is the proper apex code to set this trigger that will take an incoming email with nothing in the subject line and set placeholder verbiage 
mukesh guptamukesh gupta
Hi Jeff,

Please use below code:-
 
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';
		}	
       
    }

}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Jeff WaalkesJeff Waalkes
Thanks for the answer, when I use that I still have no subject on my case, I have a flow that is set up that when an email is submitted the email subject line is the case subject. I am trying to keep that flow and email to case active but look when an email subject is blank or null and set a value. I have Milestones that are tied and when no subject is set those are not kicked off. 

Is this still the right path to set a before trigger? 

Thanks