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
BmartBmart 

Help with OpportunityTeamMember query and using the Team Member UserId values to a custom object.

Hi everyone. not a developer here so I struggle sometimes with code. I'm trying to create a trigger where when on the Opportunity object I flag the billed complete field (custom field, Jan Billed complete in this example) it queries the opportunity team members of the opportunity and then creates a new CSM Commission record (custom object).  I can get everything to work in terms of creating the CSM Commission record, but when creating the CSM record I'm having trouble getting the OpportunityTeamMember who's teammemberrole == 'Client Service Manager' from the CSMTeamMember. Can someone help me out and point out what I need to do? to get the values to feed over correctly? Thanks for your help in advance. 

trigger Opportunity_to_CSM_Commission on Opportunity (after update) {
// create a set of all the unique line item revision records based on WF ID
     
List<string> CSMIDS = new List<string>();  

       for (Opportunity o : Trigger.new)  
		{
           CSMIDS.add(o.id);   
        }
    
           
Date datobj =date.today();
Integer dy = datobj.day();
Integer mon = datobj.month();
Integer yr = datobj.year();
Integer lastyr = datobj.year()-1;    
           
 List <CSM_Commission__c	> CSMINSERTS = new list <CSM_Commission__c> ();	

 
 List <OpportunityTeamMember> CSMTeamMember=[Select OpportunityID, Userid, TeamMemberRole from OpportunityTeamMember Where Opportunityid =:CSMIDS ];
 




for (Opportunity Opp : Trigger.new)  {
	   	 
	if (/*I want to add a filter here where TeamMemberRole='Client Service Manager' && */OPP.Intl__c == false && OPP.Jan_Billed_Complete__c == true && OPP.Jan_CSM_Payment_Complete__c != true)
			
			{
		   CSM_Commission__c CSMCOM= new CSM_Commission__c ();
           CSMCOM.Opportunity__c = OPP.Id;
           CSMCOM.Name = 'IO#' + OPP.IO_Number__c + '- January' + yr + '- CSM Payment';
           CSMCOM.Jan_Billed__c = OPP.Jan_Billed__c;
          // CSMCOM.CSM__c = I want to get the OpportunityTeamMember userid where the teamMemberRole = 'Client Service Manager' and assign that person as the CSM__c user lookup value.
           //CSMCOM.OwnerId= I want to get the OpportunityTeamMember userid where the teamMemberRole = 'Client Service Manager' and assign that person as the Owner of the CSM Commission record.
           CSMINSERTS.add(CSMCOM); 
			}
			    
       		} 
		


try{

	insert CSMINSERTS;
}catch (System.Dmlexception e) {
	
}
}


Balaji BondarBalaji Bondar
Hi ,

Below code will help you out.Please udpate the code as per your business requirements:

trigger Opportunity_to_CSM_Commission on Opportunity (after update) {

Date datobj =date.today();
Integer dy = datobj.day();
Integer mon = datobj.month();
Integer yr = datobj.year();
Integer lastyr = datobj.year()-1;    

// create a set of all the unique line item revision records based on WF ID
   Map<Id,Map<String,OpportunityTeamMember>> opportunityIdOpportunityTeamMemberListMap = new Map<Id,Map<String,OpportunityTeamMember>>();  
	List <CSM_Commission__c	> CSMINSERTS = new list <CSM_Commission__c> ();	   
    for(Opportunity opportunityObj :[Select Id,(Select Id, TeamMemberRole From OpportunityTeamMembers) from opportunity where Id IN: Trigger.New]){
		Map<String,OpportunityTeamMember> OppRoleOpportunityTeamMemberMap = new Map<String,OpportunityTeamMember>();
		for(OpportunityTeamMember OpportunityTeamMemberObj : opportunityObj.OpportunityTeamMembers){
			OppRoleOpportunityTeamMemberMap.add(OpportunityTeamMemberObj.TeamMemberRole , OpportunityTeamMemberObj);	
		}
		opportunityIdOpportunityTeamMemberListMap.put(opportunityObj.Id , OppRoleOpportunityTeamMemberMap);
    }

	for (Opportunity Opp : Trigger.new)  {
		Map<String,OpportunityTeamMember> OppRoleOpportunityTeamMemberMap = new Map<String,OpportunityTeamMember>();
		CSM_Commission__c CSMCOM= new CSM_Commission__c ();
		CSMCOM.Opportunity__c = OPP.Id;
		CSMCOM.Name = 'IO#' + OPP.IO_Number__c + '- January' + yr + '- CSM Payment';
		CSMCOM.Jan_Billed__c = OPP.Jan_Billed__c;
		if(OppRoleOpportunityTeamMemberMap.containsKey(Opp.Id)) OppRoleOpportunityTeamMemberMap = OppRoleOpportunityTeamMemberMap.get(Opp.Id);
		
		if(OppRoleOpportunityTeamMemberMap.containsKey('Client Service Manager')){
			CSMCOM.CSM__c = OppRoleOpportunityTeamMemberMap.get('Client Service Manager').Id;
			CSMCOM.OwnerId= OppRoleOpportunityTeamMemberMap.get('Client Service Manager').Id;
		}
		CSMINSERTS.add(CSMCOM); 
	}

	try{
		insert CSMINSERTS;
	}catch (System.Dmlexception e) {}

}

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
BmartBmart
Hi Balaji, thank you for replying to my issue. I appreciate your help. When trying to save the code I get the following error. I double checked the mapping name and it appers to be fine, so I'm not sure what needs to be fixed here.

Save error: Method does not exist or incorrect signature: [MAP<String,OpportunityTeamMember>].add(String, SOBJECT:OpportunityTeamMember) Opportunity_to_CSM_Commission.trigger /Undertone Sandbox/src/triggers line 15 Force.com save problem