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
Mahmood ButtMahmood Butt 

Fetch all content notes related to an event

Requirement
I have to fetch all the content Notes that are associated to events created after 01-Aug-2016.

Logic path
I have taken the following path to get to content notes
  • fetch events
  • find the Link via ContentDocumentLink object by passing them Event ids fetched in step-1
  • fetch ContentVersion by passing ContentDocumentIds retrieved in step-2
  • check each contentversion record by looking at their fileType to filter out only notes (SNOTE).
Code
Set<id> ev=(new Map<Id, Event>([SELECT id from Event where CreatedDate > 2016-08-01T00:00:00Z])).keySet(); 
Map<id, ContentDocumentLink> cdlMap = new Map<id, ContentDocumentLink>(); 

for(ContentDocumentLink cdl: [SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId IN : ev]) 
{ 
    cdlMap.put(cdl.ContentDocumentId, cdl);         
    contentDocumentIdSet.add(cdl.ContentDocumentId); 
} 
system.debug('cdl keys > ' + contentDocumentIdSet ); 
List<ContentVersion> cvMap = new List<ContentVersion>(
              [SELECT Title, FileType, ContentDocumentId 
               FROM ContentVersion 
               WHERE ContentDocumentId IN:contentDocumentIdSet]); 

system.debug('cdlMap ' + cdlMap.size() + '| cvMap > ' + cvMap.size());

Problem
ContentVersion list (cvMap) is not returning anything.
  • I tried passing cdlMap.keySet() didn't work.
  • I then created a Set<id> and passed the map keyset to it and then supplied it to the SOQL, that didn't work either.
  • and now I tried the above code to created a set in loop and provide it in SOQL. Not working.
If I hardcode ContentDocumentId to SOQL single or multiple, it is working. Any help would be highly appreciated.
Best Answer chosen by Mahmood Butt
Austin MuellerAustin Mueller
Try this out!
 
Map< Id, Event > ev = new Map< Id, Event > ( [SELECT Id FROM Event WHERE CreatedDate > 2016-08-01T00:00:00Z] );

if( !ev.isEmpty() ) {

    List< Id > cdl = new List< Id >();
    Set< Id > events = ev.keySet();

    for ( ContentDocumentLink c : [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId IN :events] ) {

        cdl.add( c.ContentDocumentId );

    }

    if ( !cdl.isEmpty() ) {

        List< ContentVersion > notes = [SELECT Title, FileType, ContentDocumentId FROM ContentVersion WHERE ContentDocumentId IN :cdl AND IsLatest = true AND IsDeleted = false AND FileType = 'SNOTE'];

    }

}