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.

  1. Is there anything missing the approach of cloning EmailMessage?
  2. Can someone help me to understand how salesforce identify to create attachment record for an EmailMessage contains image in the htmlbody?

Edit Added logic in future method to clone attachments. Attachements are cloned and linked to new EmailMessages. But images are still shown as broken in the new Email body as the image url is not updating with attachment url in Email Htmlbody. Adding future method logic for reference.

private static void cloneAttachments(Map<Id,Id>mapOldNewMailIds){
    Attachment clonedAttachment;
    List<Attachment> listNewAttachments = new List<Attachment>();
    for(Attachment oAttachment : [SELECT Id, Body, ContentType, Description, IsPrivate, Name, ParentId FROM Attachment WHERE ParentId IN :mapOldNewMailIds.keySet()]){
        clonedAttachment = oAttachment.clone(false,true,false,false);
        clonedAttachment.ParentId =mapOldNewMailIds.get(oAttachment.ParentId);
      List<Database.SaveResult> listDSR = Database.insert(listNewAttachments,FALSE);

I tried updating HtmlBody by forming Attachment download url, but looks like HtmlBody is not updatable and update action is failing with

first error: INVALID_OPERATION, operation is not allowed: []

Any other better solution approach to handle this scenario?

Thanks in advance.

  • The actual emailMessage attachment is saved in a separate object - depending on how you have configured Email2Case - either as Attachment or as a ContentDocumentLink/ContentDocument. Query your existing system to see a real example and that will give you the approach to take
    – cropredy
    Jun 17, 2021 at 16:07
  • I have added a logic to clone attachment and updated ParentId with cloned EmailMessage Ids. Now I can see the image in the related attachment list. But image is not loading in email message and src attribute of img tag is still as src="cid:ii_kq0csi6n2".
    – sf dev
    Jun 21, 2021 at 7:57
  • please use edit and show your code
    – cropredy
    Jun 21, 2021 at 16:34
  • I have updated the code I tried to clone attachments. Thanks
    – sf dev
    Jun 22, 2021 at 3:32

1 Answer 1


I have achieved this by moving cloning EmailMessage and Attachment logic to future method by passing emailmessage ids into future method. So that original Attachments will be created and updated HtmlBody with attachment url. In the future method, added logic to clone the EmailMessage and Attachments and replacing HtmlBody of cloned EmailMessage with new Attachment Ids.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .