From the docs I've successfully been able to create a document in Salesforce using Inserting a New Document. I'd like to be able to link this document to multiple cases. From what I understand, it appears I need to use ContentDocument and ContentDocumentLink objects to achieve this.

How can this be achieved with the rest api? I see they have an example for how to create a ContentVersion in the docs, but they don't explain how to create a ContentDocument and get its Id to associate it to the ContentVersion

1 Answer 1


ContentDocument is not directly created - if you submit a ContentVersion without setting ContentDocumentId, it will create a new ContentDocument. You won't get the ContentDocument's Id in the response, so you'd need to retrieve or query the new ContentVersion to get the ContentDocument's Id for creating ContentDocumentLink records.

But, when creating a new ContentDocument in this way, you can also create an initial ContentDocumentLink by way of the FirstPublishLocationId field. From the Object Reference:

Setting FirstPublishLocationId allows you to create a file and share it with an initial record/group in a single transaction, and have the option to create more links to share the file with other records or groups later. When a file is created, it’s automatically linked to the record, and PublishStatus will change to Public from Pending/Personal.

This field is only set the first time a version is published via the API. FirstPublishLocationId can’t be set to another ID when a new content version is inserted.

  • do you know what example values are used in PathOnClient or ContentUrl? I'm using a rest api client, Postman, and I don't know what to put.
    – Tyler Zika
    May 16, 2019 at 17:24
  • 1
    If you're uploading a binary file, use PathOnClient and just put the full filename, with extension. The extension drives detection of the file type. Despite the name, Salesforce doesn't actually care about the directory structure of the uploading system. May 16, 2019 at 17:27
  • 1
    +1. On this -- "I'm not sure (and not able to quickly test) whether the ContentDocument's Id would be included in the response in that case.", the response actually doesn't return it. It only returns the id of the newly created contentversion.
    – Jayant Das
    May 16, 2019 at 17:33
  • 1
    @ThomasTaylor that worked, thanks! Do you have any ideas why the preview for my file is not working? Does preview only work for pdfs, or should preview also work for text files?
    – Tyler Zika
    May 16, 2019 at 17:43
  • 1
    I updated the answer to reflect Jayant's info and how to get the ContentDocument Id for additional links. This help article suggests that text files should get previews, if PathOnClient had a .txt extension, and the file is less than 524 KB. May 16, 2019 at 18:03

You must log in to answer this question.

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