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
Sascha DeinertSascha Deinert 

How can I save certain lines of an e-mail in a variable/field?


I add an email handler service to my org. I store some information from the plainTextBody in fields of the object bt__c (Firstname, Phone....), this works so far. But now I want to store each row in a separate field which the code can't assign. Or can I remove the row which can be assigned to field from the plainTextBody and at the end should store the plainTextBody in one separate field.

Because the mail contains information that I cannot assign to any specific field. For this reason I want to store the rest of the mail in seperate field. Or can I store each row which cannot assign to any specific field in a variable which I can insert into a field? 

I tried to remove the row from the variable emailBodyRows.remove[i] but it doesn't work.
if(email.plainTextBody != Null && email.plainTextBody != '') {
        String[] emailBodyRows = email.plainTextBody.split('\n');           
        Set<String> fieldimport = new Set<String>{'Firstname', 'Phone'};
             FOR (Integer i = 0; i < emailBodyRows.size(); i++) {
                 FOR (String fi : fieldimport) {                  
                     IF (emailBodyRows[i].contains(fi)) {
                         String fieldName = fi + '__c';
                         c.put(fieldName, emailBodyRows[i].substringAfter(fi));
  try {
    insert c;   
    Note note = new Note();    
        note.Title = email.subject + ' - ' + email.fromName + ' - ' +;
        note.Body = email.plainTextBody;
        note.ParentId = c.Id;
    insert note;
  } catch (System.exception e) {
    System.debug(e.getMessage() + e.getStackTraceString());  // assume things may not work, and you'll want details as to why.
  return result;

Vishwajeet kumarVishwajeet kumar
I think storing email body could be useful, since it is incoming data. Ideally you should store whole email without removing anything from it. 
For populating fields, if those values are in name-value pair seperated by a delimiter you can split them and use value to populate fields.

Example : 
String[] emailBodyRows = ['Start Email Body data','FirstName : John','Phone : 1234567890','End Email Body data'];

IF (emailBodyRows[i].contains(fi)) {
String fieldName = fi + '__c';
String[] fieldValue = emailBodyRows[i].split(':');    //2nd one is value 
c.put(fieldName, fieldValue[1].trim());