I am using the standard price book for the order ,but my standard price book contains same product with different standard price.How to query the products in standard price book based on the Standard price?
[SalesForce] How to query the products in standard price book based on StandardPrice
Related Solutions
This might help you, I was looking for information on this myself and found the following information from the Salesforce boards quite useful.
How to insert/upload new Products, Price Books & Opportunity Line Item via Data Loader?
Questions this solution will help answer:
Step 1: How do I create and activate a Product Price Book?
Step 2: How do I load Products via the Data Loader?
Step 3: How do I load Products into Standard Price Book and Custom Price Books via the Data Loader?
Step 4: How do I load Opportunity Line Items via the Data Loader?
The following steps need to be taken in the exact order:
How do I create and activate a Product Price Book?
Step 1 - SET UP Price Books in Salesforce CRM Usually there are only a small finite set of price books, therefore this action can be done directly in the application. If you choose to use the Data Loader, use the "PriceBook2" table.
- In salesforce click on the Products Tab.
- In the bottom right-hand corner, click on "Manage Price Books"
- Add as many Price Books as necessary.
- The Standard Price Book MUST be active.
How do I load Products via the Data Loader?
Step 2a - PREPARE Products file (See attached template "Products_INSERT_Template_Step2a.csv")
- Make sure all Products have a Product Name -This is a required field when loading Products into the "Products2" Table
- If you use Record Types, you will need the "RecordTypeID" for the insert file -EXPORT "Record Type" table via the Force.com AppExchange Data Loader (you will need to check "Show all Sforce Objects"). This export will provide the 18 digit ID, which is needed for the insert.
- Make sure your column headers in the file exactly match fields in SFDC
Step 2b - INSERT Products to the "Products2" Table.
1.Open Data Loader 2. Insert to "Products2" table 3. Save success and error files in a folder for this project, you will use them in the Step 3b.
How do I load Products into Standard Price Book and Custom Price Books via the Force.com AppExchange Data Loader?
Step 3a - EXPORT "Price Book 2" Table You will need the "PriceBook2ID's" and other fields to prepare a file for the next step.
- Open the Data Loader
- Export the "Price Book 2" table
- You need "Price Book 2 ID" for insert file
- Save file in an easy to access location
- This will be used to create your final insert file for Step 3c and 3e
Step 3b - PREPARE file for Standard Price Book
NOTE: Even if you do not want to associate these Products with the Standard Price Book this step still must be followed.
Data Columns: -Product2ID: Use the "ID" from the "Product2" success file from Step 2 -PriceBook2ID: get the Standard Price Book ID from file created in Step 3a -CurrencyIsoCode: use Salesforce CRM currency symbol (only for Multi-Currency org) -Unit Price: this is the List Price/Standard Price field for the product -UseStandardPrice: this should be TRUE or FALSE and indicates whether to use the price from Standard Price Book or not. ----FALSE (only option for Standard Price Book) ----TRUE (is not an option because "Use Standard Price" field is not available in the Standard Price Book) -IsActive: TRUE (must be TRUE to insert - this can be changed to FALSE after Step 5 is complete)
Step 3c - INSERT into "Price Book Entry" Table
- Open the Data Loader
- Insert to "Price Book Entry" table
- Save success and error files in a folder for this project
Step 3d - PREPARE file for Custom Price Books Use the file from last step and modify it to have the Custom Price Book ID.
Data Columns: -Product2ID: Use the "ID" from the "Product2" success file from Step 2 -PriceBook2ID: get the Custom Price Book ID from file created in Step 3a -CurrencyIsoCode: use Salesforce currency symbol (only for Multi-Currency org) -Unit Price: this is the List Price/Standard Price field for the product -UseStandardPrice: this should be TRUE or FALSE and indicates whether to use the price from Standard Price Book or not. ----FALSE (this choice means you will use the Unit Price from the custom Price Book and NOT the Unit Price from the Standard Price Book) ----TRUE (this option only works if this Product has been added to the Standard Price Book. This choice will use the Unit Price from the Standard Price Book) -IsActive: TRUE
Step 3e - INSERT into "Price Book Entry" Table MAKE SURE your Custom Price Books are Active in SFDC.
- Open Data Loader
- Insert to "Price Book Entry" table
- Save success and error files in a folder for this project
Step 3f (optional) INACTIVATE Standard Products in Standard Price Book
- Take the success files the Standard Price Book Entry insert from step 3c
- Change the values in "IsActive" from true to FALSE
- Map only the "Product2ID" and "IsActive" fields
- Open AppExchange Data Loader
- UPDATE "Price Book Entry" Table
- Save success and error files in a folder for this project
How do I load Opportunity Line Items via the Data Loader?
Step 4 (optional) INSERT products into "OpportunitiesLineItem" Table
Data Columns: -Opportunity ID -PricebookEntryID -Total Price -Quantity -Unit price -other fields as needed NOTE: The Opportunity Product Table provides the "ProductID" as a mappable field, however this field should not be mapped in this process.
Export the Opportunity Product (OpportunityLineItem) table for a template
- Export the appropriate Opportunities from the data loader with filter criteria.
- Copy OpportunityID into the insert/update template file.
- Export the "Price Book Entry" table from the data loader, the "ID" column is the PricebookEntryID.
- Copy the appropriate PricebookEntryID's (with corresponding Opportunities) into the insert/update file.
- Add the other data into the relevant columns in the insert/update file.
- Open AppExchange Data Loader.
- Insert to "Opportunity Product (OpportunityLineItem)" table
Any special reason why you can't have a small trigger?
trigger oppWorkaround on Opportunity(before insert, before update){
for(Opportunity o : trigger.new){
if(o.Pricebook2Id == null){
o.Pricebook2Id = 'hardcoded id here';
}
}
}
Put the id of your standard pricebook and you're done (except developer sandboxes). Bit better way would be to waste 1 query to SELECT Id FROM Pricebook2 LIMIT 1
.
You will still need to specify PricebookEntries for products you want to be selectable (but since you mention standard prices I assume you're aware of that).
Related Topic
- [SalesForce] Difference between Product(Standard Price book) and Product (Saturn Price book) related list on Opportunity
- [SalesForce] Is that possible to see the list of Price Book Entries
- [SalesForce] Standard vs Standard Price Book
- [SalesForce] Find all Product2 records that do NOT have a PricebookEntry in the Standard Price Book
Best Answer
The other answers will work with one caveat. It will be difficult to unit test the code without using @SeeAllData=true. If you want to be able to cover your code with a unit test that does not use @SeeAlLData=true, but instead uses the new
Test.getStandardPricebookId()
, you will have to take a slightly different approach.The below is an example unit test that inserts a PricebookEntry with the standard Pricebook Id. It is followed by three approaches to writing the application code, each better than the previous.
Example unit test
Example application code (option 1 - fail)
The above fails to select anything because the Pricebook2 object itself is not visible. It was just the Id that was made available through the new
Test.getStandardPricebookId()
.Example application code (option 2 - ok)
Icky because you need Test.isRunningTest() in your application code, but works.
Example application code (option 3 - cleaner)
You could add a checkbox formula field to the PricebookEntry with a value of
{!Pricebook2.IsStandard}
. Your code would simplify to:The above works as the formula field value is evaluated correctly on the query.
I originally tinkered with this in this blog post I wrote a few months ago.