Vandevelde Jan

Certified Salesforce Consultant

For Admins

Skip the pricebook selection page and go directly to productsearch depending on fieldvalues

In case you use different pricebooks and products in your Salesforce org, there may come a time that you wish you could set a pricebook and go to the product search page without having the user select a pricebook from a click of a button. 

There was someone on the developer forum who had such a need and posted the following question

Access to specific pricebook and product page

Hello,I have a problem with JavaScript button which i wanna put to our opportunity page to make faster access to specific products. Code looks like this:

var loc;
loc = “/p/opp/SelectSearch?”;
loc += “addTo={!Opportunity.Id}&” +
“retURL={!Opportunity.Id}&” +
“sfdc.override=1&” +
“rolodexIndex=21”;
window.top.location.href = loc;

with that code i can access to specific place but i don’t know how to skip step with choosing pricebook when i have a pricebook ID. If someone can help me with info how i can put to code pricebook ID to identify it and skip manual pricebook validate for actual opportunity. My code work fine when pricebook is already chosen and validate.

This got me thinking and I could think about some use cases for that too like:

  • Select a pricebook depending on a value of a picklist, check box or specific field on the opportunity
  • Select a pricebook depending on a userfield
  • Select a pricebook depending on the regio of an account
  • Or just plain old time reduction. You just want to skip the pricebook selectionscreen

So I started up my developer org, searched the web a bit and came up with a working solution for this kind of problem.

The solution is creating a custom button on your opportunity or the opportunity product related list and replace the standard “choose Price Book” and “Add Product” buttons by one

Create the new button and choose:

  • Display Type: Detail Page Button
  • Behaviour: Execute JavaScript
  • Content Source: OnClick JavaScript

In the formula editor you can copy/paste (and change at your convenience) the following code snippet. This example will always look if there is a pricebook linked to the opportunity already or not. Then it will set a pricebook based on the standard picklist field “Type” on the opportunity.

You can easily modify the fields and values written in uppercase to suit your needs.

{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
 
 
var pricebookid1 = "PRICEBOOK4NEWCUSTOMERS";
var pricebookid2 = "PRICEBOOK4OTHERS";
 
if("{!Opportunity.Type}" == "New Customer"){
 
var opportunityId = "{!Opportunity.Id}";
var opportunityQuery = sforce.connection.query("Select Id, Name, Type, Pricebook2Id FROM Opportunity WHERE Id='" + opportunityId + "'");
var opptyResult = opportunityQuery.getArray("records");
if(opptyResult[0].Pricebook2Id == null){
opptyResult[0].Pricebook2Id = pricebookid1;
var updateopptyResult = sforce.connection.update([opptyResult[0]]);
}
}
if("{!Opportunity.Type}" != "New Customer"){
 
var opportunityId = "{!Opportunity.Id}";
var opportunityQuery = sforce.connection.query("Select Id, Name, Type, Pricebook2Id FROM Opportunity WHERE Id='" + opportunityId + "'");
var opptyResult = opportunityQuery.getArray("records");
if(opptyResult[0].Pricebook2Id == null){
opptyResult[0].Pricebook2Id = pricebookid2;
var updateopptyResult = sforce.connection.update([opptyResult[0]]);
}
}
 
 
var loc;
 
loc = "/p/opp/SelectSearch?";
 
loc +=
"addTo={!Opportunity.Id}&" +
"retURL={!Opportunity.Id}&" +
"sfdc.override=1&";
window.top.location.href = loc;

Can you think of some other use cases? Or do you have suggestions to optimize my code? Feel free to leave a comment ????

 

Add comment


Security code
Refresh

On Twitter

Contact Info

Jan Vandevelde

Ninoofsesteenweg 42

9450 Denderhoutem

België

Mobile: +32 (0) 472 58 47 67

 

Email Address: contact@vandeveldejan.be

VAT BE 0882 025 057

Certifications