1

I'm working on one scenario update description value in ContentDocument Object.

This my piece of code.

//Update Description value in ContentDocument object
        List<ContentDocument> lstcd = new List<ContentDocument>();
        for(ContentDocumentLink cdl: [SELECT Id, ContentDocument.Description, ContentDocumentId, ContentDocument.LatestPublishedVersionId FROM ContentDocumentLink WHERE LinkedEntityId = '500N0000002hZC9'])
        {
            ContentDocument cd = new ContentDocument();
            cd.id = cdl.ContentDocumentId;
            cd.Description = 'Testing';
            lstcd.add(cd);
        }
        
        update lstcd;
   

But I'm getting this Error:

Compile Error: Field is not writeable: ContentDocument.Description

How do I fix this error?

2 Answers 2

8

The content data model is slightly different than other objects in salesforce.

The way it works is that each document is represented as a ContentDocument. Each ContentDocument contains one or more ContentVersion objects (depending on how many versions of a given document have been uploaded). When you are query some fields on ContentDocument, like description, you are just pulling the description from the latest ContentVersion object. To determine which ContentVersion is the latest for a given document you can look at the LatestPublishedVersionId field.

Given this, your updated code would look like:

List<ContentVersion> versions = new List<ContentVersion>();

for(ContentDocumentLink cdl: [SELECT Id, ContentDocument.Description, ContentDocumentId, ContentDocument.LatestPublishedVersionId FROM ContentDocumentLink WHERE LinkedEntityId = '500N0000002hZC9']) {
      ContentVersion version = new ContentVersion();
      version.id = cdl.ContentDocument.LatestPublishedVersionId;
      version.description = 'Testing';
      versions.add(version);
}

update versions;
0
3

seems via apex it is only nillable

Properties

Filter, Nillable, Sort

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentdocument.htm

I believe you have to create / modify content version:

https://developer.salesforce.com/docs/atlas.en-us.sfFieldRef.meta/sfFieldRef/salesforce_field_reference_ContentVersion.htm

7
  • Yes, you are right this field is nillable this means we change have the Description field without any value. But it don't tell whether it is editable or not.
    – user10286
    Commented Nov 4, 2015 at 5:54
  • i do not think you can nill it either directly. It is all done via the contentdocumentversion - It has a nillable property so if the version description is nill it can nill the document description.
    – Eric
    Commented Nov 4, 2015 at 5:57
  • But if I go via Chatter File UI, I can edit the Description. Is there any other way to edit this field in Apex?
    – user10286
    Commented Nov 4, 2015 at 6:04
  • because behind the scenes it is updating the content version
    – Eric
    Commented Nov 4, 2015 at 6:05
  • Hope I can add record in content version via apex code is that right?
    – user10286
    Commented Nov 4, 2015 at 6:15

You must log in to answer this question.

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