I am trying to create Opportunity Team Member using External id's for Opportunity and User.

First of all I create an instance of Opportunity and User using their respective External Id's

 User teamUser = new User(User_External_ID__c =reader.getText());
 Opportunity req = new Opportunity(Opportunity_External_ID__c = reqNumber);

I then create an instance of Opportunity Team Member

   OpportunityTeamMember teamMember = new OpportunityTeamMember(Opportunity = req, User = teamUser, TeamMemberRole = 'FM');

Once I do an upsert of for teamMember, it gives me an error saying

 VF_PAGE_MESSAGE OpportunityTeamMember: bad field names on insert/update call: User, Opportunity

 System.DmlException: Upsert failed. First exception on row 0; first error: INVALID_FIELD_FOR_INSERT_UPDATE, OpportunityTeamMember: bad field names on insert/update call: User, Opportunity: [User, Opportunity]

 VARIABLE_ASSIGNMENT [1675]|e|"common.apex.runtime.impl.DmlExecutionException: Upsert failed. First exception on row 0; first error: INVALID_FIELD_FOR_INSERT_UPDATE, OpportunityTeamMember: bad field names on insert/update call: User, Opportunity: [User, Opportunity]"|0x19dcd463

When I use UserId and OpportunityId instead of User and Opportunity

 OpportunityTeamMember teamMember = new OpportunityTeamMember(OpportunityId = req, UserId = teamUser, TeamMemberRole = 'FM');

It errors out saying expecting Salesforce id for Opportunity and User (which makes sense).

I am now not sure what I am missing when inserting Opportunity Team Members using external Id's for Opportunity and User.

  • What happens if you try setting the OpportunityId with req.Id and the UserId with teamUser.Id? Jul 1, 2013 at 11:25
  • It wont allow me to insert Opportunity Team Members as at this point req.Id and teamUser.Id is both null since the instance created only has there External ID's.
    – Yash Mehta
    Jul 1, 2013 at 13:30
  • And what if you use SOQL to get the Opportunity and User? Jul 1, 2013 at 14:35
  • 1
    Maybe this will help bobbuzzard.blogspot.co.il/2012/03/… Jul 2, 2013 at 6:16
  • 1
    Can't you bulk load the Opportunity and Users in one query and put them in a Map or something to use them later on? Jul 2, 2013 at 17:27

2 Answers 2


As I see you're concerned with running into governor limits, it would be wise to "bulkify" your queries. I'm not sure where your reader or reqNumber variables are coming from in your code snippet, so I'll improvise.

First, (as @Lex mentioned) you'll want to create a set of your User_External_ID__c values and a set of your Opportunity_External_ID__c values. Query these two objects separately and then build a map from the External ID to the standard Salesforce ID so you can reference them later. Once that's done, you should be able to loop through and generate a new OpportunityTeamMember instance and add it to a list so you can make a single insert call. And you're done, with only 2 queries and 1 DML statement used.

Something like this should be a good place for you to start:

// sets of external IDs
Set<String> userExtIds = new Set<String>();
Set<String> oppExtIds = new Set<String>();
// populate sets of external IDs
for ( SomeObject reader : readers ) {
// maps of external IDs -> SF IDs
Map<String, Id> userExtIdToSFIdMap = new Map<String, Id>();
Map<String, Id> oppExtIdToSFIdMap = new Map<String, Id>();
// populate user ID map
for ( User u : [select Id, User_External_ID__c from User where User_External_ID__c in :userExtIds] ) {
    userExtIdToSFIdMap.put(u.User_External_ID__c, u.Id);   
// populate opportunity ID map
for ( Opportunity o : [select Id, Opportunity_External_ID__c from Opportunity where Opportunity_External_ID__c in :oppExtIds] ) {
    oppExtIdToSFIdMap.put(o.Opportunity_External_ID__c, o.Id);
// build list of OpportunityTeamMember instances
List<OpportunityTeamMember> members = new List<OpportunityTeamMember>();
for ( SomeObject reader : readers ) {
    members.add(new OpportunityTeamMember(
                OpportunityId = oppExtIdToSFIdMap.get(reader.getReqNumber()),
                UserId = userExtIdToSFIdMap.get(reader.getText()),
                TeamMemberRole = 'FM')
// insert OpportunityTeamMember records
insert members;

Like I mentioned before you'll want to determine the best way to populate those external IDs. For the purpose of the code I just assumed your reader variable was an instance of SomeObject and reqNumber could be returned by calling reader.getReqNumber(). This might not be the case, so you'll have to take it from there.


Your code should be:

OpportunityTeamMember teamMember = new OpportunityTeamMember(OpportunityId = req.Id, UserId = teamUser.Id, TeamMemberRole = 'FM');

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .