function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aidel BruckAidel Bruck 

query on contentdocumentlink returning wrong results

I am having trouble with a query on contentdocumentlink.
I upload a document to an account. Then I query the contentdocumentlink to get the linkentityid and I get a different id.
Any idea why this is happening. 
Here is the test class and the query.

query from trigger:

list<account> a= new list<account>();
 list<ContentDocumentlink> doc= [select id, LinkedEntityId from contentdocumentlink where contentdocumentid=: newdoc.ContentDocumentId];
            A= [select name, id from account where id=: doc[0].LinkedEntityId];

This is the test code
public class TestNewFileAlert 

    public static testmethod void MyUnitTest()
           Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        User futureUser = new User(firstname = 'Future', lastname = 'User',
                                   alias = 'future', defaultgroupnotificationfrequency = 'N',
                                   digestfrequency = 'N', email = '',
                                   emailencodingkey = 'UTF-8', languagelocalekey='en_US', 
                                   localesidkey='en_US', profileid = p.Id, 
                                   timezonesidkey = 'America/Los_Angeles',
                                   username = '',
                                   userpermissionsmarketinguser = false,
                                   userpermissionsofflineuser = false, userroleid= null);
            Account acct = new Account(Name='TEST_ACCT');
            insert acct;
            ContentVersion contentVersion = new ContentVersion
                Title = 'Penguins',
                PathOnClient = 'Penguins.jpg',
                VersionData = Blob.valueOf('Test Content'),
                IsMajorVersion = true
            insert contentVersion; 
            List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
            //create ContentDocumentLink  record 
            ContentDocumentLink cdl = New ContentDocumentLink();
            cdl.LinkedEntityId =;
            cdl.ContentDocumentId = documents[0].Id;
            cdl.shareType = 'V';
            insert cdl;

SandhyaSandhya (Salesforce Developers) 

You might have deleted the document in classic.
I would suggest you refer below help article to know 
ContentDocument and ContentDocumentLink trigger behavior in Classic and in Lightning.
Best Regards,
Aidel BruckAidel Bruck
I can't even uplod the file. I get the error before it is even uploaded
Tyler McCartyTyler McCarty
LinkedEntityID returns the ID of the User, not the ID of the parent Account