2

I have a contact trigger that passes Trigger.NewMap and Trigger.OldMap to an apex method, and then in the apex method, the following debug statement returns null for c.Account.Unit_Type__c:

for(Contact c: newMap.values()){
        System.debug('MM ContactTrigger ++++ c.Account.Unit_Type__c = ' + c.Account.Unit_Type__c);
}

Question: I know I can do a separate SOQL query using the map as a WHERE condition to get a list of the contacts and the values of the related Account and Account.Field__c's, and then loop through that list instead of the newMap.values(), but is there any way to ADD that relationship data to the Trigger.NewMap <Id,Contact> map to allow me to traverse the relationships with dot notation while looping through newMap.values like I am trying to do in the code above?

I ask because there is already a lot of code in this class already using the newMap.values() loop but I now want to introduce a new conditional check against related account data. Ideally I'd like to do that without rewriting too much of the class and the easiest way would be if I could just traverse the relationship straight from the map. I am also considering a formula field on Contact but just wanted to check my options first.

1 Answer 1

1

First of all, a trigger context won't include any child or parent relationships. These have to be queried for. But assuming you have queried the parent Accounts of the Contacts in the trigger and saved the results in a map

map<id,Account> aIdToAccountMap;

Then, you can modify the sobjects in Trigger.newMap as follows

for (Contact c: Trigger.new)
     c.Account = aIdToAccountMap.get(c.AccountId);

Note that this technique does not work for child relationships; you could not have a map of ContactIds to a list of Attachments as in:

map<ID,Attachment[]> cIdToAttachmentsMap;

and do ...

for (Contact c: Trigger.new)
   c.Attachments = cIdToAttachments.get(c.Id); 
5
  • I thought editing child relationships did not work. See salesforce.stackexchange.com/questions/54067/…. Is this now supported?
    – dsharrison
    Oct 5, 2016 at 2:02
  • 1
    @D.S. - I believe I said above that editing in child relationships does not work. There is a clever (but involved) workaround to this -- see andyinthecloud.com/2014/12/03/…
    – cropredy
    Oct 5, 2016 at 15:10
  • right you are I totally missed that!
    – dsharrison
    Oct 5, 2016 at 17:26
  • Thanks for your help! Turns out that in this trigger context I wasn't able to modify trigger.new values because I was already in after update and got the read-only error, but your answer still clarifies the scenario for me. Ultimately since I was after update already, I chose to build my IdToAccountMap and just .get() the Unit_Type__c value from the map using c.AccountId as the key. - but I see how I could leverage this in the future in other contexts. Oct 5, 2016 at 19:23
  • @MorganMarchese - you could have used clone() to get around the Trigger.New issue in after update
    – cropredy
    Oct 7, 2016 at 18:34

You must log in to answer this question.

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