Chirag Mehta
Chirag Mehta

Reputation: 783

"Related Content" stored in which object / How to create "Related Content" records from Apex

If you navigate to account/contact/custom object we do have a related list "related content" (if content is enabled and related list is added to page layout).

My question is were are these "related content" records stored? in which object?

Using apex I'm able to upload file to content version, but not able to create or find the object which stores the "related content" information.

UPDATE

Tried to create a link to show up in "related content" section of account, but no success. Got error " Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, You cannot create a link for this type of entity through the api: [LinkedEntityId]"

Any idea?

           ContentVersion cv = new ContentVersion(
            versionData = EncodingUtil.Base64Decode(base64BlobValue),
            Title = fileName,
            PathOnClient = '/'+fileName,
            FirstPublishLocationId = '058900000009KcL'
        );
        insert cv;

        //fetch ContentDocumentId
        cv = [Select Id,ContentDocumentId from ContentVersion where Id = :cv.Id];

        insert new ContentDocumentLink(LinkedEntityId=parentId,ContentDocumentId=cv.ContentDocumentId,ShareType='V');

** USE CASE **

The use case is to allow user to attach content right from object detail page for eg say Account will have button say Attach Content, this will bring upload content page, once uploaded (i will create contentversion records - this is happening perfectly, no errors) and then I need to relate the uploaded content to account (from which request orginated) ie create "related content" records (here I'm facing difficulty, trying to create contentdocumentlink records but its erroring out).

The use case is just one click to attach content to account or opportunity instead of long current process were user goes to content, uploads there first and then comes back to account/opty and searches content again, and then attaches it to account/contact.

Upvotes: 5

Views: 6675

Answers (2)

jordan.baucke
jordan.baucke

Reputation: 4328

See the ContentDocumentLink specification in the user docs, LinkedEntityId represents:

ID of the linked object. Can include Chatter users, groups, records
(any that support Chatter feed tracking including custom objects),
and Salesforce CRM Content libraries.

I'm thinking that based on that explanation, you can only create the ContentDocumentLink for Chatter based object fields, not for regular sObject records or custom sobjects, etc.

Upvotes: 1

Simon Goodyear
Simon Goodyear

Reputation: 416

As you know the content is stored in the ContentDocument object and the links are stored in the ContentDocumentLink table.

I find that the http://workbench.developerforce.com really useful for figuring out these kinds of relationships.

Upvotes: 2

Related Questions