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
Harshwardhan Singh KarkiHarshwardhan Singh Karki 

No Records are being returned when fetching the records from content document in task trigger?

Does contentDocumentLink and contentDocument return no record when used in apex as in my query it returns no records.

Query -
list<ContentDocumentLink> cdList = [Select Id, ContentDocumentId, LinkedEntityId From ContentDocumentLink Where LinkedEntityId = :taskId ];
taskId is the triggered record

Note* - How can i fetch file id which is attached while task is saved ?(For this i did go through Content Document and Content Document Link but unable to fetch the records, So is there any limitation to this.)
chrischris
Hi,

to get the file you need to make a second query to the ContentVersion object, using the ContentDocumentId from your first query.
 
SELECT Id, Title, VersionData, PathOnClient FROM ContentVersion WHERE ContentDocumentId = cdList[0].ContentDocumentId AND IsLatest = true


 
Harshwardhan Singh KarkiHarshwardhan Singh Karki
The first query is returning 0 records so how could we filter 2nd query using the id from first query..Below is my code in which i am unable to fetch the file id due to the query is returning no records , it is being called through trigger and thus it shows error of list exception ..

Code

Class -

public  with sharing class RelateFileToObjectHandler {
    public static void getFileId(Task taskRec){
        if(taskRec <> NULL){
            string taskId = taskRec.id;
            list<ContentDocumentLink> cdList = [Select Id, ContentDocumentId, LinkedEntityId From ContentDocumentLink Where LinkedEntityId = :taskId ];
            system.debug(cdList);
           // if(cdList.size() > 0){
                String cdId= cdList[0].ContentDocumentId;
                
                ContentDocumentLink cdLinkRec = new ContentDocumentLink();
                cdLinkRec.ContentDocumentId = cdId;
                cdLinkRec.LinkedEntityId = taskRec.WhoId;
                try{
                    insert cdLinkRec;
                }
                catch(Exception e){
                    system.debug('Error is '+ e.getMessage() +' at line number'+ e.getLineNumber());
                }
           // }
            
        }
    }
}
chrischris
Ok can you give more details on this  Task trigger, is it before insert/update, or after/insert/update ?

Also can you share the code in charge of creating the Content Document ?

What makes you think that at the time the task trigger runs, the Content Document record exists in the database ?
Harshwardhan Singh KarkiHarshwardhan Singh Karki
Trigger is on After Insert in both task as well as content document
For now i am doing it with Content Document still, it does not return any list of records either through content Version or ContentDocumentLink

yes i too though about the same whether file is being save first or not then i did the trigger on content  document too but again no records were return when done in content document too but it is not fetching record in content document too for contentVersion also (returning 0 records)

public with sharing class RelateAttachmentIdToObjectHandler {
    public static void getFileId(ContentDocument fileRec){
        if(fileRec <> NULL){
            system.debug(fileRec.Id);
            String cdvId = [SELECT Id, Title, VersionData, PathOnClient FROM ContentVersion WHERE ContentDocumentId = :fileRec.Id AND IsLatest = true].Id;
            system.debug(cdvId);
            list<ContentDocumentLink> cdList = [Select Id, ContentDocumentId, LinkedEntityId,TYPEOF LinkedEntity WHEN Task THEN Id , Subject END From ContentDocumentLink Where ContentDocumentId = :fileRec.Id];
            system.debug(cdList);
            string taskId = cdList[0].linkedEntityId;
            system.debug(taskId);
            Task taskRec= [Select Id, Subject , WhoId From Task Where Id = : taskId];
            ContentDocumentLink cdLinkRec = new ContentDocumentLink();
            cdLinkRec.ContentDocumentId = fileRec.Id;
            cdLinkRec.LinkedEntityId = taskRec.WhoId;
            try{
                insert cdLinkRec;
            }
            catch(Exception e){
                system.debug('Error is '+ e.getMessage() +' at line number'+ e.getLineNumber());
                
            }
        }
      }
        
    }