You need to sign in to do that
Don't have an account?
Harshwardhan 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.)
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.)
to get the file you need to make a second query to the ContentVersion object, using the ContentDocumentId from your first query.
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());
}
// }
}
}
}
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 ?
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());
}
}
}
}