function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AbAb 

Trigger is not able to get the values from the Parent objects

Hello,

I have below trigger
trigger temp on test__c (before update, before insert) {
    String one= '';
    Id two;
    for (test__c obj : Trigger.new) {
        one = obj.Custom__r.one__c ;
        two = obj.Id;
        System.debug(one); //null
        System.debug(two); // has a Id
        System.debug(obj); // Custom__r gives a Id
    }
}
I am trying to get the "obj.Custom__r.one__c", this value is present but its alwys showing as null

Thank you for suggestion !
Best Answer chosen by Ab
sachin kadian 5sachin kadian 5
trigger temp on test__c (after update, after insert) {
    String one= '';
    Id two;
List<test__c> testList = [select id,Custom__r.one__c from test__c where id IN : trigger.newMap.keySet()];
    for (test__c obj : testList) {
        one = obj.Custom__r.one__c ;
        two = obj.Id;
        System.debug(one); //null
        System.debug(two); // has a Id
        System.debug(obj); // Custom__r gives a Id
    }
}

Try like this

All Answers

MagulanDuraipandianMagulanDuraipandian
Check this - http://www.infallibletechie.com/2013/08/unable-to-get-values-from-r-reference.html
sachin kadian 5sachin kadian 5
Hi Sandrine
you cannot get parent values from trigger.new. For that you need to query it first.
sachin kadian 5sachin kadian 5
trigger temp on test__c (after update, after insert) {
    String one= '';
    Id two;
List<test__c> testList = [select id,Custom__r.one__c from test__c where id IN : trigger.newMap.keySet()];
    for (test__c obj : testList) {
        one = obj.Custom__r.one__c ;
        two = obj.Id;
        System.debug(one); //null
        System.debug(two); // has a Id
        System.debug(obj); // Custom__r gives a Id
    }
}

Try like this
This was selected as the best answer