Say I have an event ID, and the goal is to get the event data and the names of every contact associated with the event in the EventRelation object.

Currently I look up the event record with the event ID from the Event object and run a separate query on the EventRelation object (also with event ID), and for each matching row in EventRelation, lookup the relation ID in the Contact object to get the contact names.

Basically this is a lot of calls, is it possible to do this all in one query?

  • query on evenrelation and learn about polymorphic soql to get parent Contact – cropredy Nov 14 '19 at 21:53

You can use a relationship query:

[SELECT Id, (SELECT Id, Relation.Name FROM EventRelations) FROM Event]

This will return Events and their Relations, if they have any.


Updated to add example with relation information.

  • this will not retrieve contact names – cropredy Nov 14 '19 at 21:59
  • @cropredy Yep I just edited after I realized I left out the Relation Information. – Nicholas Savini Nov 14 '19 at 22:00
  • 2
    note that while Relation.Name works, if OP wanted Contact.Email or Contact.Phone, you'd need to use polymorphic SOQL – cropredy Nov 14 '19 at 22:01
  • @NicholasSavini this seems to work well. How do you figure out that Relation.Name is what was needed? I tried running describe on the EventRelation object and there were no child relations. – rosghub Nov 14 '19 at 23:41
  • @rosghub In the documentation, you'll find that the name of the relation field for Event Relation is RelationId. When using Relationship Queries, the Relationship name for a Standard field will be the field name without "Id". If it were a custom relationship, you would need to add "__r" to the end (ie Custom_Field__r). More information here. – Nicholas Savini Nov 14 '19 at 23:54

You can use Relationship queries like:

for Normal Lookup fields:

SELECT Amount, Id, Name, (SELECT Quantity, ListPrice,
  PriceBookEntry.UnitPrice, PricebookEntry.Name,
  PricebookEntry.product2.Family FROM OpportunityLineItems)
  FROM Opportunity

for Polymorphic Lookup fields:

    WHEN Account THEN Phone, NumberOfEmployees
    WHEN Opportunity THEN Amount, CloseDate
    ELSE Name, Email
FROM Event WHERE WHAT.TYPE in ('Account','Opportunity')

later in Apex code:

List<Event> allEvs = [SELECT id,custom_field1,
    WHEN Account THEN Phone, NumberOfEmployees
    WHEN Opportunity THEN Amount, CloseDate
    ELSE Name, Email
FROM Event WHERE WHAT.TYPE in ('Account','Opportunity')];
for (Event ev: allEvs) {
    if (ev.What instanceof Account) {
        Account acc = ev.What;
    } else if (ev.What instanceof Opportunity) {
        Opportunity opp = ev.What;
  • This answer is correct in principle, but it would be stronger if it directly connected to this question, which is about Event, EventRelation, and Contact. – David Reed Apr 7 at 13:05
  • @DavidReed Here is a thought, if someone is much able to post a question on StackExchange, then I believe the same one can understand the solution as well. And if someone can't understand the logic or explanation from the answer then, then he/she should stop searching for the answer. – Deepak sharma Apr 26 at 6:32

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.