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:
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..."}]
fileName
field? ; (2) see this help articleinsert cvList
notinsert cdList
? Invalid xref Id usually means running user doesn't have access to a lookup fieldId networkId = [SELECT Id from Network where name = 'Your Community Name'].Id;
then assigncv.NetworkID = networkId;