0

I am running a SOQL query on contentversion to fetch files that are attached to opportunity. However, I am noticing that it is even pulling the notes attached to the opportunity as well. Is there a way I can run a filter that does not fetch the notes and only the ones that are under Files? Do I need to create libraries or add some sort of filter in my SOQL query to achieve this?

Set<Id> contentDocIds = new Set<Id>();

for (ContentDocumentLink cdl : [SELECT id, LinkedEntityId, ContentDocumentId 
FROM ContentDocumentLink WHERE LinkedEntityId IN (SELECT Id FROM Opportunity 
WHERE ......) and LinkedEntity.Type='Opportunity']) 
{  contentDocIds.add(cdl.ContentDocumentId); }

List<ContentVersion> cvList = [select id, Checksum, ContentBodyId, 
ContentDocumentId, ContentLocation,...... FROM ContentVersion WHERE 
ContentDocumentId IN :contentDocIds AND IsLatest = 
true]; 

Thanks

1

Answer 1:

You can filter by FileType of SNOTE WHERE FileType != 'SNOTE'

Answer 2:

Use the AttachedConentDocuments related list on the object. Below is an example of how you might use this inside of a trigger:

List<Opportunity> opportunities = [
    SELECT Id, (
        SELECT ContentDocumentId, LinkedEntityId
        FROM AttachedContentDocuments
    )
    FROM Opportunity
    WHERE Id IN :Trigger.new
];

Map<Id, ContentVersion> documentIdToContentVersion = new Map<Id, ContentVersion>();

for (Opportunity opp : opportunities) {
    for (AttachedContentDocument acd : opportunities.AttachedContentDocuments) {
        documentIdToContentVersion.put(acd.ContentDocumentId, null);
    }
}


for (ContentVersion cv : [
    SELECT Id, ... 
    FROM ContentVersion 
    WHERE ContentDocumentId IN :documentIdToContentVersion.KeySet()
]) {
    documentIdToContentVersion.put(cv.ContentDocumentId, cv);
}

Extra info

I learned something new while digging around. Here is the documentation on this object.

I found this while digging around trying to figure out how the ContentNote object worked. It bugged me that this ContentNote existed but didn't seem to actually have any association to a content document (the ContentNote key prefix is 069 ContentDocument is 068)

Its worth noting that there is a similar AttachedContentNotes relationship as well if you want to only work with notes. It also has an AttachedContentNote object that has a WSDL exactly matching the ContentDocumentLink object.

A last note, there's a good chance you don't even need the ContentVersion object unless you need to actually access the VersionData. Most of the fields are already on the content document. When Querying the ContentVersion table, I have noticed that whether or not you a query the VersionData field, it is returned in the query. This has caused issues for me in the past with regards to a VF page as it very quickly chewed up the view state size limit.

  • 1
    Hi, I actually followed your previous answer and added the criteria ContentDocument.FileType!='SNOTE' while fetching the contentDocumentLink and it worked like a charm :) – Anonymous Oct 9 at 21:25
  • Good to know, I'm just digging around and learning something new and trying to share it with anyone who stumbles upon the answer, Ill add another note for that as well so you can accept it. – gNerb Oct 9 at 21:27
  • oh I see, in your edited answer basically you are fetching the files in a slightly different way rather than fetching the files I did. I actually like the new way you suggested. I will use that from next time but for now, I am gonna live with what I have, since I went too far. Thanks again for the help and sharing your insights. – Anonymous Oct 9 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.