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
JennMJennM 

insert opportunity product

How do you insert an opportunity product?  The schema is very confusing, I think that I would find the PricebookEntryID and insert that?  But, I am getting an error that the PricebookEntryID is of the wrong type (MALFORMED_ID, Price Book Entry ID: id value of incorrect type: 01us0000001eOEkIAM)

 

 

opportunityId = newopp.id,

Quantity = 1,

ServiceDate = l.Donation_Close_Date__c,

PricebookEntryId =

OpportunityLineItem newoppprod = new OpportunityLineItem('01us0000001eOEkIAM');

insert newoppprod;

 

Best Answer chosen by Admin (Salesforce Developers) 
mtbclimbermtbclimber

There are lots of requirements for inserting opportunitylineitem records that aren't necessarily apparent at first glance, a couple of them to check into:

 

1) Is the PricebookEntryId you are using referring to a PricebookEntry object in the same Pricebook that the newopp opportunity object has set according to its Pricebook2Id field?

 

2) Are all the objects "active" , i.e. the Product2, Pricebook2 and PricebookEntry objects? They all have "IsActive" fields which need to be true I believe.

 

 

All Answers

Jeremy_nJeremy_n

The id looks okay, but here is what the statements would look like:

 

 

OpportunityLineItem newoppprod = new OpportunityLineItem(    
    opportunityId = newopp.id,
    Quantity = 1,
    ServiceDate = l.Donation_Close_Date__c,
    PricebookEntryId = '01us0000001eOEkIAM'
);
insert newoppprod;

 Generally you will also need a UnitPrice specified as well as all that. 

 

Jeremy

 

JennMJennM

Thanks. Somehow my post got messed up, but I have the same thing.  Is it failing because I didn't put in a unit price?  Why is the error saying the PricebookEntryID is the wrong type?

mtbclimbermtbclimber

There are lots of requirements for inserting opportunitylineitem records that aren't necessarily apparent at first glance, a couple of them to check into:

 

1) Is the PricebookEntryId you are using referring to a PricebookEntry object in the same Pricebook that the newopp opportunity object has set according to its Pricebook2Id field?

 

2) Are all the objects "active" , i.e. the Product2, Pricebook2 and PricebookEntry objects? They all have "IsActive" fields which need to be true I believe.

 

 

This was selected as the best answer
JennMJennM

Thanks.  I needed to insert the Pricebook2ID when I was adding the opportunity and then the newoppprod worked!