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
Sebastian ŻulińskiSebastian Żuliński 

Acces to specific pricebook and produckt paige

Hello,

I have a problem with JavaScript button which i wanna put to our opportunity paige to make faster acces 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 = lo
c;

with that code i can acces 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 choosen and validate.

Thx in advance.
Best Answer chosen by Sebastian Żuliński
Jan VandeveldeJan Vandevelde
Try this:
In following code replace the bold parts with id's from your org (pricdebookid is the id of the pricebook you would like set as default)

{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}

var pricebookid = "01sw0000000UGtr";
var opportunityId = "{!Opportunity.Id}";
var opportunityQuery = sforce.connection.query("Select Id, Name FROM Opportunity WHERE Id='" + opportunityId + "'");
var opptyResult = opportunityQuery.getArray("records");
opptyResult[0].Pricebook2Id = pricebookid;
var updateopptyResult = sforce.connection.update([opptyResult[0]]);

var loc;

loc = "/p/opp/SelectSearch?";

loc +=
"addTo={!Opportunity.Id}&" +
"retURL={!Opportunity.Id}&" +
"sfdc.override=1&" +
    "rolodexIndex=21";
window.top.location.href = loc;



I tried this in my org and works fine.
Please select as best answer if it helped you ;-)

All Answers

Jan VandeveldeJan Vandevelde
Try this:
In following code replace the bold parts with id's from your org (pricdebookid is the id of the pricebook you would like set as default)

{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}

var pricebookid = "01sw0000000UGtr";
var opportunityId = "{!Opportunity.Id}";
var opportunityQuery = sforce.connection.query("Select Id, Name FROM Opportunity WHERE Id='" + opportunityId + "'");
var opptyResult = opportunityQuery.getArray("records");
opptyResult[0].Pricebook2Id = pricebookid;
var updateopptyResult = sforce.connection.update([opptyResult[0]]);

var loc;

loc = "/p/opp/SelectSearch?";

loc +=
"addTo={!Opportunity.Id}&" +
"retURL={!Opportunity.Id}&" +
"sfdc.override=1&" +
    "rolodexIndex=21";
window.top.location.href = loc;



I tried this in my org and works fine.
Please select as best answer if it helped you ;-)
This was selected as the best answer
Sebastian ŻulińskiSebastian Żuliński
Really BIG thx Jan !!

Your code work perfect. This is what exactly i wanna :)

best regards
Sebastian
Jan VandeveldeJan Vandevelde
Just an addition, I suppose you have multiple pricebooks. The code above always sets to the same pricebook, you would need an if statement if there is already a pricebook selected ofcourse.

So here is the modified code snippet:

{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}

var pricebookid = "01sw0000000UGtr";
var opportunityId = "{!Opportunity.Id}";
var opportunityQuery = sforce.connection.query("Select Id, Name FROM Opportunity WHERE Id='" + opportunityId + "'");
var opptyResult = opportunityQuery.getArray("records");
if(opptyResult[0].Pricebook2Id != null){
opptyResult[0].Pricebook2Id = pricebookid;
var updateopptyResult = sforce.connection.update([opptyResult[0]]);
}


var loc;

loc = "/p/opp/SelectSearch?";

loc +=
"addTo={!Opportunity.Id}&" +
"retURL={!Opportunity.Id}&" +
"sfdc.override=1&" +
"rolodexIndex=21";
window.top.location.href = loc;