Im facing below error:
Implementation restriction: ContentDocumentLink requires a filter by a single Id on ContentDocumentId or LinkedEntityId using the equals operator or multiple Id's using the IN operator.
PFB Code:
public class AttachmentController {
@AuraEnabled
public static void updatePicturePath(String recordId){
system.debug('recId '+ recordId);
//In Lightning Experience, Attachments are stored in ContentDocuments
try{
ContentDocumentLink docLink =[SELECT ContentDocumentId
FROM ContentDocumentLink
WHERE LinkedEntityId = :recordId];
//ContentVersion Id uniquely identifies the attachment
ContentVersion ver = [SELECT Id FROM ContentVersion Where
ContentDocumentId = :docLink.ContentDocumentId];
//Update the Picture_Path field with the url of the image
Speaker__c speaker = [SELECT Id FROM Speaker__c WHERE Id = :recordId];
speaker.Picture_Path__c = '/sfc/servlet.shepherd/version/download/'+ ver.Id;
upsert speaker;
}catch(Exception e){
system.debug('In Catch block');
}
}
}
Best Answer
The reason you are getting this error is because you are getting null value in
recordId
parameter (As mentioned by you in your comment).The solution for your problem is to make sure that you pass a valid
Id
parameter to your method from wherever it is being called.Salesforce SOQL ignores the where clause when you filter on required Lookup fields by null. And as LinkedEntityId is a required lookup field on ContentDocumentLink, and you are trying to filter it by
null
, it ignores the entire where clause which leads to no filter at all in SOQL and that leads to below error.SOQL ignores : WHERE RequiredLookup__c = null is the idea on community, you can promote it, although in your case LinkedEntityId should always have a value, just that you are not passing the parameter properly.
ContentDocumentLink
.ContentDocument
fields if you're filtering byContentDocumentId
. You can only filter on ContentDocument fields if you're filtering byLinkedEntityId
.You can find further help on
ContentDocumentLink
at this link