I've been trying to make a fix for a known error when logging emails from the Outlook integration where embedded images display as broken links in the feed. My solution for this would be a trigger that edits the HtmlBody of the relevant EmailMessage, inserting the url of the locally stored image file.

My trigger fires after insert of a new ContentDocumentLink, with the end goal being updating the relevant EmailMessage. Using logs from the dev console, I know this code fetches the correct Email and attachment Ids. However, when the EmailMessage is updated, I receive the following error:

13:31:55:073 EXCEPTION_THROWN [31]|System.DmlException: Update failed. First exception on row 0 with id 02s5C000000w4bEQAQ; first error: INVALID_OPERATION, operation is not allowed: []

At the moment, you will see that the Subject of the Email is being edited. Eventually this would be the HtmlBody, but I've been testing multiple fields to see if was a specific field edit that was causing the error. Unfortunately, it seems that no matter what field I edit on the email the error occurs.

public class ContentDocumentHandler {
    public static void handle(List<ContentDocumentLink> links) {
        List<EmailMessage> updateEmails = new List<EmailMessage>();
        for(ContentDocumentLink cdl: links) {
            Id leid = cdl.LinkedEntityId;
            Id cdid = cdl.ContentDocumentId;                 
            List<EmailMessage> em = [SELECT Id, HtmlBody FROM EmailMessage WHERE Id =: leid limit 1];
            if(!em.isEmpty()) {   
                for(EmailMessage email: em) {
                List<ContentDocument> condocs = [SELECT Id From ContentDocument WHERE Id =: cdid limit 1];
                Set<Id> contentIds = new Set<Id>();
                for (ContentDocument cont: condocs) {
                List<ContentVersion> cvlist = [SELECT Id,Title FROM ContentVersion WHERE ContentDocumentId IN :contentIds];
                String prefix = '/sfc/servlet.shepherd/version/download/';
                String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
                for(ContentVersion cv: cvlist) {
                        String url = baseUrl + prefix + cv.Id;
                        updateEmails[0].Subject = 'Hello';
            update updateEmails;

EmailMessage can be updated only when it's in the Draft status.

update() is supported when an email record is in Draft status, and IsPrivateDraft is false. It is also supported if Status and IsPrivateDraft are true and CreatedBy is associated with the current user. When the email record status is not in Draft status, the IsExternallyVisible field and custom fields only can be updated.

So that would be what's preventing you from editing the emails. I haven't done email integration myself so I don't know if creating new EmailMessage records, copying the fields and removing old ones would work for you but that's one possibility.

  • This was the issue! Appreciate the help
    – user83559
    Jul 10 '20 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.