0

I am developing an external application which will insert & update products and their pricebook entries into Salesforce system.

In order to avoid maintaining the Salesforce ProductId in my external application, I created a new custom field erpId__c on Product2 Object and marked it external. This way, I am able to successfully upsert products using Bulk API.

Now, I want to do the same with PriceBookEntry but it looks like while inserting pricebookentry, Salesforce ProductId is a required field.

Is there any way to avoid this limitation and create PriceBookEntry using my Product External Id?

I wrote below Apex code to quickly outline my problem but my real use-case will involve Bulk API.

PricebookEntry sp = new PricebookEntry();
sp.Pricebook2Id = '01s09000000kBb1AAE'; // standard pricebook
sp.erpId__c = '67890';
sp.UnitPrice = 20000;

insert sp;

Error:

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Product2Id]: [Product2Id]

1 Answer 1

1

PricebookEntry represents a product entry in a Pricebook, creating an association between Product & Pricebook (along with the unit price). Hence, ProductId, PricebookId & UnitPrice are required fields in this standard object and cannot be marked as not required. So, you cannot get around without specifying the required fields.

If you are performing DML on products and price books in the same Bulk API code, you can figure out the Salesforce ID corresponding to the products pretty easily (since, the ID will be available in the DML result) and use it to create PricebookEntry records.

If you need to be able to use the external ID of the product (instead of the standard Salesforce ID), create custom object for Pricebook & PricebookEntry objects (basically clone them) and implement the required functionality. Take a look at the product data model to understand what objects are/ can be related with price book.

You must log in to answer this question.

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