You need to sign in to do that
Don't have an account?
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?
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
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
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!
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?
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.
I successfully ran the trigger provided by soof in the first response to this post. But since this is my first trigger, I am not clear on how to deploy it. I get an error that I need to add test coverage. Can you give sample code that would do this for this particular trigger? thanks.
It is vital though to ensure that the criteria is well mapped out. Otherwise the wrong price book could be used pretty easily.
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!
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.