I am trying to perform upsert on opportunity based on external Id. I am building the opportunity objects based on sObject as I am using that function for other objects too.

Now Opportunity object contains the reference to Account and it has BasicId__c field which I am trying to update.

in order to dynamically update the Opportunity data and Account reference filed I have written the following code.

 SObject sObj = Schema.getGlobalDescribe().get('Opportunity').newSObject();

 //for normal Opportunity field (this works fine)

 //for reference field update `Account.BasicId__c`

 SObject sRefObj = Schema.getGlobalDescribe().get('Account').newSObject();

Now, when I perform an upsert after casting sobj to Opportunity it throws following error

INVALID_FIELD: Field name provided, BasicId__c is not an External ID or indexed field for Account

Not sure how would i able to update the reference field.

Kindly note

BasicId__c is not the external Id in Account

2 Answers 2


To be able to do this the field BasicId__c should be an External Id. Also, the parent record with that particular External Id should be present in the database.

  1. If you put the account in opportunity using sObj.putSObject('Account',sRefObj); it doesn't mean that it will update/insert account too. You need to put an account with the External Id field populated in it. Also, No other fields are allowed put, while you perform the DML. So when you insert opportunity, it will just update the account reference in it by searching the account using the populated external id field. Note you can not use any field other than external Id in this method, not even ID.
  2. Now, what if you don't have an external id field. you want to update/insert the account update it separately before inserting/updating the opportunity. So you need to put a Salesforce Id of an Account into Opportunity.


  • but it is not , it is just the normal field like First Name which I want to update , any alternative
    – Hunt
    May 29, 2020 at 11:54
  • Suppose I need to update the Name file in the account then? I can not convert that into external id
    – Hunt
    May 29, 2020 at 11:59
  • I guess You cannot convert the name field to an external id. first, insert the accounts, create the map of account the name field and id, then use that map to fill the accountId field on an opportunities. May 29, 2020 at 12:04
  • @Hunt I have updated my answer I hope you can understand that. Also, it's always better to put a comment mentioning what is wrong in the answer or what you don't understand before you put -1. May 29, 2020 at 14:04

You can't update a related object in a DML operation like this. You need to update the account and opportunity separately. The syntax you've attempted to use is used to set a lookup field by an External ID (i.e. your code is attempting to set AccountId to an Account that matches an External Id in the BasicId__c field). In other words, you need at minimum two DML operations here in order to update both records, since you can't mix data types in an upsert call.

You must log in to answer this question.

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