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
pradeepkumar battapradeepkumar batta 

Update grand parent field with grand child


Hi every one,
    i have requirement for update grand parent field update when update happens in grand child object .how can we achieve this .
The object like A (grand parent) ,B (parent) ,C(grand child)
Please suggest me.



 
Abhishek BansalAbhishek Bansal
Hi,

Please find below a sample code for your requirement :
 
trigger updateGrandParent on grandChild(after insert, after update){
	Set<Id> parentIds = new Set<Id>();
	for(grandChild child : triggger.new){
		parentIds.add(child.ParentId);
	}
	Map<Id,Parent> parentRecords = new Map<Id,Parent>([Select grandParent from Parent where id in :parentIds]);
	Set<Id> grandParentIds = new Set<Id>();
	
	for(Parent parentRecord : parentRecords.vaues()){
		grandParentIds.add(parentRecord.grandParent);
	}
	
	Map<Id,GrandParent> grandParentRecords = new Map<Id,GrandParent>([Select id,fieldToUpdate from GrandParent where id in : grandParentIds]);
	
	for(grandChild child : triggger.new){
		if(parentRecords.containsKey(child.ParentId) && grandParentRecords.containsKey(parentRecords.get(child.ParentId).grandParent)){
			grandParentRecords.get(parentRecords.get(child.ParentId).grandParent).fieldToUpdate = 'Update Value';
		}
	}
	update grandParentRecords.values;
}
//Replace following varibales
//1. grandChild = API name of your grand child record;
//2. Parent = Api Name of your parent object
//3. GrandParent = APi name of your grand parent object
//4. ParentId = API name of Lookup field on grand child of parent object
//5. grandParent = Api Name of lookup field on parent object of grand child
//6. fieldToUpdate = field of grand parent which you want to update.

Let me know if you need any help or clarification.

Regards,
Abhishek
Dan BroussardDan Broussard
Abhishek,
Did you use the Set<> command rather than the List<> command to avoid duplicate retruns from the Select query?
Thanks
Dan Broussard
Abhishek BansalAbhishek Bansal
Yes Dan, Since there can be multiple child records associated with a single parent record so in order to avoid duplicates in collection, I have used set collection type instead of List. Regards, Abhishek
usersfdc21usersfdc21
Thank you Abhishek, it was useful.
Pranav HP 9Pranav HP 9
Hi I've a similar requirement:
I've 3 objects:Parent__c,Children__c,GrandChild__c. In Parent object I've 2 fields Number_of_Children__c,Number_of_Grand_Children__c in Parent__c .
In Number_of_Children__c field I want to have the count of Children__c for the specific parent,I'm able to achieve this
For Number_of_Grand_Children__cI I need to have count of the GrandChild__c. I'm unable to achieve this
For Example:
I have a record for Parent__c, I've created 2 Children__c records let's say Child1 and Child2  and now Number_of_Children__c field will have the value=2.Further I drill down and create 2 child records each under Child1  and Child2, then I need to populate the field  Number_of_Grand_Children__c=4.

Please let me know if anyone can help me in this via trigger/ any apex means.
PS:Do not suggest declaration approach,it can be achieved via declaration