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
LaurenP6777LaurenP6777 

Enabling OpportunitySplits broke Test Codes in Sandbox

I recently turned on Overlay Opportunity Splits in one of my sandboxes. Now Test Code that previously worked is erroring with the following:

INVALID_CROSS_REFERENCE_KEY, Unable to create an opportunity team member for new opportunity split user.: [SplitOwnerId]

As you can see the code below isn't trying to insert a Team Member or OpportunitySplit so I've been going crazy trying to fix the issue. When I create an opportunity manually, an OpportunitySplit is NOT automatically created. I am stumped. 
@isTEst
Private class TestUpdateStormStatusonOppty{

    static testMethod void  UpdateStormStatus()
    {
    //create a user to run the test as
    
    Profile p = [Select id from Profile where name = 'CDS'];


User u = new User(alias = 'mo123', email='mousey.1234@covance.com',
      emailencodingkey='UTF-8', lastname='123', languagelocalekey='en_US',
      localesidkey='en_US', profileid = p.Id, country='United States',
      CVD_Division__c='CDS', User_Type__c='AE',
      timezonesidkey='America/Los_Angeles', username='mousey.1234@covance.com');
  insert u;

 //create an oppty with required fields
  RecordType RecType = [Select Id From RecordType  Where SobjectType = 'Opportunity' and DeveloperName = 'CDS'];
  
  Opportunity o = new Opportunity(Name='TestMethodMouse1',
        AccountId='001C000000nGrv3',
        Compound__c='a08C000000Gva2H',
        RecordTypeId=RecType.Id,
        CloseDate=System.Today(),
        StageName='Prospect',
        NextStep='test',
        Study_Phase__c='PreClinical',
        Probability_of_Win__c='10%',
        Integrated_Opportunity2__c='No',
        Primary_Contact_Role_Assigned__c=true);
        insert o;

        
        RFI_Status__c CSWA = new RFI_Status__c(
        Opportunity__c=o.id,
        Storm_Status__c='Current Opportunity',
        Triage_Outcome__c='No Go');
        
        Test.startTest();
        insert CSWA;
        Test.stopTest();
        

        Opportunity op=[SELECT Related_Storm_Status__c, Id FROM Opportunity WHERE Id=:o.id];
        System.assertEquals(op.Related_Storm_Status__c,'Current Opportunity');
        
}
}
Best Answer chosen by LaurenP6777
LaurenP6777LaurenP6777
I figured it out. The code needs to specifically declare the Opportunity Owner Id when creating the Opportunity. 

OwnerId=u.id
 
@isTEst
Private class TestUpdateStormStatusonOppty{

    static testMethod void  UpdateStormStatus()
    {
    //create a user to run the test as
    
    Profile p = [Select id from Profile where name = 'CDS'];


User u = new User(alias = 'mo123', email='mousey.1234@covance.com',
      emailencodingkey='UTF-8', lastname='123', languagelocalekey='en_US',
      localesidkey='en_US', profileid = p.Id, country='United States',
      CVD_Division__c='CDS', User_Type__c='AE',
      timezonesidkey='America/Los_Angeles', username='mousey.1234@covance.com');
  insert u;

 //create an oppty with required fields
  RecordType RecType = [Select Id From RecordType  Where SobjectType = 'Opportunity' and DeveloperName = 'CDS'];
  
  Opportunity o = new Opportunity(Name='TestMethodMouse1',
        AccountId='001C000000nGrv3',
        Compound__c='a08C000000Gva2H',
        RecordTypeId=RecType.Id,
        CloseDate=System.Today(),
        StageName='Prospect',
        NextStep='test',
        Study_Phase__c='PreClinical',
        Probability_of_Win__c='10%',
        Integrated_Opportunity2__c='No',
        Primary_Contact_Role_Assigned__c=true,
        OwnerId=u.id);
        insert o;

        
        RFI_Status__c CSWA = new RFI_Status__c(
        Opportunity__c=o.id,
        Storm_Status__c='Current Opportunity',
        Triage_Outcome__c='No Go');
        
        Test.startTest();
        insert CSWA;
        Test.stopTest();
        

        Opportunity op=[SELECT Related_Storm_Status__c, Id FROM Opportunity WHERE Id=:o.id];
        System.assertEquals(op.Related_Storm_Status__c,'Current Opportunity');
        
}
}

 

All Answers

Mahesh DMahesh D
Hi Lauren,

We have a similar post earlier:

https://developer.salesforce.com/forums/?id=906F0000000ApANIA0

Please try to run it using the runAs() method and see if it works.

Also check the split settings for the below check box:

Let users add members to opportunity teams while editing splits. --- Checked / Unchecked

Regards,
Mahesh
LaurenP6777LaurenP6777
I’m not having any luck with the “runAs” method. Also, when I uncheck “Let users add members to opportunity team while editing splits” and run the test method, I get the following error: INVALID_CROSS_REFERENCE_KEY, You can't create an Opportunity Split for a user unless they're part of the opportunity team.: [SplitOwnerId]
LaurenP6777LaurenP6777
I figured it out. The code needs to specifically declare the Opportunity Owner Id when creating the Opportunity. 

OwnerId=u.id
 
@isTEst
Private class TestUpdateStormStatusonOppty{

    static testMethod void  UpdateStormStatus()
    {
    //create a user to run the test as
    
    Profile p = [Select id from Profile where name = 'CDS'];


User u = new User(alias = 'mo123', email='mousey.1234@covance.com',
      emailencodingkey='UTF-8', lastname='123', languagelocalekey='en_US',
      localesidkey='en_US', profileid = p.Id, country='United States',
      CVD_Division__c='CDS', User_Type__c='AE',
      timezonesidkey='America/Los_Angeles', username='mousey.1234@covance.com');
  insert u;

 //create an oppty with required fields
  RecordType RecType = [Select Id From RecordType  Where SobjectType = 'Opportunity' and DeveloperName = 'CDS'];
  
  Opportunity o = new Opportunity(Name='TestMethodMouse1',
        AccountId='001C000000nGrv3',
        Compound__c='a08C000000Gva2H',
        RecordTypeId=RecType.Id,
        CloseDate=System.Today(),
        StageName='Prospect',
        NextStep='test',
        Study_Phase__c='PreClinical',
        Probability_of_Win__c='10%',
        Integrated_Opportunity2__c='No',
        Primary_Contact_Role_Assigned__c=true,
        OwnerId=u.id);
        insert o;

        
        RFI_Status__c CSWA = new RFI_Status__c(
        Opportunity__c=o.id,
        Storm_Status__c='Current Opportunity',
        Triage_Outcome__c='No Go');
        
        Test.startTest();
        insert CSWA;
        Test.stopTest();
        

        Opportunity op=[SELECT Related_Storm_Status__c, Id FROM Opportunity WHERE Id=:o.id];
        System.assertEquals(op.Related_Storm_Status__c,'Current Opportunity');
        
}
}

 
This was selected as the best answer