0

I wan't to create a ContentDocumentLink and delete him in an other function, i tried this code:

// function of creation:
ContentDocumentLink cdl = new ContentDocumentLink(
    ContentDocumentId = doc.Id,
    LinkedEntityId = ws.Id
);
insert cdl;

// function of deletation
delete cdl;

But when I run this function of creation and after function of deletation, I have this error:

Delete failed. First exception on row 0 with id 06A0E000002ea8HUAQ; first error: DEPENDENCY_EXISTS, Cannot complete this operation.
// this part is translate by me:
You can't delete this document of is own library

I found this answer, but I didn't understood how he made to delete the ContentDocumentLink.

0

You can just delete ContentDocumentLink.

There issue that you are facing and what was described in the related question is following:

If you upload document from the code and create first ContentDocumentLink it has ShareType of I and it is primary, which means you can't delete it (you need to delete file instead).

If you have something like 10 ContentDocumentLink, you can easily delete 10 of them (9 of them if 1 of 10 is primary).

Please ask follow up question if something is unclear. Also refer to documentation

3
  • Can I change the primary ContentDocumentLink when I create a new link whit a shareType set to I ? or any other way?
    – Hugo
    Oct 9 '20 at 15:14
  • Do you mean you want to change sharing for the user who uploaded the file?
    – ytiq
    Oct 9 '20 at 15:20
  • No when I create my link a to a library, he is set with I by default (I tried to change it but got an other error), then I create a new one b to an other library and I wan't to a. My question was can I set the link b as primary and then delete a?
    – Hugo
    Oct 9 '20 at 15:43

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.