0

I previously had this code and it was working (ContentDocumentLinks as related list subquery):

List<Id> recordIds = new List<Id>{'0032200000SRusR'};
List<Contact> contacts = [
        SELECT Id, Email,
        (SELECT Id, ContentDocument.LatestPublishedVersion.Title,
                ContentDocument.LatestPublishedVersion.FileExtension,
                ContentDocument.LatestPublishedVersion.VersionData
        FROM ContentDocumentLinks)
        FROM Contact WHERE Id IN :recordIds
];

for (Contact contact : contacts) {
    for (ContentDocumentLink link : contact.ContentDocumentLinks) {
        System.debug(link.Id);
    }
}

But currently it throws error:

13:11:07:000 FATAL_ERROR AnonymousBlock: line 12, column 1

13:11:10:149 FATAL_ERROR System.QueryException: 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.

Which is thrown on for-loop row for (ContentDocumentLink link : contact.ContentDocumentLinks)!

Workaround is clear: retrieve ContentDocumentLink in a separate query with WHERE LinkedEntityId IN :recordIds, but I was wondering, when it stopped working? Is it a known issue? Should I raise a support case for it?

UPDATE: the first comment is the answer, leaving the question for others who should be aware. If there are more than 1 related file attached to the record, the code will fail addressing contact.ContentDocumentLinks. Thank you, Sander de Jong!

2
  • 7
    As far as I can remember, querying on ContentDocumentLink has always had this restriction. Maybe there are more than 1 ContentDocumentLink objects now? Commented Dec 1, 2022 at 11:27
  • Oh, so the problem is in quantity of related records! Thank you! Commented Dec 1, 2022 at 11:35

0

You must log in to answer this question.

Browse other questions tagged .