1

Hello I am having problems to create a ContentDocument with Apex code. I am trying to create those records from a partner user community that has some permission set. I get the next error when trying to insert a list of ContectDocuments: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

@AuraEnabled
public static String sendNotification(String filesToSend, String[] listMembers, String mensaje){
    System.debug('filesToSend '+ filesToSend);
    System.debug('members '+listMembers);
    System.debug('mensaje '+ mensaje);
    List<FileDataWP> files = (List<FileDataWP>) JSON.deserialize(filesToSend, List<FileDataWP>.class);

    List<Account> membersList = new List<Account>();
    membersList = [SELECT Id, Name, Email, Phone FROM Account WHERE Id IN: membersList];

    List<Notification> newListNotification = new List<Notification>();

    try {
        if(String.isNotBlank(filesToSend)) {
            List<ContentVersion> cvList = new List<ContentVersion>();
            List<ContentDocumentLink> cdList = new List<ContentDocumentLink>();

                for(FileDataWP file : files) {
                    ContentVersion conVer = new ContentVersion();
                    conVer.ContentLocation = 'S'; // S = Salesforce, E = External Files
                    conVer.VersionData = EncodingUtil.base64Decode(file.fileContent);
                    conVer.Title = file.fileName;
                    conVer.PathOnClient = file.fileName;
                    cvList.add(conVer);
                }
                insert cvList;


            List<ContentVersion> listCV = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =: cvList];
            for(Account iAccount: membersList){
                Notification iNotification = new Notification();
                iNotification.Member__c = iAccount.Id;
                iNotification.Message__c = mensaje;
                iNotification.NotificationType__c = 'S';
                newListNotification.add(iNotification); 
            }
            insert newListNotification;


            for(Notification iNotifiation : newListNotification){
                for(ContentVersion  cv : listCV) {
                    ContentDocumentLink conDocLink = New ContentDocumentLink();
                    conDocLink.ContentDocumentId = cv.ContentDocumentId;
                    conDocLink.Visibility = 'AllUsers';
                    conDocLink.LinkedEntityId = iNotification.Id;
                    conDocLink.shareType = 'V'; // V = Viewer, C = Collaborator, I = Inferred
                    cdList.add(conDocLink);
                }
            }

            update newListNotification;
            insert cdList;   
            return 'success';     
        }else{
            return 'Error';
        }
        
    } catch (Exception ex) {
        System.debug(ex.getMessage() + ' -> ' + ex.getStackTraceString());
        throw new AuraHandledException(ex.getMessage());
    }
    
}

The error is in this part insert cvList; couple of days ago this worked fine but not it is broken. I have not change it. Event when I go to the Salesforce and try to delete the record I get a pop message telling me: enter image description here

Could anybody help me to fix it? What am I doing wrong?do I have to enable something in the Permission set?

I got the next print in the consosole:

14:27:24.6 (14434220)|USER_DEBUG|[70]|DEBUG|
 filesToSend [{"fileName":"Captura de pantalla 2022-10-31 a las 12.35.19.png",
               "fileContent":"iVBORw0KGgoAAA..."}]
8
  • (1) does the JSON include a value for the fileName field? ; (2) see this help article
    – cropredy
    Feb 6 at 16:32
  • @cropredy hello yes I have the name file. Actually I updated the post to show what came in the filesToSend
    – TechGuy
    Feb 7 at 9:01
  • and the error is on insert cvList not insert cdList? Invalid xref Id usually means running user doesn't have access to a lookup field
    – cropredy
    Feb 7 at 17:02
  • @TechGuy I am facing the same issue can you tell us how it's fixed and what permission need to be added?
    – SFDC
    Mar 8 at 13:44
  • @SFDC can you try setting NetworkId on contentversion like this Id networkId = [SELECT Id from Network where name = 'Your Community Name'].Id; then assign cv.NetworkID = networkId;
    – Shamina
    Mar 8 at 18:50

0

You must log in to answer this question.

Browse other questions tagged .