0

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>();
//ContentDocumentLink[] cdlList =[SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId IN : ev];
Set<id> contentDocumentIdSet = new set<id>();
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. I wonder why it is not working in this case.

  • If you don't have permission to view the ContentVersions that you're trying to query, then that would explain what you're seeing. If they are privately shared (and not with you), and you're using the Dev Console to test, then you will be able to get the ContentDocumentLink objects, but querying on ContentVersion will return nothing. – Aidan Aug 31 '16 at 11:47
  • They are publicly shared with 'AllUsers'. and when i soql on individual ids on contentversion I get the records but not when I supply them in the form of a set<id> using IN clause. – Mahmood Aug 31 '16 at 12:42
  • That's really odd. I adapted your code slightly so that it would work on one of my orgs and it did work (when I had access to the CV). I only really changed the for-loop: for(ContentDocumentLink cdl: [SELECT Id, LinkedEntityId, ContentDocumentId FROM ContentDocumentLink WHERE ContentDocumentId = '06920000002qcCo']). So it doesn't look like something fundamental to SF – Aidan Aug 31 '16 at 12:51
  • That's what I mentioned in my question. If I supply hardcoded Ids, just like you did, even the IN clause work fine. But if i try to supply map.keySet() or a Set<id> to pass the ids to the soql dynamically, it doesn't return anything. – Mahmood Sep 1 '16 at 13:36
1

Following code works courtesy of Ausin Mueller.

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'];

}

}

One thing that boggles my mind: Why Set<id> or map.keySet()didn't work while List<id> worked. Anyone who can explain this to me?

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.