+ Start a Discussion
stratus adminstratus admin 

User ID null

Can anyone see why the following would produce a NULL value for the User ID in the following. The purpose of this is to set the Account Owner on the Opportunity Sales team.


trigger SetOpportunityAccountOwner on Opportunity (after insert, after update) {
 OpportunityTeamMember[] newMembers = new OpportunityTeamMember[]{};
 OpportunityShare[] newShares = new OpportunityShare[]{};

 for (Opportunity o : trigger.new)
    if (o.OwnerId != o.Account.OwnerId)
        OpportunityTeamMember m = new OpportunityTeamMember();
        m.OpportunityId = o.id;
        m.UserId = o.Account.ownerid;
        m.TeamMemberRole = 'Account Owner';




USER_DEBUG|[14,9]|DEBUG|test: OpportunityTeamMember:{OpportunityId=006S0000003lpdcIAA, UserId=null, TeamMemberRole=Account Owner}


ERROR ADDING OPPORTUNITY TEAM MEMBER:OpportunityTeamMember:{OpportunityId=006S0000003lpdcIAA, UserId=null, TeamMemberRole=Account Owner}::Database.Error[getFields=(User);getMessage=Required fields are missing: [User];getStatusCode=System.StatusCode.REQUIRED_FIELD_MISSING;]


Thanks in advance.




The objects passed in the trigger context have none of the related objects populated, so o.Account is null and therefore o.Account.OwnerId is null. You'll have to run a query to get the accounts ownerId.