You need to sign in to do that
Don't have an account?
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.
Below is my trigger
and here is my test class
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 ]; } }
You can do this by updating your trigger code as below, Also the code is not properly written for bulk,
Hope this helps !!
--
Thanks,
Swayam
All Answers
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
How can I accoplish this with my code?
You can do this by updating your trigger code as below, Also the code is not properly written for bulk,
Hope this helps !!
--
Thanks,
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
Appreciate your continued support!
Regads,
Taitana
Thank you so much for your help!