I have found myself in a bit of a problem... I am grabbing events and their attachments based on certain filter criteria so there is no problem getting attachments in SOQL. The problem comes with the text filter and SOSL.

Apparently, I cannot perform a SOSL query with a child query inside it (I.e: (SELECT Id, Name FROM Attachments)).

SOQL I can do: SELECT Id, Subject, (SELECT Id, Name FROM Attachments) FROM Event.

Now I am a bit at a loss as to how I can retrieve the events and their attachments with SOSL... I could of course query attachments second, but that means more processing as I attempt to match the attachments up to their events... And I am doing a SOSL query for text search as I can do 'IN ALL FIELDS' as opposed to constructing a string matching each possible field to the text in SOQL...

Is there anything that can be done to retrieve the attachments of an event with the event in SOSL?

  • how you are searching events? Is that based on whatId or WhoId or EventId? Commented Apr 2, 2018 at 0:18
  • In my SOSL search, it is based on a string the user puts in, then I do FIND 'String' IN ALL FIELDS RETURNING Event(all my event fields I want data for WHERE my limiting parameters) Commented Apr 2, 2018 at 0:20
  • so, based on the events you want to fetch attachments, right? and you dont want to search attachment for that string? Commented Apr 2, 2018 at 0:22
  • Yes, I want to just grab the attachments of each event, without searching the attachments. I am only interested in searching Event fields, unless there is some hacky way to do this by also searching for anything in attachments? Commented Apr 2, 2018 at 0:24

1 Answer 1


SOSL doesn't support relationship query the way SOQL supports. So, you need to fetch those records separately like this.

List<Event> lstEvent = [FIND :searchString
                                    IN ALL FIELDS RETURNING Event (Id,WhatId, Subject,......)
                                    ORDER BY CreatedDate DESC)                         
                                   LIMIT 2000][0];
Map<Id, Event> mapEvent = new Map<Id,Event>(lstEvent);

List<Attachment> lstAttachment = [SELECT Id, Name... FROM Attachment WHERE ParentId IN:mapEvent.keySet()];

You could refer my blog posting on some keyword search series. Keyword Search Series - Display paginated data using Visualforce and Datatable for CaseComments with server side search

Also, refer SOSL Limits

The search engine limits the number of records analyzed at each stage of the search process. Sometimes, these limits cause a matching record to be excluded from a user’s results.

  1. The search engine looks for matches to the search term across a maximum of 2,000 records (this limit starts with API version 28.0)
  2. SOSL applies different limits for a given object or situation. If the search is for a single object, the full record limit is applied. If the search is global across multiple objects, each object has individual limits that total 2,000 records.
  3. Admins (users with the View All Data permission) see the full set of results returned.
  4. For all other users, SOSL applies user permission filters. Individual users see only those records that they have access to. Results sets and order vary by the user issuing the search and can change throughout the day as records are added or removed from the index.
  • Could I then loop through the attachment list, and throw them in the correct events Attachments field? Since in SOQL, if you do a relationship query of Attachments, you can get ev.Attachments. Commented Apr 2, 2018 at 0:33
  • SOSL doesn't support relationship query in SELECT clause Commented Apr 2, 2018 at 0:35
  • Yes, but would I be able to just access the event Attachments field to throw the attachments in the separate list into? Like: `for(each attachment in attachment list){Event ev = eventMap.get(attachment.ParentId); ev.Attachments = attachment;} Is that attachments field accessible if I do not get it from the query? Commented Apr 2, 2018 at 0:46
  • you need to query for attachments separately Commented Apr 2, 2018 at 0:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .