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
kuldeep paliwalkuldeep paliwal 

i want to write a trigger on a custom object(TestObject) which is related to opportunity when i add a user in given lookup field, it also add in Opportunity team member if that user already exist it replace old user if not than it update on OTM

Best Answer chosen by kuldeep paliwal
kuldeep paliwalkuldeep paliwal
trigger updateOppTeamMember on Resource_Request__c (after insert, after update) {
    // Write trigger on RR for insert and update triiger event
    List<id> OppoIdList = new List<id>();
    // create list of Oportunity
    map<id, id> OppoRRMap = new map<id,id>();
    // Create map for RR Id and its associated Opportunity ID
    map<id, Resource_Request__c> ResourceRequestMap = new map<id, Resource_Request__c>();
    // Create map for RR Id and Its associated RR Object
    for(Resource_Request__c rr :{
    // Itterate Over RR
    // Add RR OpporutunityID to its associated Opportunity
        OppoRRMap.put(rr.Id, rr.Opportunity__c);
    // Put RR Id and its associated RR Opportunity Id to OppoRRMap 
        ResourceRequestMap.put(rr.Id, rr);
    // Put RR id and its associated RR object to ResourceRequestMap
    List<OpportunityTeamMember> otmListForDelete= new List<OpportunityTeamMember>();
    List<OpportunityTeamMember> otmListForInsert= new List<OpportunityTeamMember>();
    List<OpportunityTeamMember> otmList = [Select Id, OpportunityId ,TeamMemberRole, User.Name From OpportunityTeamMember Where OpportunityId In:OppoIdList ];
    system.debug('--otmList --'+otmList );
    //Create List of OTM from fire a query on OTm where id is from OppoIdList
    map<id, List<OpportunityTeamMember>> OppoOtmMap = new map<id, List<OpportunityTeamMember>>();
    // Create map for Opportunity id and list of its associated OTM
    for(OpportunityTeamMember otm : otmList){
    // Itterate OTm with OtmList
        List<OpportunityTeamMember> tempList = OppoOtmMap.get(otm.OpportunityId);
    // get OTM Opportunty id from OppoOtmMap and store in tempList
        if(tempList!= null && tempList.size()>0){
    // Now we have to store OTM in templist if they are one or more or there are not in OTM 
    // if already one than add it        
        } else{
                tempList = new List<OpportunityTeamMember>();
     // If new than create new list
     // Than add OTM in tempList    
        OppoOtmMap.put(otm.OpportunityId, tempList);
      // Now we have templist with OTM, Put OTM ID and its associated OtmList in OppoOtmMap
        for(Resource_Request__c RR :{

        Boolean flag =false;
        Resource_Request__c oldRR = null;
        system.debug('--oldMap--' +Trigger.oldMap);
        if(Trigger.oldMap != null){
          oldRR = Trigger.oldMap.get(;
        List<OpportunityTeamMember> tempList1 = OppoOtmMap.get(rr.Opportunity__c);
        system.debug('--tempList1 --'+tempList1 );

            if(tempList1 != null){
                system.debug('--tempList1 --'+tempList1 );
                for(OpportunityTeamMember otm : tempList1 ){
                    if(otm.userId== oldRR.CRM_AssignedTeam__c || otm.userId== oldRR.Analyst_Contractor_lookup__c || otm.userId== oldRR.BD_Partner__c || otm.userId== oldRR.Delivery_Consultant__c || otm.userId== oldRR.HAT_Support__c || otm.userId== oldRR.Legal_Proposal_Support_assigned__c || otm.userId== oldRR.Presentation_QA_Assigned__c){

            if(tempList1 != null){
            for(OpportunityTeamMember otm : tempList1 ){
            if(rr.CRM_AssignedTeam__c== otm.userId){
                        flag = true;
            if(flag == true){
            // if flag is true then nothing will happen
            } else{
            // if flag is not true new OTM will be added
                    OpportunityTeamMember otm1 = new OpportunityTeamMember();
                    otm1.userId = rr.CRM_AssignedTeam__c;
                    otm1.TeamMemberRole= 'Sr. Client Insights Manager';
                    otm1.OpportunityId = rr.Opportunity__c;
            // add New OTM to otmList


        insert otmListForInsert;

        delete otmListForDelete;

Hear is an answer
Thanx for not given any answer