I am facing an issue while writing a test class for Opportunity trigger handler class. We have enabled and Opportunity Team member and Opportunity Split process in our org. In the split process, 100% of the amount will be assigned to the Opportunity owner. Now in the test class, in one of the testmethod we are updating the amount of the Opportunity and test class is failing with the below error.

System.DmlException: Update failed. First exception on row 0 with id 006xxxxxxxxxxxxx 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]

To get rid of this error, I created a test user with System Admin profile and run the whole code under System.runAs(testuser) and set the Opportunity owner to testuser.id. I have just made below two query to check if the opportunity creation inside a test class by default creates the Opportunity Team member and Opportunity Split.

List<OpportunitySplit> optySplit = [SELECT Id,OpportunityId,SplitAmount,SplitOwnerId,SplitPercentage FROM OpportunitySplit 
     where OpportunityId=:testOpportunity.Id];
     system.debug('split size'+optySplit.size());
     List<OpportunityTeamMember> optyTeam = [SELECT Id,OpportunityAccessLevel,OpportunityId,TeamMemberRole,UserId FROM OpportunityTeamMember
      where OpportunityId=:testOpportunity.Id];
    system.debug('team member size'+optyTeam.size());

Split size return 1 but team member size returns 0. As the error suggested , now I am trying to create a Opportunity Team member using the below code snippet it throws the below error:

    OpportunityTeamMember teamMember1 = new OpportunityTeamMember();
    teamMember1.OpportunityId = testOpportunity.Id;
    teamMember1.OpportunityAccessLevel = 'Edit';
    teamMember1.TeamMemberRole = 'Opportunity Owner';
    teamMember1.UserId = testuser.Id;
insert teamMember1;

System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, To add this user to the team, first ask the opportunity's owner to share the opportunity with the user.: []

As the error suggested, I tried to give access to the user by creating a new record in the OpportunityShare object, but getting compile error saying

        OpportunityShare optyShare = new OpportunityShare();
        optyShare.OpportunityId = testOpportunity.Id;
        optyShare.UserOrGroupId = testuser1.Id;

Field is not writeable: OpportunityShare.UserOrGroupId

OWD of opportunity is set as Public Read/Write. AccountTeamMember is not enabled in our org. Could you please let me know what went wrong here. I am stuck here for last 2 days.

Thanks Somnath


As you are using apex code, so while using apex code you need to add the rowcause as Manual as you are manually adding an Users share in the opportunity and the opportunity accesslevel field .

Just have to add the below for your code to work.

Oppshare.rowcause = 'manual'; Oppshare.opportunityaccesslevel = 'read/write';

Please go through this document https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_opportunityshare.htm

for more information on the same.

  • getting the same compilation error : Field is not writeable. – Somnath Paul Feb 28 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.