You need to sign in to do that
Don't have an account?
DJ Maheshwari
How to get values of Parent of Parent in child object
I created Three Custom Objects.
1. Custom Product
2. Custom Asset
3 Work Order
On all the objects there is custom field, named Description.
And Custon Product have self Lookup. Custom Asset is a child of Custom Product(Lookup relationship). Work Order is Child of Custom asset(lookup relationship).
If I insert Work Order and Select the Asset record then the value of Description is populate while insert if the selected asset record description field is empty then it populates the value of description of custom product of selected asset and if that is also empty then it populates the value of self parent of custom product of selected asset.
1. Custom Product
2. Custom Asset
3 Work Order
On all the objects there is custom field, named Description.
And Custon Product have self Lookup. Custom Asset is a child of Custom Product(Lookup relationship). Work Order is Child of Custom asset(lookup relationship).
If I insert Work Order and Select the Asset record then the value of Description is populate while insert if the selected asset record description field is empty then it populates the value of description of custom product of selected asset and if that is also empty then it populates the value of self parent of custom product of selected asset.
Hi Dharmesh,
First, you need to check whether WorkOrder.Asset is having a description, if so we can have a Map to hold asset.Id ,asset
Else, we need to check if the Asset is having Product and its descrption is not equal to null we can have a Map to hold asset.Id,assetRecord.Product__r
Map<Id,Asset__c> assetMap = new Map<Id,Asset__c>();
Map<Id,Product__c> productMap = new Map<Id,Product__c>();
if(assetIdSet.size() > 0){
for(Asset__c assetRecord : [SELECT Id,Name,Description__c,Product__c,Product__r.Description__c,Product__r.Product__r.Description__c,Product__r.Product__c FROM Asset__c WHERE Id IN: assetIdSet]){
assetMap.put(assetRecord.Id, assetRecord);
if(assetRecord.Description__c == null && assetRecord.Product__c != null){
productMap.put(assetRecord.Id, assetRecord.Product__r);
}
}
}
After getting the parent values we can have the logic to populate the description on Work Order
for(Work_Order__c order : workOrderList){
if(order.Description__c == null && order.Asset__c != null){
if(WorkOrder_cTriggerHelper.assetMap.containsKey(order.Asset__c)){
if(WorkOrder_cTriggerHelper.assetMap.get(order.Asset__c).Description__c != null){
order.Description__c = WorkOrder_cTriggerHelper.assetMap.get(order.Asset__c).Description__c;
}else if(WorkOrder_cTriggerHelper.productMap.get(order.Asset__c).Description__c != null ){
order.Description__c = WorkOrder_cTriggerHelper.productMap.get(order.Asset__c).Description__c;
}else if(WorkOrder_cTriggerHelper.productMap.get(order.Asset__c).Product__r.Description__c != null){
order.Description__c = WorkOrder_cTriggerHelper.productMap.get(order.Asset__c).Product__r.Description__c;
}
}
}
All Answers
Hi Dharmesh,
First, you need to check whether WorkOrder.Asset is having a description, if so we can have a Map to hold asset.Id ,asset
Else, we need to check if the Asset is having Product and its descrption is not equal to null we can have a Map to hold asset.Id,assetRecord.Product__r
Map<Id,Asset__c> assetMap = new Map<Id,Asset__c>();
Map<Id,Product__c> productMap = new Map<Id,Product__c>();
if(assetIdSet.size() > 0){
for(Asset__c assetRecord : [SELECT Id,Name,Description__c,Product__c,Product__r.Description__c,Product__r.Product__r.Description__c,Product__r.Product__c FROM Asset__c WHERE Id IN: assetIdSet]){
assetMap.put(assetRecord.Id, assetRecord);
if(assetRecord.Description__c == null && assetRecord.Product__c != null){
productMap.put(assetRecord.Id, assetRecord.Product__r);
}
}
}
After getting the parent values we can have the logic to populate the description on Work Order
for(Work_Order__c order : workOrderList){
if(order.Description__c == null && order.Asset__c != null){
if(WorkOrder_cTriggerHelper.assetMap.containsKey(order.Asset__c)){
if(WorkOrder_cTriggerHelper.assetMap.get(order.Asset__c).Description__c != null){
order.Description__c = WorkOrder_cTriggerHelper.assetMap.get(order.Asset__c).Description__c;
}else if(WorkOrder_cTriggerHelper.productMap.get(order.Asset__c).Description__c != null ){
order.Description__c = WorkOrder_cTriggerHelper.productMap.get(order.Asset__c).Description__c;
}else if(WorkOrder_cTriggerHelper.productMap.get(order.Asset__c).Product__r.Description__c != null){
order.Description__c = WorkOrder_cTriggerHelper.productMap.get(order.Asset__c).Product__r.Description__c;
}
}
}
Suppose there are fields like:
Asset lookup on workorder - Asset__c
Product lookup on Asset - Product__c
Product lookup on product - ParentProduct__c
If you want to access the Description field of Asset and Description field of parent of asset (i.e., Product) and also description field of parent of parent of Asset(i.e., product's parent) then you can use this query:
List<WorkOrder> workOrderList = [SELECT Id, Name,Description,Asset__c.Description__c,Asset__c.Description__c, Asset__c.Product__c.Description__c,Asset__c.Product__c.ParentProduct__c.Description__c];
//populating the description field of workorder
for(WorkOrder wo: workOrderList){
if(wo.Asset__c.Description__c!=Null){
wo.Description=wo.Asset__c.Description__c;
}else if(wo.Asset__c.Product__c.Description__c!=Null){
wo.Description = wo.Asset__c.Product__c.Description__c;
}else if(wo.Asset__c.Product__c.ParentProduct__c.Description__c!=Null){
wo.Description=wo.Asset__c.Product__c.ParentProduct__c.Description__c;
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com