I am trying to upload a new revision of a file in ContentVersion object programmatically by Apex. I am getting the below error while inserting a new record of ContentVersion.

15:21:52:424 EXCEPTION_THROWN [13]|System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Document with ID: 0693i000000ERjc is already linked with the entity with ID: 0013i000002GYXM: Linked Entity ID: [LinkedEntityId]

As per my code, I am passing the ContentDocumentId and ReasonForChange so that it creates a new revision of it, but this is not working. Any pointers will be helpful.

ContentVersion vContentVersion = new ContentVersion();
vContentVersion.VersionData = EncodingUtil.base64Decode(EncodingUtil.urlDecode(base64Data, 'UTF-8'));
vContentVersion.Title = fileName;
vContentVersion.PathOnClient = fileName;
vContentVersion.ContentDocumentId = contentDocumentId;
vContentVersion.ReasonForChange = 'Demo';
insert vContentVersion;

Also in the error message, 0013i000002GYXM is the accountId.

  • Do you have a trigger on ContentVersion or ContentDocument? – David Reed Aug 10 '19 at 0:36
  • Do. I didn't create any trigger. Do I have to? – snl Aug 10 '19 at 0:44
  • Can you show us the full stacktrace, or at least line number, of this error? Are you inserting a ContentDocumentLink? – David Reed Aug 10 '19 at 1:36
  • It's giving an error while inserting vContentVersion. Do I need to insert a record in ContentDocumentLink before inserting ContentVersion? – snl Aug 10 '19 at 2:03
  • 1
    I'm asking for more context here because this error doesn't appear to come from the code you have shown us. LinkedEntityId is a field on ContentDocumentLink. So there is clearly more code involved, either here or in some other automation, that creates a link to an Account record. – David Reed Aug 10 '19 at 2:11

Your Answer

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

Browse other questions tagged or ask your own question.