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
John NeilanJohn Neilan 

Trigger to Update Opportunity Price Book

Hello,

I created the class below to fire on an Insert/Update trigger on the Opportunity. It is meant to change the Opportunity price book based upon the value entered in a custom picklist on the Opp. Everything saves and runs without error, but nothing gets changed. Am I missing something that would make the update? Thanks,
 
Public Class ClassSetPriceBook {

public void pbSet(List<Opportunity> SetPB){

ID pb1 = '01sK00000000y6x';
ID pb2 = '01sK00000000y72';
ID pb3 = '01sK00000000y77';
ID pb4 = '01sK00000000y7C';

String Edition1 = 'Edition-1';
String Edition2 = 'Edition-2';
String Edition3 = 'Edition-3';
String Edition4 = 'Edition-4';

for(Opportunity opp : SetPB) {
    if (opp.Edition__c == Edition1) {
        opp.Pricebook2Id = pb1;
    }

    else if (opp.Type == Edition2) {
        opp.Pricebook2Id = pb2;
    }

    else if (opp.Type == Edition3) {
        opp.Pricebook2Id = pb3;
    }

    else if (opp.Type == Edition4) {
        opp.Pricebook2Id = pb4;
    }
}
}
}

 
Pankaj_GanwaniPankaj_Ganwani
Hi,

Can you please share your trigger code as well?
John NeilanJohn Neilan
My trigger code is:
 
ClassSetPriceBook updater3 = new ClassSetPriceBook();
updater3.pbSet(Trigger.new);

It's a Before Insert, Before Update trigger
Pankaj_GanwaniPankaj_Ganwani
Hi,

I think your would be something like that:

trigger triggername on Opportunity(before insert, before update)//make sure these events are added.
{
       ClassSetPriceBook updater3 = new ClassSetPriceBook();
      if(Trigger.isBefore)
{
     if(Trigger.isInsert)
         updater3.pbSet(Trigger.new);
}
}