I have an object 'Household Served' with a field called volunteer, and it also has an Event attached to it. The Event has a related list of Contacts. I need to write a trigger that validates the the volunteer on the Household served object is in the related list of the event.

Currently I have :

trigger VolunteerValidate on Household_Served__c (before insert, before update) {
    for(Household_Served__c house : Trigger.new){
        Event__c e = new Event__c();
        e.Id = house.Event__c;
        if(house.Volunteer_Name__c != e.RelatedListGoesHere){


How can I access the Event's related list of contacts?

  • Are you using the system built in Event object or your own custom Event__c custom object? Apr 29, 2015 at 20:55
  • the custom Event__c Apr 29, 2015 at 21:00

2 Answers 2


The related list of contacts can be accessed via a sub-select. I'm assuming you are using the standard Contacts object.

You want something like this (this is bulkified, btw)

  1. Get a list of eventids
  2. Query events and associated contacts. Put in a map.
  3. Loop through Household_Server__c
  4. Get related Event__c from the map.
  5. Loop through the contacts on the Event__c
  6. If one matches, you are gold.

Here is the code:

Set<id> eventIds = new Set<Id>();
for (Household_Served__c house : Trigger.new) {
Map<Id, Event__c> eventMap = new Map<id,Event__c>([SELECT id, Name, 
                                                                   (SELECT Id, Name 
                                                                    FROM Contacts)
                                                   FROM Event__c 
                                                   WHERE id in :eventIds]);

for(Household_Served__c house : Trigger.new){
    Event__c e eventMap.get(house.Event__c);
    if (e == null){

    Boolean contactIsOnEvent = false;
    for (Contact c : e.Contacts){
        if(house.Volunteer_Name__c = c.Name){
            contactIsOnEvent = true;

You would need to query the event object to get the list of contacts for that particular event record. For this, you would need the related list name for the contacts. You can get this by going to the Contact Object and to the Event field and check for the "Child Relationship Name". Get that name and use in the SOQL query. Once you get the list of contacts, you can loop through that list and compare if the Volunteer name exists in that contact list or not.

Assuming that the Child Relationship is Contacts, it would be something like the below (not tested):

trigger VolunteerValidate on Household_Served__c (before insert, before update) {
    for(Household_Served__c house : Trigger.new){
        /*Event__c e = new Event__c();
        e.Id = house.Event__c;*/
        //You would need to append __r for the relationship to the Contacts,            like the one below
        Event__c e = [ select id,name,(select name from Contacts__r) from Event__c where id = :house.Event__c ];
        for(Contact c: e.Contacts__r){
            if(house.Volunteer_Name__c != c.name){
               //your logic


You must log in to answer this question.

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