3

I'm trying to link attachment files to Salesforce EmailMessages. Steps so far:

  1. Upload attachment file by posting to /services/data/v58.0/sobjects/ContentVersion.
  2. Query ContentVersion table to get corresponding ContentDocumentId, e.g., <doc_id_1>
  3. Post to /services/data/v58.0/sobjects/ContentDocumentLink with json body
{
  "LinkedEntityId": <email_message_id_1>,
  "ContentDocumentId": <doc_id_1>,
}

At this point when I navigate to view /EmailMessage/<email_message_id_1>/view in browser I see the file attached. The issue is I want to verify the attachments with the API. If I query

services/data/v58.0/query?q=SELECT+Id,+ContentDocumentId,+LinkedEntityId+from+ContentDocumentLink+WHERE+LinkedEntityId=\'<email_message_id_1>\''

I get no results, but when I repeat the POST in step 3 above I get a 400 response Document with ID: <doc_id_1> is already linked with the entity with ID: <email_message_id_1>. And to make matters weirder, if I query

services/data/v58.0/query?q=SELECT+Id,+ContentDocumentId,+LinkedEntityId+from+ContentDocumentLink+WHERE+ContentDocumentId=\'<doc_id_1>\''

I get one record linking the content document to LinkedEntityId=<user_id_1> where <user_id_1> is the user I'm making the API calls as.

Any idea why the ContentDocumentLink connecting the document to the email message isn't being returned?

5
  • 2
    does the running user have Salesforce CRM Content permission or Query All Files permission. Note this Help article
    – cropredy
    Oct 3, 2023 at 17:57
  • The user has both Modify All Data and Query All Files
    – Alex
    Oct 3, 2023 at 19:56
  • long shot -- Ids are case sensitive, a copy-paste error? or, a 15 vs 18 character Id issue
    – cropredy
    Oct 3, 2023 at 22:38
  • Turns out the ContentDocumentLink objects aren't coming back because the EmailMessage object has Status=5 (draft). If I patch the EmailMessage to Status=3 (sent), the same exact query to the ContentDocumentLink table for entries with LinkedEntityId=<email_message_id_1> starts returning the expected links.
    – Alex
    Oct 4, 2023 at 0:56
  • Since the attachment files are visible on the email message in browser while it's in draft, there must be some piece of data connecting them. Is it the case that the ContentDocumentLink entries exist already but are being filtered out for some reason? Or is there some other object type linking the document and email when the email is in draft? And in the latter case, what object is linking them?
    – Alex
    Oct 4, 2023 at 0:58

0

You must log in to answer this question.