Conceptually, there is one Standard Pricebook and there are many custom Pricebooks. This allows you to do the following:
- For SKU xyz, the standard pricebook price could be $10.00
- For the same SKU xyz, on the pricebook called Commercial, the price is $10.00
- For the same SKU xyz, on the pricebook called US Government, the price is only $8.00
When you create a pricebookEntry for the junction between Product2 and Standard Pricebook, set useStandardPricebook
to false
. All pricebookEntries on standard Pricebook have useStandardPrice
= false
When you create a pricebookEntry for the junction between Product2 xyz and Pricebook US Government, set useStandardPricebook
to false
and set unitPrice to 8.00.
On custom pricebook called 'Commercial' where SKU xyz is $10.00, then when you create a pricebookEntry for the junction between Product2 xyz and Pricebook Commercial, set useStandardPricebook
to true
and set unitPrice to 10.00.
In effect, your updates to PricebookEntry are mimicing the SFDC Force.com user interface where you define standard pricebook prices for a Product and then, if you use custom pricebooks, you can choose to use the standard price or override the standard price, just for that pricebook.
Don't forget that all pricebook entries must be inserted against the Standard pricebook before you can insert pricebookEntries on custom pricebooks.
As the error states, you need to specify the opportunity id. Presumably, you'll find the opportunity attached to the order.
That probably looks like this:
trigger CreateOpportunityProduct on Order_Line_Item__c(after insert, after update){
if(trigger.isUpdate)
{
map<id, order__c> orders = new map<id, order__c>();
List<ID> pricebookID=new List<ID>();
List<ID> opportunityID=new List<ID>();
List<OpportunityLineItem> newopportunitylineitemlist=new List< OpportunityLineItem>();
for(order_line_item__c record: trigger.new) {
orders.put(record.order__c, null);
}
orders.putall([select id, opportunity__c from order__c where id in :orders.keyset()]);
for(Order_Line_Item__c orderlist: Trigger.new){
OpportunityLineItem Oppitem=new OpportunityLineItem();
Oppitem.Quantity=orderlist.Quantity__c;
Oppitem.UnitPrice=orderlist.UnitPrice__c;
oppitem.opportunityid = orders.get(orderlist.order__c).opportunity__c;
pricebookID.add(Oppitem.PriceBookEntryId);
opportunityID.add(Oppitem.OpportunityId);
newopportunitylineitemlist.add(Oppitem);
}
List<Order__c> orderlist= new List<Order__c>([Select Id,Opportunities__c From Order__c Where Opportunities__c IN:opportunityID]);
List<PricebookEntry> pricebookentrylist = new List<PricebookEntry>([Select Id,Product2Id From PricebookEntry Where Id IN:pricebookID]);
if(newopportunitylineitemlist.size()>0)
insert(newopportunitylineitemlist);
}
}
Adjust field names as necessary.
Best Answer
There's a lookup on the Opportunity to pricebook called Pricbook2ID. Just query and check to see if it null.
From: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_opportunity.htm
Pricebook2Id
Type:
reference
Properties:
Create, Defaulted on create, Filter, Group, Nillable, Sort, Update
Description
ID of a related Pricebook2 object. The Pricebook2Id field indicates which Pricebook2 applies to this opportunity. The Pricebook2Id field is defined only for those organizations that have products enabled as a feature. You can specify values for only one field (Pricebook2Id or PricebookId)—not both fields. For this reason, both fields are declared nillable.