I have a Lookup relationship to another custom sObject, and I want to access: sObject.sObject1__r.Name

However, sObject__r.Name is undefined in the context of the trigger. If I attempt to run this query I quickly overflow the SOQL Query Gov. Limit,

Is there a way to run a single SOQL for all items in Trigger.new (if the trigger is called repeatedly) like one firing after updating a list of more than 100 items?

Example code (hits gov. limits)

trigger MeetingMemberTrigger on MeetingMember__c (before insert, before update,
        after insert, after update) {

List<Meeting__c> meetings = [SELECT Id,Name FROM Meeting__c WHERE Id IN 
                                        (SELECT Meeting__c FROM MeetingMember__c WHERE Id IN :Trigger.new)];
for(MeetingMember__c member : Trigger.new){ 
   // do some stuff with member.Meeting__r.Name (which is undefined)


The solution is to execute a query for all of your 'missing' related data and store the results into a map so that you can access it quickly without iterating over elements to find what you're looking for.

trigger MeetingMemberTrigger on MeetingMember__c (before insert, before update,
        after insert, after update) {

    if (Trigger.isUpdate) {
        // query for the additional data you need in the trigger
        Map<Id, MeetingMember__c> meetingMembersMap 
                    = new Map<Id, MeetingMember__c>([SELECT Id
                                                        , Meeting__r.Name 
                                                    FROM MeetingMember__c 
                                                    WHERE Id IN :Trigger.New]);

        for(MeetingMember__c triggerMember : Trigger.new){
            // retrieve the record from the map which has the related data
            MeetingMember__c queryMember = meetingMembersMap.get(triggerMember.id);

            // do stuff using queryMember.Meeting__r.Name here
            if (queryMember.Meeting__r.Name == 'something') {

                // manipulate the item in the trigger collection as necessary
                // triggerMember.SomeField__c = 'new value';
| improve this answer | |
  • And this won't overflow the gov. limit when I run update on a list of MeetingMember__c that's over 100 items? – jordan.baucke Aug 9 '13 at 16:32
  • It shouldn't; it's only executing one SOQL statement that won't return too many results. – Mike Chale Aug 9 '13 at 16:42
  • Maybe I misunderstood your question but I thought you were asking how to avoid re-running the query for meeting__r.name for every batch of 200 meetingMembers. I don't see how this solves that issue. – Greg Grinberg Aug 9 '13 at 17:14
  • The original post was doing a separate query for each record in Trigger.new, and the query needed to be moved out of the loop. There will still be a query run each time this executes. – Jeremy Nottingham Aug 9 '13 at 19:49
  • Note that this will not work on the before insert items: they don't have an id yet (id == null for items in Trigger.New). – Legolas Feb 14 '17 at 10:23

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.