+ Start a Discussion
SandrineSandrine 

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 Sandrine
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