+ Start a Discussion
ChristiaanChristiaan 

Is it possible to make the price book dependent on the type of opportunity

I have activated the opportunity products (and 2 price books) in my sandbox environment and have activated "Prompt users to add products to opportunities"

 

After making a new Opportunity, in step 2 users have to select a pricebook before products can be added to the opportunity.

 

In our situation the two price books (professionals Services and projects) are dependent on the type of Opportunity: (professional Services and project). It would be very user friendly if the price book automaticly would be chosen dependent of the opportunity type that is filled in at the new Opportunity (professional services and projects).

 

At this moment users have to fill in what type of opportunity it is, but again have to select what pricebook belongs to that opportunity type. That's not ideal.

 

Is there a solution to automate this?

 

soofsoof

Try the following trigger:

 

trigger setPriceBook on Opportunity (before insert) {

ID PRICEBOOK_PROF_SERV = '<Professional Services Pricebook2 ID>';

ID PRICEBOOK_PROJECTS = '<Projects Pricebook2 ID>';

ID REC_TYPE_PROF_SERV = '<Professional Services RecordTypeId>';

ID REC_TYPE_PROJECTS = '<Projects RecordTypeId>';

for( Opportunity oppty : trigger.new ) {

if ( oppty.RecordTypeId == REC_TYPE_PROF_SERV ) {

oppty.Pricebook2Id = PRICEBOOK_PROF_SERV;

}

else if ( oppty.RecordTypeId == REC_TYPE_PROJECTS ) {

oppty.Pricebook2Id = PRICEBOOK_PROJECTS;

}

}

}

 

If this doesn't work, let me know what error you see.  Don't forget to set the correct IDs in the variables on the top! :)  Hope this helps!

 

-soof

 

ChristiaanChristiaan

Hi Soof,

 

Thanks for your reaction. Your solution is a big step forewards, but I have one question.

 

The solution is based on different record types (Professional Services, Projects). We don't use separate record types, but only a picklist (field name is Type) in the opportunity. Is it possible to make this trigger dependent on the picklist value instead of the record type?

 

With kind regards,

 

Christiaan

soofsoof

Yes, it sure is!  You just need a few minor changes to the code I posted earlier... try this:

 

trigger setPriceBook on Opportunity (before insert) {

ID PRICEBOOK_PROF_SERV = '<Professional Services Pricebook2 ID>';

ID PRICEBOOK_PROJECTS = '<Projects Pricebook2 ID>';

String TYPE_PROF_SERV = 'Professional Services';

String TYPE_PROJECTS = 'Projects';

for( Opportunity oppty : trigger.new ) {

if ( oppty.Type == TYPE_PROF_SERV ) {

oppty.Pricebook2Id = PRICEBOOK_PROF_SERV;

}

else if ( oppty.Type == TYPE_PROJECTS ) {

oppty.Pricebook2Id = PRICEBOOK_PROJECTS;

}

}

}

 

Hope this helps! 

 

-soof

 

sparx--sparx--

Hey There,

 

I am trying to use this code to help me do the same thing, but in Service Contracts.  Can someone help with the code to make the following happen:

 

 - Set the pricebook for the line items on a Service Contract based on a custom picklist. 

 

Example:

 - User creates a new Service Contract and during the creation they select "reseller" as the type of contract.  When they go to add line items to the Service Contract, I only want line items from the Reseller Pricebook to be displayed. 

 

Thoughts on how I can do this?

Nicky123Nicky123

Hi,

       I am trying  to do the following-

I have one custom 'Opportunity' field called Project which is a lookup to Custom Object 'Project' . Now I want to do the the functionality that if the project named 'xyz' is selected, then only the single pricebook named 'xyz' need to be displayed in picklist while selecting the pricebook for that Opportunity.

       Can anybody help me doing the same???

 

Thank you.

bettymacbettymac

I successfully ran the trigger provided by 

DEVENDRA RAIDEVENDRA RAI
I have a similar requirement. We have Campaigns for which we need to associate multiple pricebooks ( for this I created a custom object (Offer). Now when a Order is created out for a campaign,and some one tries to select a pricebook at addproduct step, we want to constrain the pricebook matching to offer list. Can some one suggest how to constrain the pricebook.
Jason OrbisonJason Orbison
Coding a trigger is not neccessary. Use process builder and set the criteria as the Record Type. The update will be the Opportunity object and the field will be Price Book ID. Leave it as text and use the id of the price book to update the opportunity. Then Activate.

It is vital though to ensure that the criteria is well mapped out. Otherwise the wrong price book could be used pretty easily. 
David García GallegoDavid García Gallego
Hello guys, 

I have another issue related to this question. 
I'd like to do the same but for the 'Order' standard Object. I mean, i have 2 different record types for order and 2 different record types for pricebooks, depending on the record type selected in orders. 

This is the code I'm writing

trigger setPriceBook on Order (before insert) {

ID PRICEBOOK_MUESTRAS = '0124E0000005DyQ';

ID PRICEBOOK_PRODUCTOS_ESTANDAR = '0124E0000005E7I';

ID REC_TYPE_PEDIDO_DE_MUESTRAS = '0124E0000005DyB';

ID REC_TYPE_PEDIDO_ESTANDAR = '0124E0000005Dr5';

for( Order order : trigger.new ) {

if ( order.RecordTypeId == REC_TYPE_PEDIDO_DE_MUESTRAS ) {

order.Pricebook2Id = PRICEBOOK_MUESTRAS;

}

else if ( orderitem.RecordTypeId == REC_TYPE_PEDIDO_ESTANDAR ) {

orderitem.Pricebook2Id = PRICEBOOK_PRODUCTOS_ESTANDAR;

}

}

}


Any ideas? Thanks in advance guys!
sfdc1.3890582999007844E12sfdc1.3890582999007844E12
@davidgarciagallego  I reccomend not hard coding values. It is definitely not best practice. It woud be better to use a map or list  than hardcode. This way the code works no matter what org you are in. 

What I did is create a picklist in pricebooks. For example, piclist values can be (Muestras,Estandar,Muestras) then in your code you map your pricebooks

Map<String,Pricebook2> pbs = new Map<String,Pricebook2> ();

  for(Pricebook2 pb : [Select Id,pbytpes__c from Pricebook2 where isactive = true and pbytpes__c != null])
            pbs.put(pb.pbytpes__c ,pb);

for( Order o : trigger.new ) {
Pricebook2 pbk = pbs.get(o.opportunitytypefield__c);
o.pricebook2id = pbk.id;
}

Don't forget to make sure that you key has values, you need to add the if statement. You may need to add the pricebook picklist and then map it. This is how I map all my pricebooks. Sales has a required field and that field is mapped using pricebook field. Only one query and no hard coding.