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
Tatiana B CookeTatiana B Cooke 

Getting Error when trying to blank out a Lookup field once a trigger has run to insert Opportunity Team Member.

Getting Error when trying to blank out a Lookup field once a trigger has run to insert Opportunity Team Member.Below is a trigger I wrote to create an Opportunnity Team Member when a lookup field called Secondary Owner is updated. The issue is when I try and blank out the lookup (Say if the secondary owner was input incorrectly) I get the following error. 
 
Error: Apex trigger OTOpportunityTrigger caused an unexpected exception, contact your administrator: OTOpportunityTrigger: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [User]: [User]: Trigger.OTOpportunityTrigger: line 13, column 1

Below is my trigger
trigger OTOpportunityTrigger on Opportunity (before update) {
    List<OpportunityTeamMember> listOpptyTeamMem = new List<OpportunityTeamMember>();
    
    for(Opportunity oppty : trigger.New){
        OpportunityTeamMember OTM = new OpportunityTeamMember();
        OTM.OpportunityId = oppty.Id;
        OTM.TeamMemberRole = 'Secondary Owner'; 
        OTM.UserId = oppty.Secondary_Owner__c;
        listOpptyTeamMem.add(OTM);   
        
       
            if(listOpptyTeamMem.size() > 0){
        insert listOpptyTeamMem;
       
         
        // get all of the team members' sharing recordsPost to Community
        List<OpportunityShare> shares = [select Id, OpportunityAccessLevel, 
          RowCause from OpportunityShare where OpportunityId = :oppty.Id
          and RowCause = 'Team'];
 
        // set all team members access to read/write
        for (OpportunityShare share : shares) 
          share.OpportunityAccessLevel = 'Edit';
 
        update shares;
    }
}
}

and here is my test class
 
@isTest(SeeAllData=true)
public with sharing class TestOTOpportunityTrigger {

    public static testmethod void test_1() {
             
            //create test account
            Account a  = new Account();
            a.name     = 'Teston';
            insert a;

            //create test opportunity 
            
        Opportunity o  = new Opportunity();
        o.OwnerID = '005i0000004LFZH';
        o.Name = 'Test';
        o.AccountID = '001i000001mCGvY';
        o.Unit_Number__c = 'a01i000000YWLGK';
        o.Financing_Type__c = 'Cash Buyer';
        o.Nature_of_Tenancy__c = 'Severalty';
        o.Sale_Type__c = 'Founder';
        o.CloseDate = Date.today();
        o.stageName = 'S1: Ready to Contract';
        o.Secondary_Owner__c = '005i0000004LFZH';
        
        
        insert o;
        
        OpportunityTeamMember OTM = new OpportunityTeamMember();
        OTM.OpportunityId = o.Id;
        OTM.TeamMemberRole = 'Secondary Owner'; 
        OTM.UserId = o.Secondary_Owner__c;   
        

            
            insert OTM;
             
             
            List<OpportunityTeamMember> OTMs = [SELECT id, UserId, OPPORTUNITYACCESSLEVEL
                                                FROM OpportunityTeamMember
                                                WHERE OpportunityId =: otm.Id
                                                ];
         
             
             
            //update O to execute the trigger
            update otm;
             
            //re-query
            OTMs = [SELECT id, UserId, OPPORTUNITYACCESSLEVEL
                    FROM OpportunityTeamMember
                    WHERE OpportunityId =: otm.Id
                    ];
             

        }

    }

 
Best Answer chosen by Tatiana B Cooke
Swayam@SalesforceGuySwayam@SalesforceGuy
Hi, 

You can do this by updating your trigger code as below, Also the code is not properly written for bulk, 
 
trigger OTOpportunityTrigger on Opportunity (before update) {
    List<OpportunityTeamMember> listOpptyTeamMem = new List<OpportunityTeamMember>();
    Set<Id> OpptyIdSet  =  new Set<Id>();
    
    for(Opportunity oppty : trigger.New)
    {
        //Checking Oppty SecondaryOwner 
        if(oppty.Secondary_Owner__c != null)
        {
            OpportunityTeamMember OTM = new OpportunityTeamMember();
            OTM.OpportunityId = oppty.Id;
            OTM.TeamMemberRole = 'Secondary Owner'; 
            OTM.UserId = oppty.Secondary_Owner__c;
            listOpptyTeamMem.add(OTM);
        }
        //Adding Oppty Id 
        OpptyId.add(oppty.Id);   
    }
        
       
        if(listOpptyTeamMem.size() > 0)
        {
        insert listOpptyTeamMem;
                
        // get all of the team members' sharing recordsPost to Community
        List<OpportunityShare> shares = [select Id, OpportunityAccessLevel, 
          RowCause from OpportunityShare where OpportunityId IN : OpptyIdSet
          and RowCause = 'Team'];
 
        // set all team members access to read/write
        for (OpportunityShare share : shares) 
          share.OpportunityAccessLevel = 'Edit';
 
        update shares;
        }

}

Hope this helps !!

--
Thanks,
Swayam




 

All Answers

Swayam@SalesforceGuySwayam@SalesforceGuy

Hi,

It is a required field, while creating oppotunity team member. Your are getting it as expected (Written in code)

What you want this code to behave?

Hope this helps !!

--
Thanks,
Swayam

Tatiana B CookeTatiana B Cooke
If I blank out the field I would like the record to be deleted or if it is easier, just blank out the field and leave the opportunity team member. 

How can I accoplish this with my code?
Swayam@SalesforceGuySwayam@SalesforceGuy
Hi, 

You can do this by updating your trigger code as below, Also the code is not properly written for bulk, 
 
trigger OTOpportunityTrigger on Opportunity (before update) {
    List<OpportunityTeamMember> listOpptyTeamMem = new List<OpportunityTeamMember>();
    Set<Id> OpptyIdSet  =  new Set<Id>();
    
    for(Opportunity oppty : trigger.New)
    {
        //Checking Oppty SecondaryOwner 
        if(oppty.Secondary_Owner__c != null)
        {
            OpportunityTeamMember OTM = new OpportunityTeamMember();
            OTM.OpportunityId = oppty.Id;
            OTM.TeamMemberRole = 'Secondary Owner'; 
            OTM.UserId = oppty.Secondary_Owner__c;
            listOpptyTeamMem.add(OTM);
        }
        //Adding Oppty Id 
        OpptyId.add(oppty.Id);   
    }
        
       
        if(listOpptyTeamMem.size() > 0)
        {
        insert listOpptyTeamMem;
                
        // get all of the team members' sharing recordsPost to Community
        List<OpportunityShare> shares = [select Id, OpportunityAccessLevel, 
          RowCause from OpportunityShare where OpportunityId IN : OpptyIdSet
          and RowCause = 'Team'];
 
        // set all team members access to read/write
        for (OpportunityShare share : shares) 
          share.OpportunityAccessLevel = 'Edit';
 
        update shares;
        }

}

Hope this helps !!

--
Thanks,
Swayam




 
This was selected as the best answer
Tatiana B CookeTatiana B Cooke
Thank you so much Swayam! 

I update the code to the above and am getting the below error when trying to save. 

Error: Compile Error: Variable does not exist: OpptyId at line 16 column 9
 
trigger OTOpportunityTrigger on Opportunity (before update) {
    List<OpportunityTeamMember> listOpptyTeamMem = new List<OpportunityTeamMember>();
    Set<Id> OpptyIdSet =  new Set<Id>();
    for(Opportunity oppty : trigger.New)
    {
        //Checking Oppty SecondaryOwner
        if(oppty.Secondary_Owner__c != null)
        {
            OpportunityTeamMember OTM = new OpportunityTeamMember();
            OTM.OpportunityId = oppty.Id;
            OTM.TeamMemberRole = 'Secondary Owner';
            OTM.UserId = oppty.Secondary_Owner__c;
            listOpptyTeamMem.add(OTM);
        }
        //Adding Oppty Id
        OpptyId.add(oppty.Id);  
    }
        if(listOpptyTeamMem.size() > 0)
        insert listOpptyTeamMem;
        // get all of the team members' sharing recordsPost to Community

        List<OpportunityShare> shares = [select Id, OpportunityAccessLevel,
          RowCause from OpportunityShare where OpportunityId IN : OpptyIdSet
          and RowCause = 'Team'];
          
        // set all team members access to read/write
        for (OpportunityShare share : shares)
          share.OpportunityAccessLevel = 'Edit';

        update shares;
}

Appreciate your continued support! 

Regads, 

Taitana
Tatiana B CookeTatiana B Cooke
Was able to get this to work with the below code. 

Thank you so much for your help!
 
trigger OTOpportunityTrigger on Opportunity (before update) {
    List<OpportunityTeamMember> listOpptyTeamMem = new List<OpportunityTeamMember>();
    Set<Id> OpptyIdSet =  new Set<Id>();
    for(Opportunity oppty : trigger.New)
    {
        //Checking Oppty SecondaryOwner
        if(oppty.Secondary_Owner__c != null)
        {
            OpportunityTeamMember OTM = new OpportunityTeamMember();
            OTM.OpportunityId = oppty.Id;
            OTM.TeamMemberRole = 'Secondary Owner';
            OTM.UserId = oppty.Secondary_Owner__c;
            listOpptyTeamMem.add(OTM);

        if(listOpptyTeamMem.size() > 0)
        insert listOpptyTeamMem;
        // get all of the team members' sharing recordsPost to Community

        List<OpportunityShare> shares = [select Id, OpportunityAccessLevel,
          RowCause from OpportunityShare where OpportunityId IN : OpptyIdSet
          and RowCause = 'Team'];
          
        // set all team members access to read/write
        for (OpportunityShare share : shares)
          share.OpportunityAccessLevel = 'Edit';

        update shares;
}