You need to sign in to do that
Don't have an account?
Lauren_H
Trigger to set pricebook on opportunity
This trigger I would think would be really easy, but I'm struggling, and new to triggers. How would I write a trigger to set the standard price book on every opportunity?
A much simpler trigger will do what you're looking for:
trigger InsertPriceBookTrigger on Opportunity (before insert) {
List<Pricebook2> stdPBL = [select id from Pricebook2 where IsStandard = TRUE];
if(!stdPBL.isEmpty()){
for(Opportunity o: Trigger.new)
o.PriceBook2Id = stdPBL[0].id;
}
}
All Answers
Hi Lauren,
Check the following link
http://salesforce.stackexchange.com/questions/7935/is-it-possible-to-default-a-pricebook-based-on-the-recordtype
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks,
Subhani,
Salesforce Certified Developer,
www.mydbsync.com
I did see that post, but wasn't quite the answer I'm looking for. I'm trying to modify this one: We are only using one pricebook for all record types, so if I remove all that information, it should work correct?
trigger InsertPriceBookTrigger on Opportunity (before insert) {
list<string> lstPricebookName = new list<string> ();
lstPricebookName.add('Sales Price book');
lstPricebookName.add('Service Price book');
Ilist<Pricebook2 > lstPricebook = [select id from Pricebook2 where Name IN:lstPricebookName and isActive='true'];
map<Name,ID> mapPricebook = new map<Name,id>();
if(lstPricebook!=null && lstPricebook .size()>0){
for(Pricebook2 objprice:lstPricebook){
mapPricebook.put(objprice.Name,objprice.id);
}
}
For(Opportunity Opp : Trigger.New ){
if(Opp.recordType.Name='Sales')
if(mapPricebook!=null && mapPricebook.get('Sales Price Book')!=null)
Opp.PriceBook2Id = mapPricebook.get('Sales Price Book');
else if(Opp.recordType.Name='Service'){
if(mapPricebook!=null && mapPricebook.get('service Price Book')!=null)
Opp.PriceBook2Id = mapPricebook.get('service Price Book');
}
}
}
A much simpler trigger will do what you're looking for:
trigger InsertPriceBookTrigger on Opportunity (before insert) {
List<Pricebook2> stdPBL = [select id from Pricebook2 where IsStandard = TRUE];
if(!stdPBL.isEmpty()){
for(Opportunity o: Trigger.new)
o.PriceBook2Id = stdPBL[0].id;
}
}