Unable to get values from __r Reference in a trigger

In a trigger we cannot get parent record or child record values using __r notation.

The work around for this is we have to get the parent records or child records using the trigger.newMap.keySet().

Sample Code:

List<ParentObject> List = [SELECT Name, Id FROM ChildRecord WHERE ParentObject__c IN : trigger.newMap.keySet();

5 comments:

  1. can we pull a Grand Parent Records from its grand Child?

    Sample Code

    List lst = [SELECT Name, ID, Parent__c, Parent__r.GrandParent__r.id FROM ChildRecord Where id IN : Trigger.New()];

    When i query this am getting Null for Parent__r.GrandParent__r.id (i did with both after and before update)

    ReplyDelete
    Replies
    1. Hi,
      This is possible.

      Kindly check GrandParent__c has value for the child's parent record.

      Cheers!!!

      Delete
    2. can you please check this link... https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AqZnIAK
      i do have the values for all, it didn't work therefore it did work around for that came up with querying the parent to get grand parent id.

      thanks

      Delete
    3. Hi,
      You can't get relationship value from trigger.new.

      Relationship values and formula field values will be null in trigger.new.

      Thank You!

      Delete
    4. Perfect finally got confirmed by expert, this is bugging me all the time. Thanks a lot

      Delete