You need to sign in to do that
Don't have an account?
Zach Delacroix
Can anyone please advise how to get the MainParent's ID?
Thank you!
-Zach
How to Get Parent ID
Hi Experts,
I have a simple question and I'm not sure why it's not returning correct data..
In my Class, I want to get the Parent Id of the Object. I'll need to traverse from Child, then Parent, and Parent of the Parent. Goes like this.
myObject > myObjectParent >MainParent.
myObject is where my trigger is, I want to be able to get the MainParent's ID and Here's how I do it. (The usual approach when I get Ids)
public class testClass { public void myMethod(List<myObject__c> myObjects){ Set<Id> myObjectID = new Set<Id>(); for(myObject__c myObject : MyObjects){ myObjectID.add(myObject.MyParentObject__r.MainParentID); } } }Am I doing it wrong for this kind of situation? The code above returns Null so I can't use it in my query..
Can anyone please advise how to get the MainParent's ID?
Thank you!
-Zach
Since your method is being called from trigger, so you can get the ancestor upto 1 level only. For accessing the values for more than 1 levels you will have to perform SOQL.
I am assuming that your trigger is fired on after insert or update.
All Answers
Not sure how you have ran the query for 'myObject', could not find it here. But the following piece of code returns the Account Id for an Opportunity Product (Opportunity Line Item) using the Opportunity object. This example is also having similar hierarchy like myObject > myObjectParent >MainParent (OpportunityLineItem > Opportunity > Account).
See if this helps, basically you need to have the Main Object Id in the select statement while retrieving the myObject records using the relationship.
BTW, was your other issue resolved?
Thanks
AR
If this reply helps solve your problem then please mark it as best answer.
Since your method is being called from trigger, so you can get the ancestor upto 1 level only. For accessing the values for more than 1 levels you will have to perform SOQL.
I am assuming that your trigger is fired on after insert or update.
In case of before insert you will have to go for following approach instead:
I don't have any documentation at the moment to share with you, but you can practically try this in your dev org.
You're the best Pankaj! Hope to learn more from you :)
Thanks for the suggestions as well @Anupam Rastogi
Have a nice Day!
-Zach
I was going through your answer and have a question. When you say "you can get the ancestor upto 1 level only", I expected it to mean that I can at least retrieve the fields of parent one level up without using SOQL i.e. below should give me at least the value of grandparent Id (assuming it is stored at parent level):
Id gpId = myObject.MyParentObject__r.MainParentID;
Can you please explain.
Id gpId = myObject.MyParentObject__r.MainParentID;
As per your above code statement, we are accessing the parent at 2nd level. The 1st level means that myObject.MyParentObject__c. In other words, just one above is 1st level and one above of 1st level is 2nd level and so on.
We cannot retrieve any field value at parent level in trigger except Id(ie. myObject.MyParentObject__c). After dot(.) we consider the levels and these are determined by the lookup fields.
Example: Object A is the parent of Object B and Object C is child of Object B. Now, we want to access the grandparent at object C, then we will use 2nd level formula like C.B.A. C.B will be first level and C.B.A will be second level.
Please let me know if this makes sense.
Thanks,
Pankaj
Thanks!
Seema