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
VaderVader 

Replicating Opportunity Team Members to Child Opportunities

We currently have our opportunities set up as a self lookup in a parent/child relationship via a single Visual Force page.  They parent and child records are identified primarilly via record type.  For example when an opportunity is created, you have the ability to add products.  When saved, the opportunity information becomes the parent opportunity and the products added become the child opportunities.  Each of these child records (products) becomes a record in the opportunity object when saved.  

 

We did this so that we could track stages and probabilities for each product.

 

Owners of the opportunities currently add members to their Opportunity Team on the parent opportunity record.  This team infomation is then propagated to the child records via an Insert Trigger.

 

The problem is that, I also need to allow the same level of permission for the child records that exists on the parent record.  

 

Right now, setting a value of Read/Write on the parent will only create a value of "Read" for every child entry in the Opportunity Team Member object.  Here is the trigger code:

 

trigger syncOpportunityTeamMember on Opportunity (after insert) 
{
    Map<id,Id> projectChildOppMap = new Map<Id,Id>();
    
    for(Opportunity o:trigger.new)
    {
        if(o.Project_Opportunity__c!=null)
            projectChildOppMap.put(o.Id,o.Project_Opportunity__c);            
    }
    
    Set<Id> projOppIdSet = new Set<Id>();
    projOppIdSet.addAll(projectChildOppMap.values());
    
    Map<Id,List<OpportunityTeamMember>> oppTeamMemberMap = new Map<Id,List<OpportunityTeamMember>>();
    
    for(OpportunityTeamMember otm:[Select Id,UserId,OpportunityId,OpportunityAccessLevel,TeamMemberRole from OpportunityTeamMember where OpportunityId In:projOppIdSet])
    {
        List<OpportunityTeamMember> lst = oppTeamMemberMap.containsKey(otm.OpportunityId) ? oppTeamMemberMap.get(otm.OpportunityId):new List<OpportunityTeamMember>();
        lst.add(otm);
        oppTeamMemberMap.put(otm.OpportunityId,lst);                    
    }
    
    List<OpportunityTeamMember> lst2Insert = new List<OpportunityTeamMember>();
    
    for(Id oppId :projectChildOppMap.keySet())
    {
        if(projectChildOppMap.containsKey(oppId) && oppTeamMemberMap.containsKey(projectChildOppMap.get(oppId)))
        {
            for(OpportunityTeamMember otm:oppTeamMemberMap.get(projectChildOppMap.get(oppId)).deepClone())
            {
                otm.OpportunityId = oppId;
                lst2Insert.add(otm);    
            }
        }
    }
    
    if(lst2Insert.size()>0)
        insert lst2Insert;
}