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
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 .
Austin AtkinsAustin Atkins
Annotated bibliography buy a research paper (https://www.bestcustomwriting.com/buy-research-paper). When I was studying in school, I thought writing a paper without using any information sources was possible. Then college quickly changed my opinion. Since that time, the thing I hate most in my life is creating a bibliography. I remember the time when I tried to include as few sources as possible, and once I was advised to try the annotated bibliography writing service. That decision was the best I've ever made.
JaneJLocaneJaneJLocane
Thanks for the detailed explanation. I wrote a term paper in this subject and it was difficult. But thanks to the feedback from friends, I turned to an essay writer https://www.bestessay.com/ and did not regret it. My work was completed on time and I began to order more voluminous tasks there.
tanuja danimatanuja danima
Thank you so much for the useful information.
Thank you for your reply.  I probably should have made this clear in my original post.
Good Night Images
Happy Birthday Brother Images
 
Humanter ManaHumanter Mana
Now easily, you can find the best team of professional medics, which I would recommend you now without any problems to find the best opportunity for yourself. Basically, it's really a little bit tricky, maybe the fact that finding a quality medical team for yourself is really quite a challenge I can tell you. How to solve first of all I would like to recommend you to go to This site https://www.valhallamedics.com/event-medical-staff-specialists-are-essential-at-every-event/ . This is where you will be able to find the best medical team for you at the moment.
yasmin eltutanyasmin eltutan
Daha önce SEO hizmeti almadıysanız, SEO Aydın (https://seoaydin.com.tr/) ajansı sizler için en doğru tercih olacaktır. Tecrübeleri ve kazanımlarıyla en doğru SEO hizmetlerini sunmaktadır.