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
Gaither SimmonsGaither Simmons 

Test Class unable to insert opportunity

In my test class I need to create an opportunity to test against. 

            //Create Opportunity
            // set up opp and Verify that the results are as expected.
            RecordType SigSales = [select Id from RecordType where Name = 'Sigma Sales' and SobjectType = 'Opportunity'];
            String opportunityName = 'My TEST Opportunity';

            Opportunity opp1 = new Opportunity(
                //AccountId=testAccount.Id, 
                Name=opportunityName, 
                StageName='Idea',
                CloseDate=Date.today(),
                RecordType=SigSales
            );
            insert opp1;

When I run the test I get the following error: 
11:12:43.788 (12788478666)|VF_PAGE_MESSAGE|You can't create an Opportunity Split for a user unless they're part of the opportunity team.
11:12:43.788 (12788578660)|EXCEPTION_THROWN|[77]|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, You can't create an Opportunity Split for a user unless they're part of the opportunity team.: [SplitOwnerId]
11:12:43.789 (12789451592)|HEAP_ALLOCATE|[77]|Bytes:196
11:12:43.797 (12797690786)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, You can't create an Opportunity Split for a user unless they're part of the opportunity team.: [SplitOwnerId]

Class.TestAddSAFromAccounttoOpp.myUnitTest: line 77, column 1
11:12:43.797 (12797713236)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, You can't create an Opportunity Split for a user unless they're part of the opportunity team.: [SplitOwnerId]

I am not attempting to create any splits. Not sure what is going on here. 

Thanks!
Gaither
James LoghryJames Loghry
Depending on how you have Opportunity Splits set up, it can by default create an Opportunity Split of 100% for the Opportunity's owner.  It should also add this user to the Opportunity Team though.  I would try creating a user record in your unit tess, one where the profile has access to Opportunities, and run your test as that user, using System.runAs().  If that doesn't work, you might have to create the user record and then create an OpportunityTeamMember for that User as well.
Mahesh DMahesh D
Hi Gaither Simmons,

Today similar kind of issue got fixed by adding the OwnerId field while inserting the Opportunity record.

Regards,
Mahesh
Jodi SpitlerJodi Spitler
This is a bit of an old thread, but I found something that would help here:  there is a checkbox on the opportunity split settings that allows you to add team members by adding a split.  I would assume (haven't tried it ;)) that would solve this problem in the test class.  If not, you could also add the person to the opp team in the test class before inserting the split.