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
V'NathV'Nath 

EmailMessage Htmlbody not populated with original format with workflow or trigger

I have configured Email-to-Case functionality to my sandbox and for routing address customers will send emails with their queries, while sending emails they may include some screenshots .

Sample Email Content: 


Hi this is google image and below image screen shot

 User-added image

What Salesforce functionality happen here is by defualt email text will be populated to description field and screen shot will get stored into attachments. and under case object we do have Emails related list where we can see original email content which customer has sent.

But the problem is my customer wants to see original content which customers has sent with in the description field .

So I have created one custom description field and datatype is Rich text area. 

Customization i have done : 

Created workflow rule on EmailMessage Object and tried to populate EmailMessage object HtmlBody field into Case object description (Custome field ) .
text is populated but images are not able to see and some empty textboxes i can see like below.
User-added image


So above solution is not working and again i have done some tirgger on EmailMessage object to update htmlbody into desc field

like below : 

first i put event after insert only but still its doesnt work for me so again i put all the events but no luck.

trigger UpdateDescTrigger on EmailMessage (after insert,after update,before insert ,before update) {
    //set<id> CaseIds = new set<Id>();
    map<Id,EmailMessage> MapEmailMessage = new map<Id,EmailMessage>();
    for(EmailMessage e:trigger.new){
        MapEmailMessage.put(e.ParentId,e);
    }
   
    list<Case> lstC = [select Id,Description__c from Case where id in:MapEmailMessage.keyset()];
    for(Case c:lstC){
        if(MapEmailMessage != null && MapEmailMessage.get(c.Id) != null )
            c.Description__c = MapEmailMessage.get(c.Id).HtmlBody;
    }
    update lstC;
   
}


My question is : i want to automatically update HTMLBody of EmailMessage into Case Description__c (Custom field)

But if i edit case record manually  and saved its working for me when i have done below trigger.


trigger updateCasse on Case (before update) {
    set<Id> parentIds = new set<Id>();
    map<Id,EmailMessage> mapEmailMessage = new map<Id,EmailMessage>();
    for(Case c:trigger.new){   
        parentIds.add(c.Id);
    }   
    list<EmailMessage> lste = [select id,HtmlBody,parentId from EmailMessage where parentid in:parentIds and Incoming = true];
   
    if(lste.size() > 0 ){
     for(EmailMessage e:lste){
         mapEmailMessage.put(e.parentId,e);
     }
     list<Case> lstC = new list<Case>();
     for(Case c:trigger.new){ 
      if(mapEmailMessage != null && mapEmailMessage.get(c.Id) != null)   
          c.description__c = mapEmailMessage.get(c.Id).HtmlBody;       
     }
    }
}

Plz help me.





Vinit_KumarVinit_Kumar
Your trigger shohuld work !!

Try debugging the code and see what's happening and try below code,I have made slight changes to it 

trigger UpdateDescTrigger on EmailMessage (after insert,after update,before insert ,before update) {
    //set<id> CaseIds = new set<Id>();
    map<Id,EmailMessage> MapEmailMessage = new map<Id,EmailMessage>();
	List<Case> newList = new List<Case>();
    for(EmailMessage e:trigger.new){
        MapEmailMessage.put(e.ParentId,e);
    }
   
    list<Case> lstC = [select Id,Description__c from Case where id in:MapEmailMessage.keyset()];
    for(Case c:lstC){
        if(MapEmailMessage != null && MapEmailMessage.get(c.Id) != null )
            c.Description__c = MapEmailMessage.get(c.Id).HtmlBody;
			newList.add(c);
    }
	if(newList.size()>0)
	{
		update newList;
    }
}


V'NathV'Nath
Thanks for your replay Vinit.

There is no use by creating new list ,I tried your code also but no luck .

But Case trigger is working when I edit case manually and saves the record it is updating EmailMessage Htmlbody  into case description (Custom field).

What else i can do here.


Vinit_KumarVinit_Kumar
Can you debug your code what's coming in the MapEmailMessage.get(c.Id).HtmlBody while running it from Email2Case !!
V'NathV'Nath
Hey Vinit,

I have raise a case for salesforce.com support and they also looking into this past 2 weeks. 

Thanks for your replay.


Kevin McAuliffe 14Kevin McAuliffe 14
V*Nath, did you ever receive an answer for this issue from support?  I have a similar issue.  I suspect that the order of operations is the problem.  If you look at the HtmlBody content, you'll see that the src attribute on the image tag is empty.  So I think that the system creates the HtmlBody content, then creates the image file, then updates the HtmlBody with the hyperlink for the src.  Here is what the rich text looks like in my custom object field:
<img src="" alt="Inline image 1" width="407" height="233"></img>
And here it is in the EmailMessage HtmlBody field (GUID redacted):
<img src="https://c.cs16.content.force.com/servlet/servlet.FileDownload?file=XXXXXXXXXXXXXXX" alt="Inline image 1" width="407" height="233">
So it appears that the src link is perhaps not yet available when the custom field is populated (even though I am doing this in an after insert trigger).

Please let me know if you ever found a solution.

Thanks,
Kevin
NavRamNavRam
I have got very much same issue. Do we have sample trigger to get custom Description rict text field populated with email body?