+ Start a Discussion

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)
    Set<Id> projOppIdSet = new Set<Id>();
    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>();
    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;
        insert lst2Insert;