+ Start a Discussion

Field update from Email Message

I'm new to Apex (and coding) and was wondeirng if it was possible to update fields from the EmailMessage.Textbody with an ApexTrigger rather than creating a custom inbound email handler service.  It seems like it would  be easier to just utilize the Email to Case On Demand feature, and have a trigger to parse the data from the default field that the email text body goes to on the Case.  So I know that I need to string the emailbody with substrings for where I want to start capturing the data, and I know that I want to set the string values equal to the Case fields.  Any other help setting this up would be appreciated.  So far I have code that looks like this, but it isn't functional since I don't know what I need to put in between the two statements to complete the code:


trigger TextBody on Case (before insert) {
String[] emailBody = email.plainTextBody.split('\n', 0);
String Site = emailBody[0].substring(17);
String State = emailBody[2].substring(25);
String Field1 = emailBody[4].substring(17);
String Field2 = emailBody[6].substring(18);
String Field3 = emailBody[8].substring(16);
String Field4 = emailBody[10].substring(17);
String Field5 = emailBody[12].substring(17);
String Codes = emailBody[14].substring(17);
String Name = emailBody[16].substring(17);


this.theCase = new Case();
this.theCase.Status = 'Open';
this.thCase.Priority = 'Low';
this.theCase.Site__c = Site;
this.theCase.State__c = State;
this.theCase. Field_1__c = Field1;
this.theCase. Field_2__c =  Field2;
this.theCase. Field_3__c =  Field3;
this.theCase. Field_4__c =  Field4;
this.theCase. Field_5__c =  Field5;
this.theCase.Codes__c = Codes;
this.theCase.Name__c = Name;
this.theCase.Origin = 'Email';
this.theCase.Subject = email.Subject;
this.theCase.Description = email.plainTextBody;


You must create an inbound email handling service. You can think about it as a trigger that fires when an email comes in.

The trigger you're proposing would not fire on an email, only when you create a new Case.


An email handling service is pretty easy to create. Just follow some of the examples.