+ Start a Discussion

Trigger Help needed for opp members


I need to write a trigger for the below scenario as follows,

Add After Upsert trigger code (SasLineItemAfter) on SAS_Line_Item__c object to upsert SAS_Line_Item__c .SAS_Assigned_SA__c into the SAS_Line_Item__c.Engagement_Script__c.Opportunity__c Sales Team.  We are not going to remove Sales Team if the SAS_Assigned_SA__c is changed to somebody else. Watch for Sales Team validtions that may come up. We don’t want those validations to just pop unhandled in the inline VF. We should trap those errors gracefully.
Add SA(s) from the Assigned SA field of the TF to the Opportunity Sales Team (on the Opportunity) with:
  1. Status = Active
  2. Team Role = Tier 2/3 SA
  3. Opportunity Access = Read/Write
Note: if multiple SAs found, put all them in the Opportunity Sales Team
Existing Assign SAs will not be added to the Opportunity Sales Team

I have written th e below trigger . but not sure if its right , kindly help me on this  issue

trigger OTOpportunityTrigger on SAS_Line_Item__c (before update) {
    List<OpportunityTeamMember> listOpptyTeamMem = new List<OpportunityTeamMember>();
    Set<Id> OpptyIdSet =  new Set<Id>();
    for(SAS_Line_Item__c sas : trigger.New)
        //Checking Oppty SecondaryOwner
        if(sas.SAS_Assigned_SA__c != null)
            OpportunityTeamMember OTM = new OpportunityTeamMember();
            OTM.UserOrGroupId = sas.SAS_Assigned_SA__c;
            OTM.Status ='Active';
            OTM.TeamMemberRole = 'Tier 2/3 SA';
            OTM.OpportunityAccessLevel = 'Read/Write';

        if(listOpptyTeamMem.size() > 0)
        insert listOpptyTeamMem;


Kindly help me pls

Thanks in advance