+ Start a Discussion
kiran punurukiran punuru 

Compare two OpportunityTeamMembers Profile

Hi All,

I am having one requirement like when 2  opportunity team member is added to a opportunity then if the profile of the opportunity team members are different then i need to set a field to true on opportunity object ,if both profiles are same then false.Any help will be greatly appreciated.

Thanks,
Kiran 
veda Hebbarveda Hebbar
Hi Kiran,

Please follow below steps:

1. Create after inter trigger on OpportunityTeamMember.
2. Create Map<Id, set<String>> mapOppIdWithProfileId;
   Create set<Id> setOppIdToUpdate;
   Create Map<Id, Id> mapUserWithProfileId;
3. Store OpportunityTeamMember.OpportunityId in set(setOppId).
   Store OpportunityTeamMember.UserId in set(setUserId).
4.Query User whi Id=:setUserId and store in map with Profile

    for(User  objUser: [Select Id, ProfileId from User where Id=:setUserId])
    {
        mapUserWithProfileId.put(objUser.Id,objUser.ProfileId);
    }



5. Query all OpportunityTeamMember with OpportunityId =:setOppId

for(OpportunityTeamMember objOppTeamMem: [Select Id,UserId,OpportunityId  From OpportunityTeamMember where OpportunityId =: setOppId ] )
{
    id idProfileId = '';
    if(mapUserWithProfileId.contains(objOppTeamMem.UserId))
        idProfileId = mapUserWithProfileId.get(objOppTeamMem.UserId);
        
    if(mapOppIdWithProfileId.containsKey(objOppTeamMem.OpportunityId))
    {
        if(mapOppIdWithProfileId.get(objOppTeamMem.OpportunityId).contains(idProfileId))
            setOppIdToUpdate.put(objOppTeamMem.OpportunityId)
        else
            mapOppIdWithProfileId.get(objOppTeamMem.OpportunityId).add(idProfileId)
    }
    else
    {
        mapOppIdWithProfileId.put(objOppTeamMem.OpportunityId, new set<Id>{idProfileId});
    }
}

6. Iterate setOppIdToUpdate
if(!setOppIdToUpdate.isEmpty())
{
    list<Opportunity> lstOppToUpdate = new List<Opportunity>();
    for(Id idOpp : setOppIdToUpdate)
    {
        lstOppToUpdate.add(New Opportunity(Id=idOpp, fildToUpdate= false ));
    }
    
    if(!lstOppToUpdate.isEmpty())
        update lstOppToUpdate;
}


Please follow above steps and let me know if you need more information or help on this.

Thanks,
Vedashri
ManojSankaranManojSankaran
Hi Kiran,

You can impletment is using a trigger. Trigger can be written in opportunity team.

If you need a trigger let me know.


Thanks
Manoj S



 
Srinivas SSrinivas S
Hi Kiran,

If you mean to say opportunity team member role not profile, please take the following code as reference to achieve your requirement -
trigger OpportunityTeamMemberTrigger on OpportunityTeamMember(after insert) {
    Map<Id,Set<String>> oppTeamMap = new Map<Id,Set<String>>();
    for(OpportunityTeamMember oppTeamMem : trigger.new) {
        if(oppTeamMap.containsKey(oppTeamMem.OpportunityId))
            oppTeamMap.get(oppTeamMem.OpportunityId).add(oppTeamMem.TeamMemberRole);
        else
            oppTeamMap.put(oppTeamMem.OpportunityId,new Set<String>{oppTeamMem.TeamMemberRole});
    }
    
    List<Opportunity> opp2Upd = new List<Opportunity>();    
    for(Opportunity opp : [select id, IsPrivate from Opportunity where id in: oppTeamMap.keyset()]) {
        if(oppTeamMap.get(opp.Id).size() > 0) {
            opp.IsPrivate = true;
            opp2Upd.add(opp);
        }
        else {
            opp.IsPrivate = true;
            opp2Upd.add(opp);
        }
    }
    
    if(opp2Upd.size() > 0) {
        update opp2Upd;
    }
}

------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
kiran punurukiran punuru
Hi Srinivas,I need to compare the opportunity profile not role,Manoj can you please tell me how to acheive this using trigger.
kiran punurukiran punuru
Hi Vedashri,

Iam getting error at the below Bolded Lines can you please help me why its going wrong.

5. Query all OpportunityTeamMember with OpportunityId =:setOppId

for(OpportunityTeamMember objOppTeamMem: [Select Id,UserId,OpportunityId  From OpportunityTeamMember where OpportunityId =: setOppId ] )
{
    id idProfileId = '';
    if(mapUserWithProfileId.contains(objOppTeamMem.UserId))
        idProfileId = mapUserWithProfileId.get(objOppTeamMem.UserId);
        
    if(mapOppIdWithProfileId.containsKey(objOppTeamMem.OpportunityId))
    {
        if(mapOppIdWithProfileId.get(objOppTeamMem.OpportunityId).contains(idProfileId))
            setOppIdToUpdate.put(objOppTeamMem.OpportunityId)
        else
            mapOppIdWithProfileId.get(objOppTeamMem.OpportunityId).add(idProfileId)

    }
    else
    {
        mapOppIdWithProfileId.put(objOppTeamMem.OpportunityId, new set<Id>{idProfileId});
    }
}

Thanks
Kiran