[SalesForce] ContentDocumentLink filter not working- Trailhead Conference Management App

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.

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.

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.

  • You can't run a query without filters against ContentDocumentLink.
  • You can't filter on ContentDocument fields if you're filtering by ContentDocumentId. You can only filter on ContentDocument fields if you're filtering by LinkedEntityId.
  • You can't filter on the related object fields. For example, you can't filter on the properties of the account to which a file is linked. You can filter on the properties of the file, such as the title field.

You can find further help on ContentDocumentLink at this link

Related Topic