+ Start a Discussion
Nikhil Nair 1Nikhil Nair 1 

Inserting/updating OpportunityLineItem using REST API

Hello everyone,

The SalesForce REST API doc is unclear about the usage of APIs to insert and update records in OpportunityLineItem. The standard fields in OLI include OpportunityId and Product2Id both of which I'm populating by retrieving the appropriate SalesForce IDs. I receive this error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY when I try to insert a record at this URI: /sobjects/OpportunityLineItem/.

Going through the flow: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_erd_products.htm#topic-title, I'm assuming this (and also the cross-reference error) means that there is no direct relationship between OLI and Products.

Firstly, why is there a Product2Id field if the relationship doesn't exist?
Secondly, is the URI I'm using incorrect? If so, what is the correct one?
Thirdly, can this problem be solved using APIs at all?

Maybe I'm going about this in a completely inefficient manner. Any suggestions would be greatly appreciated.
Best Answer chosen by Nikhil Nair 1
Keyur  ModiKeyur Modi
Hi Nikhil, 

In standard way, you have to pass pricebookEntry id, in opportunitylineItem product2Id is not writable field so, if you want to insert opportunityLineItem then you need to create instance of opporunitylineItem mentioned below.
OpportunityLineItem oli = new OpportunityLineItem(OpportunityId= opp.Id, PricebookEntryId = pbe.Id, Quantity = 1);

Please try with this , and if you have any issue then please let me know. 

Thanks, 
Keyur Modi
 

All Answers

Keyur  ModiKeyur Modi
Hi Nikhil, 

In standard way, you have to pass pricebookEntry id, in opportunitylineItem product2Id is not writable field so, if you want to insert opportunityLineItem then you need to create instance of opporunitylineItem mentioned below.
OpportunityLineItem oli = new OpportunityLineItem(OpportunityId= opp.Id, PricebookEntryId = pbe.Id, Quantity = 1);

Please try with this , and if you have any issue then please let me know. 

Thanks, 
Keyur Modi
 
This was selected as the best answer
Hari M 22Hari M 22
Hi keyur and Nikhil, 
Using Rest API iam trying to get opportunity  line item data/v47.0/sobjects/OpportunityLineItem but it is not returning any object .how to get opportunity lineitem id .