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
Bhuvaneswary RadhakrishnanBhuvaneswary Radhakrishnan 

EmailMessage with image

I have a logic in EmailMessage trigger to create a new case when customer replies to a closed case. If the case is closed, I am cloning the case record to create new case. Since we cannot reparent EmailMessage I am cloning the EmailMessage as well and updating ParentId as new case id.
I am facing issue when the email has attachments (images).
Original EmailMessage has below values: HasAttachment=true and HtmlBody contains <img src="cid:ii_kq0csi6n2" alt="image.png" width="225" height="225">
I am using emailMessage.clone(false, true, false, false) to clone the record and the cloned record has same values as original which are HasAttachment=true and HtmlBody contains <img src="cid:ii_kq0csi6n2" alt="image.png" width="225" height="225">
After inserting cloned record, when I check onbeforeinsert execution for cloned record, HasAttachment becomes false and it doesn't create any attachment for cloned EmailMessage record. Cloned EmailMessage img tag in HtmlBody still have src attribute as "cid:ii_kq0csi6n2" instead FileDownload url of attachment record.
Is there anything missing the approach of cloning EmailMessage?
Can someone help me to understand how salesforce identify to create attachment record for an EmailMessage contains image in the htmlbody?
Thanks in advance.
ShivankurShivankur (Salesforce Developers) 
Hi Bhuvaneswary,

This looks to be working as designed, since as per the standard documentation (https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_emailmessage.htm) HasAttachment indicates whether the email was sent with an attachment (true) or not (false).

You may want to add some more logic to your customization to query on content and get the attachements related to it and programtically copying them to the newly created case and email message.

Look at similar discussions/examples around this over community:
https://salesforce.stackexchange.com/questions/311561/emailmessage-hasattachment-and-files
https://developer.salesforce.com/forums/?id=906F00000008tf3IAA
https://salesforce.stackexchange.com/questions/98248/cloning-attachment-from-email-object-to-case-object

Hope above information helps. Please mark as Best Answer so that it can help others in future.

Thanks.
Bhuvaneswary RadhakrishnanBhuvaneswary Radhakrishnan
Hi Shivankur,
I have added logic to clone attachment and update parent Id with new cloned emailmessage ids. But still the image is not updating with actual url. I am doing this in future method. Do we need to update src attribute of img tag with actual one?