You need to sign in to do that
Don't have an account?
Darshit Pathak 3
INVALID_CROSS_REFERENCE_KEY exception in ContentDocumentLink
below is my code in test class in which I am getting the exception.
Can anyone help me with root cause and solution?
ContentVersion cv = new ContentVersion();
cv.Title = 'Test Content';
cv.PathOnClient = 'test';
cv.VersionData = EncodingUtil.base64Decode('Unit Test Attachment Body');
cv.NetworkId = // customer community network id;
insert cv;
Id contentDocumentId = [select Id, ContentDocumentId, OwnerId from ContentVersion where Id = :cv.Id].ContentDocumentId;
List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument where ID =:contentDocumentId];
System.assertEquals(documents.size(), 1);
System.debug('DocId : '+contentDocumentId);
ContentDocumentLink cdl = New ContentDocumentLink();
cdl.LinkedEntityId = caserec.id; // id of a case record inserted earlier in the method
cdl.ContentDocumentId = contentDocumentId;
cdl.shareType = 'V';
insert cdl; // getting exception on this line.
Can anyone help me with root cause and solution?
ContentVersion cv = new ContentVersion();
cv.Title = 'Test Content';
cv.PathOnClient = 'test';
cv.VersionData = EncodingUtil.base64Decode('Unit Test Attachment Body');
cv.NetworkId = // customer community network id;
insert cv;
Id contentDocumentId = [select Id, ContentDocumentId, OwnerId from ContentVersion where Id = :cv.Id].ContentDocumentId;
List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument where ID =:contentDocumentId];
System.assertEquals(documents.size(), 1);
System.debug('DocId : '+contentDocumentId);
ContentDocumentLink cdl = New ContentDocumentLink();
cdl.LinkedEntityId = caserec.id; // id of a case record inserted earlier in the method
cdl.ContentDocumentId = contentDocumentId;
cdl.shareType = 'V';
insert cdl; // getting exception on this line.
Such errors popup only when you try to update a field with wrong values. Therefore please debug caserec.id and contentDocumentId just before updating them to LinkedEntityId and ContentDocumentId and check wheather record corresponding to these ids exists.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Greetings to you!
- Please use the below code : -
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha.
System.debug('DocId : '+contentDocumentId); prints the document Id , and caseRec Id is also gets printed .
I tried your way too, still the error is same.