You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

Handling Inline Images of the Email Content from Email-to-Case


By: Radha

Requirement: 

A field in Case object should capture the inline images, when a new Case is created via Email-to-Case. Most of us will store the htmlbody field of the Email message to a custom case field by writing a Trigger for Email Message.

Existing Solution: 

By enabling HTML email checkbox in Email-to-Case settings, we can get the HTML content of the email in Emails related list of the Case.

HTML contents like tables are added directly to the Email Message object of the Case, but the images are added as attachments to the Emails.

Below is the sample code Written in Email Message: 

trigger HandlingHtmlContent on EmailMessage (after insert) { 
List<Case> caselist = new List<Case>();  
   for(EmailMessage em : Trigger.New){ 

Case curCase = new Case(); 

curCase.Id = em.parentId; 

curCase.emailContent__c = em.HtmlBody(); 

caselist.add(curCase); 

  } 

update caseList ;    
}

 

Email content: 

Image

The value stored in Salesforce:  

 

Image

None of the existing methods can handle your email’s inline images. I searched communities a lot and realized that many people had the same issue but they never found any solution.

Please refer the link https://success.salesforce.com/ideaview?id=08730000000Gp9dAAC

New Solution: 

We face this challenge only for the images. I found that the images are not loaded in single transaction. So, I updated the code by using @future. We have to modify the existing trigger as shown below.

public class getHtmlHandler{ 
static list<case> caseList = new list<case>(); 
@future 
public static void processEmailMessage(List<Id> emailMsgIds){   
    for(EmailMessage em : [SELECT Id, ParentId, HtmlBody FROM emailMessage WHERE Id IN: emailMsgIds]){ 
       case cc = new case(); 
       cc.id = em.ParentId; 
       cc.emailContent__c = em.HtmlBody; 
       caseList.add(cc); 
   } 
   update caseList ; 
 }   
} 

trigger HandlingHtmlContent on EmailMessage (after insert) { 
List<Id> emailMsgId = new List<Id>();  
   for(EmailMessage em : Trigger.New){ 
   emailMsgId.add(em.id); 
   } 
   getHtmlHandler.processEmailMessage(emailMsgId); 
} 

Now the value stored in Salesforce: 

Image

Conclusion: 

We can capture all inline html contents of the email sent via Email-to-Case by using the above Apex code with @future annotation.