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
Sinka Tinko 1Sinka Tinko 1 

Update child field from Parent using trigger.

Hi,

I have a field on Opportunity (parent_opportunity_owner) that needs to be updated with the parent opportunity owner(opportunity has a lookup relationship with opportunity using opportunity_parent field). I need to achieve this using a trigger. Can anyone help me out. Thanks in advance.
Best Answer chosen by Sinka Tinko 1
Medhya MahajanMedhya Mahajan
Hi, 

You can do the same using a FORMULA FIELD like this :

 parent_opportunity_owner__r.Owner.FirstName 

In case you have a scenario where you need to use the TRIGGER , here is a trigger :

Here FIELD is your custom field where you want to populate your Parent's Owner.
trigger getParentOwner on Opportunity (before update, before Insert){
    Set<Id> oppParentSet = new Set<Id>();
    for ( Opportunity opp : Trigger.new ){    
        oppParentSet.add(opp.parent_opportunity_owner__c);
    }
    
    Map<Id,Id> oppParentsOwnerMap = new Map<Id,Id>();
    
    for (Opportunity parentOpp : [SELECT Id, OwnerId FROM Opportunity WHERE ID IN : oppParentSet ){   
        oppParentsOwnerMap.put(parentOpp.Id, parentOpp.OwnerId);
    }
    
    for ( Opportunity opp : Trigger.new ){    
        if(oppParentsOwnerMap.containsKey(opp.parent_opportunity_owner__c)){
        opp.FIELD = oppParentsOwnerMap.get(opp.parent_opportunity_owner__c); 
        
        }
    }
}

Mark solved if this helps.

Regards
Medhya Mahajan

 

All Answers

Medhya MahajanMedhya Mahajan
Hi, 

You can do the same using a FORMULA FIELD like this :

 parent_opportunity_owner__r.Owner.FirstName 

In case you have a scenario where you need to use the TRIGGER , here is a trigger :

Here FIELD is your custom field where you want to populate your Parent's Owner.
trigger getParentOwner on Opportunity (before update, before Insert){
    Set<Id> oppParentSet = new Set<Id>();
    for ( Opportunity opp : Trigger.new ){    
        oppParentSet.add(opp.parent_opportunity_owner__c);
    }
    
    Map<Id,Id> oppParentsOwnerMap = new Map<Id,Id>();
    
    for (Opportunity parentOpp : [SELECT Id, OwnerId FROM Opportunity WHERE ID IN : oppParentSet ){   
        oppParentsOwnerMap.put(parentOpp.Id, parentOpp.OwnerId);
    }
    
    for ( Opportunity opp : Trigger.new ){    
        if(oppParentsOwnerMap.containsKey(opp.parent_opportunity_owner__c)){
        opp.FIELD = oppParentsOwnerMap.get(opp.parent_opportunity_owner__c); 
        
        }
    }
}

Mark solved if this helps.

Regards
Medhya Mahajan

 
This was selected as the best answer
Sinka Tinko 1Sinka Tinko 1
Hi,

Thanks for your reply. In Line 4, the parent_opportunity_owner__c is the field that needs to be populated.

On opportunity, there are two record types (Parent and Child). While creating Opportunity with Parent record type, parent_opportunity_owner__c field is not visible.

While creating Opportunity with Child record type, parent_opportunity__c (lookup to parent opportunity created above). So, a User can create a parent opportunity(record type parent) but another user can create a child opportunity (record type child) which has parent_opportunity_owner__c that needs to be populated with the parent owner (Opportunity created with Parent record type)

Thanks
 
Sinka Tinko 1Sinka Tinko 1
Ok. Got it now using the above logic. Thank you.