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
Stephen SiggsStephen Siggs 

INVALID_FIELD error upserting to Product2

I am getting the following error trying to Upsert to Product2: INVALID_FIELD: Field name provided, ProductCode does not match an External ID, Salesforce id, or indexed field for Product2.

ProductCode is definitely a field and it is showing as being indexed in the App Setup, Customize, Products area. What am I doing wrong!?

Note: using the SalesForce default sandbox setup - no customisations have been made.

I am sure that this is a daft newbie question, but I've been searching for ages and can't find an answer myself or an example of using upsert for Product2 that answer the question. Thanks in advance!
Best Answer chosen by Stephen Siggs
Ashish DevAshish Dev
Hi Stephen,
Upsert can be only used with ID, External ID or field having idLookup = true. ProductCode is not having idLookup = true, neither ID field nor External id hence can not be used for matching.

All Answers

Rohit B ☁Rohit B ☁
May be you are passing wrong value to ProductCode. Could you please paste your code here for better understanding?

Here is the sample code but without ProductCode.

Product2 p2 = new Product2(Name='Test Product', isActive=true);
insert p2;
Ashish DevAshish Dev
Hi Stephen,
Upsert can be only used with ID, External ID or field having idLookup = true. ProductCode is not having idLookup = true, neither ID field nor External id hence can not be used for matching.
This was selected as the best answer
Stephen SiggsStephen Siggs
Thank you Ashish. Can I set ProductCode idLookup = true? Or should I create a new field for ExternalID? what is best practice.

The purpose of the code I am writing is to sync the list of Product in SalesForce.com to the list of products that the User has in their ERP system.
Ashish DevAshish Dev
Stephen,
Here you should use External Id, which is very handy for storing primary key of external systems like ERP.
 
Stephen SiggsStephen Siggs
Thank you Ashish. Very useful advice.
Stephen SiggsStephen Siggs
Ok, so I tried using the ExternalId field and got an error: INVALID_FIELD: Field name provided, ExternalId does not match an External ID for Product2. The field is visible and editable for all Profiles in field level security.

Tried creating a custom External Lookup field and now getting an error: INVALID_FIELD: Field name provided, POProductCode__c does not match an External ID, Salesforce id, or indexed field for Product2. field is ticked as being indexed and is visible and editable for all Profiles in field level security.
 
Stephen SiggsStephen Siggs
Ok... so I just needed to create a regular text field with the External ID flag ticked... newbie!!! (doh!) Thank you for your help Ashish.