+ 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 = 'test@test.org',
                                   emailencodingkey = 'UTF-8', languagelocalekey='en_US', 
                                   localesidkey='en_US', profileid = p.Id, 
                                   timezonesidkey = 'America/Los_Angeles',
                                   username = 'futureasdasdasuser@test.org',
                                   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; 
            Test.setCreatedDate(contentVersion.Id, DateTime.now());
            List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
            //create ContentDocumentLink  record 
            ContentDocumentLink cdl = New ContentDocumentLink();
            cdl.LinkedEntityId = acct.id;
            cdl.ContentDocumentId = documents[0].Id;
            cdl.shareType = 'V';
            insert cdl;
            Test.setCreatedDate(cdl.Id, DateTime.now());

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