How do I update a grandchild's field if there is a change in grandparent field? I know I have to write a trigger because its cross object
I can access the immediate child record of parent which is child
List<parent__c> fbr = [SELECT Name ,
(SELECT Id , Name, CreatedDate FROM child__r)
FROM parent__c];
Any reference will be helpful. Thanks
Attribution to: MnZ
Possible Suggestion/Solution #1
"In each specified relationship, only one level of parent-to-child relationship can be specified in a query. For example, if the FROM clause specifies Account, the SELECT clause can only specify the Contact or other objects at that level. It could not specify a child object of Contact." (http://www.salesforce.com/us/developer/docs/dbcom_soql_sosl/Content/sforce_api_calls_soql_relationships.htm)
One option would be to rephrase the query, if you can, into a select from the Grandchild object, as you can reference 5 levels of parents upwards. e.g.
select child__r.parent__r.name, name from grandchild__c where ....
Attribution to: Doug B
Possible Suggestion/Solution #2
You can get your grandchilderen with a subselect in the SOQL where clause.
In words:
Select the grandchilderend who's parent's parent is one of the grandparents you have.
In code:
//example= account (parent) -> testobject__c -> testChildObject__c (grandchild)
List<Account> accs = trigger.New; //list of grandparents that have been changed
list<testChildObject__c> lst = [SELECT Id,name, testobject__c
FROM testChildObject__c
WHERE testObject__c In (SELECT id
FROM testObject__C
WHERE Account_lookup__c in:accs
)
];
- testChildObject__c object has a lookup to the testobject__c: field testObject__c
- testobject__c object has a looup to account: field Account_lookup__c
Attribution to: Samuel De Rycke
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/33829