0

I am encountering the following error when inserting a ContentDocumentLink record :

Error [statusCode=INVALID_CROSS_REFERENCE_KEY, code=[xmlrpc=1202, statusCode=INVALID_CROSS_REFERENCE_KEY, exceptionCode=INVALID_CROSS_REFERENCE_KEY, scope=PublicApi, http=400]

The insert operation is in an After Insert trigger on the object ContentDocument, I am trying to link the newly created ContentDocument with an existing Account. I already checked the following options :

  • Checked that the Account Id in the LinkedEntityId fiel, and the ContentDocument Id in the ContentDocumentId field of ContentDocumentLink record are valid Salesforce Ids;
  • The file is correctly uploaded and visible in Salesforce;
  • I also tried with Ids in static strings of the existing Account and an already existing ContentDocument, and it didn't throw this error. I know that it is possible to create a ContentDocumentLink when a ContentDocument is inserted, since I already implemented it in another project but I no longer have access to the code.

Any tips on what I am missing would be greatly appreciated. Thank you.

EDIT #1 : Trigger Handler code

List<ContentDocumentLink> doclinksToCreate = new List<ContentDocumentLink>();
   Map<String, String> docIdsToAccountIds = new Map<String, String>();
    Map<String, String> userIdsToDocIds = new Map<String, String>();
    for(ContentDocument oneDoc : docs){
        userIdsToDocIds.put(oneDoc.CreatedById, oneDoc.Id);
    }

    // Get a list of AccountIds related to portal users that created ContentDocuments
    List<User> portalUsers = [SELECT Id, AccountId, Account.Name, Profile.Name 
                            FROM User 
                            WHERE Id IN :userIdsToDocIds.keySet()
                            AND Profile.Name = :System.Label.TheProfileName];

    Set<String> accountIds = new Set<String>();
    for(User oneUser : portalUsers){
        docIdsToAccountIds.put(userIdsToDocIds.get(oneUser.Id), oneUser.AccountId);
    }

    for(String key : docIdsToAccountIds.keySet()){
        doclinksToCreate.add(new ContentDocumentLink(
            ContentDocumentId = key, //'0693M000000fhOcQAI'
            LinkedEntityId = docIdsToAccountIds.get(key), // '0013M000018llyNQAQ'
            ShareType = 'I',
            Visibility = 'AllUsers' 
        ));
    }

    Database.SaveResult[] results = Database.insert(doclinksToCreate, false);
    Database.SaveResult oneRes;
    List<String> accountsIdsToNotifyAbout = new List<String>();
    for(Integer i=0, size = results.size(); i<size; i++){
        oneRes = results[i];
        if(oneRes.isSuccess()){
            System.debug('Insert document link success for document with Id : '+doclinksToCreate[i].ContentDocumentId);
            accountsIdsToNotifyAbout.add(doclinksToCreate[i].LinkedEntityId);
        }else{
            System.debug('Error with document Id : '+doclinksToCreate[i].ContentDocumentId);
            for(Database.Error oneError : oneres.getErrors()){
                System.debug(oneError);
            }
        }
    }
5
  • Please add the complete error message, there is a message attribute which is missing.
    – Saroj Bera
    Commented Jul 2, 2022 at 19:33
  • does the running user have viz to the Account?
    – cropredy
    Commented Jul 2, 2022 at 21:42
  • @Saroj Bera the message attribute doesn't add much details it says : "invalid cross reference id"
    – THEDJA
    Commented Jul 4, 2022 at 17:54
  • @cropredy the user doesn't have viz to the account, but it shouldn't be the cause because the trigger handler runs "without sharing" and the test I did with using an existing ContentDocument with a static Id worked without errors on the same Account.
    – THEDJA
    Commented Jul 4, 2022 at 17:55
  • @THEDJA - Please edit the Question and add some relevant code so it helps the community to find an answer.
    – Saroj Bera
    Commented Jul 4, 2022 at 18:05

2 Answers 2

1

I ended up changing the logic behind the ContentDocumentLink creation in order to solve the problem. I wrote a trigger on after insert of a ContentVersion instead of a ContentDocument, then I query for the ContentDocumentId.

It had nothing to do with certain fields being null or access to apex classes and to objects. I guess the issue comes from the ContentDocument record not being committed to the database yet in an after insert trigger (Salesforce order of execution) even if the record Id is available.

0
 public Static void insertContentDocumentLink(String entityId , String contentdocumentid){
        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId=entityId;
        contentlink.contentdocumentid=contentdocumentid;//[select contentdocumentid from contentversion where id =: objVersion.id].contentdocumentid;
        contentlink.ShareType = 'I';
        contentlink.Visibility = 'AllUsers'; 
        insert contentlink;
    }
6
  • 1
    Thanks for your contribution. This answer could be improved by adding some explanation and describing how this code helps in resolving the question.
    – Shamina
    Commented Jul 2, 2022 at 19:26
  • Thank you but that didn't help solve the problem.
    – THEDJA
    Commented Jul 4, 2022 at 18:04
  • can you share your trigger code on ContentDocument Commented Jul 4, 2022 at 18:10
  • I updated the post with the trigger handler code
    – THEDJA
    Commented Jul 4, 2022 at 18:22
  • SELECT Id, AccountId, Account.Name, Profile.Name FROM User WHERE Id IN :userIdsToDocIds.keySet() AND Profile.Name = :System.Label.TheProfileName can you add AccountId!=NULL? can you check any triggers on Content Document Link ? Seems it's only for community users right? can you check respected classes, and object permission to that user? Commented Jul 4, 2022 at 18:45

You must log in to answer this question.

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