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
kiran punurukiran punuru 

If the user added on opportunity team have different profile then set a check box to true

I have one requirement like this,If the user added on opportunity team have different profile then set a check box to true which is best way to acheive this.can anyone advise on this.

thanks
kiran 
Nayana KNayana K
Trigger is a best way to achieve this :
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000kC50IAE
 
kiran punurukiran punuru
Hi Nayana ,
 
Thanks for the reply , i had used the same trigger and added users with two different profiles but the checkbox is still false.can you please help me .

regards,
kiran
Nayana KNayana K
trigger OpportunityTeamMemberTrigger on OpportunityTeamMember (after insert) {
    
    // Let's assume custom field on opportunity is : Is_Diff_Profile__C
    
    Set<Id> setIdUser = new Set<Id>();
    Map<Id, Id> mapUserIdToProfileId = new Map<Id, Id>();
    Map<Id, Id> mapOppIdToProfileId = new map<Id, Id>();
    Map<Id, Opportunity> mapOpportunityToUpdate = new map<Id, Opportunity>();
    Id idProfile;
    
    for (OpportunityTeamMember objOTM: Trigger.new){
        setIdUser.add(objOTM.UserId);  
    }
    
    for(User objUser : [SELECT Id, ProfileId FROM User WHERE Id IN : setIdUser])
    {
        mapUserIdToProfileId.put(objUser.Id, objUser.ProfileId);
    }
    
    for (OpportunityTeamMember objOTM: Trigger.new)
    {
        // get corresponding profile
        idProfile = mapUserIdToProfileId.get(objOTM.UserId);
        
        // if different profile then update opp 
        if(mapOppIdToProfileId.containsKey(objOTM.OpportunityId) 
        && idProfile != mapOppIdToProfileId.get(objOTM.OpportunityId) 
        && !mapOpportunityToUpdate.containsKey(objOTM.OpportunityId))
        {    
            mapOpportunityToUpdate.put(Id, new Opportunity(Id = objOTM.OpportunityId, Is_Diff_Profile__c = true));    
        }
        else
            mapOppIdToProfileId.put(objOTM.OpportunityId, idProfile);
    }
    
// sorry I had missed update keyword here. My bad. Please try now
    if(!mapOpportunityToUpdate.values().isEmpty())
        update mapOpportunityToUpdate.values();
}

Try now with this code
kiran punurukiran punuru
Hi Nayana,
I have already used update mapOpportunityToUpdate.values(); but still that check box is setting to true.

I have added opportunity team members like this:

First i have addded oppty team member1 with different profile as oppty owner profile . In this case it has to set to true which is not happening is it working for you?

Thanks,
kiran
 
Nayana KNayana K
trigger OpportunityTeamMemberTrigger on OpportunityTeamMember (after insert) {
    
    // Let's assume custom field on opportunity is : Is_Diff_Profile__C
    Map<Id, Opportunity> mapOpportunityToUpdate = new map<Id, Opportunity>();

    for (OpportunityTeamMember objOTM: [SELECT Id, User.ProfileId, OpportunityId, Opportunity.Owner.ProfileId 
										FROM objOTM 
										WHERE Id IN: Trigger.newMap.keySet()])
    {
		system.debug('==objOTM.User.ProfileId='+objOTM.User.ProfileId);
		system.debug('==objOTM.Opportunity.Owner.ProfileId='+objOTM.Opportunity.Owner.ProfileId);

        if(objOTM.User.ProfileId != objOTM.Opportunity.Owner.ProfileId && !mapOpportunityToUpdate.containsKey(objOTM.OpportunityId))
			mapOpportunityToUpdate.put(Id, new Opportunity(Id = objOTM.OpportunityId, Is_Diff_Profile__c = true));    
    }
    
    if(!mapOpportunityToUpdate.values().isEmpty())
        update mapOpportunityToUpdate.values();
}

/**I had misunderstood the requirement. Sorry. Try above code, if not working please let me know whether Profile Ids are retrieved properly in 10,11 line debugs.**/

 
kiran punurukiran punuru
Hi Nayana,

Iam getting error at this line : variable id does not exist
mapOpportunityToUpdate.put(Id, new Opportunity(Id = objOTM.OpportunityId, Is_Diff_Profile__c = true));

What is the ID here ?  

thanks
kiran
Nayana KNayana K
mapOpportunityToUpdate.put(objOTM.OpportunityId, new Opportunity(Id = objOTM.OpportunityId, Is_Diff_Profile__c = true));