3

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;
        }
    }
    }
}
0

The problem is that only the first If statement uses opp.Edition__c as below:

if (opp.Edition__c == Edition1)

All the rest of them use opp.Type as below:

    else if (opp.Type == Edition2) {

As a result, you're only testing one condition for changing the pricebook being used and that's the only one that can be changed in the before insert trigger.

  • Thanks. I enter that code and now get the error on the update line: DML statement cannot operate on trigger.new or trigger.old: – John N. Apr 24 '15 at 18:10
  • Is this a BeforeInsert trigger and how are you calling the class? Also, do you want to change Pricebook2 or Opportunity? – crmprogdev Apr 24 '15 at 18:12
  • It's a before trigger (I tried changing to an after trigger with your changes, still no change being made) being called with the following line: ClassSetPriceBook updater3 = new ClassSetPriceBook(); updater3.pbSet(Trigger.new); It's meant to change the Pricebook2 that is associated with the Opp. – John N. Apr 24 '15 at 18:17
  • Okay, that makes a difference. We want to revise the contents of trigger.new using the class. You're not updating Pricebook2. Instead, you're assigning a different Pricebook to the Opp which gives different values to OLI for them, correct? I see the problem in your code now. Only the 1st If statement in your code uses Edition__c. – crmprogdev Apr 24 '15 at 18:43
  • Wow, that was a dumb mistake! Thanks so much. I made that change and it's working now! – John N. Apr 24 '15 at 18:50
0

If you are calling this class method before insert, then you don't need the DML in your class. You can just do something like the following:

Trigger calls Oppotunity_Utility.cls method:

//Before Insert Logic
if(trigger.isBefore && trigger.isInsert){
    Opportunity_Utility.setOpportunityPriceBook(trigger.new);
}

Apex Class Method:

public with sharing class Opportunity_Utility {
    public static void setOpportunityPriceBook(List<Opportunity> listOpportunity){

        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 : listOpportunity) {
            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;
            }
        }
    }
}

If you are calling this logic before update, you may run into issues if you are trying to change the Pricebook2Id on an opportunity that already has line items. Changing the Pricebook2Id value through the standard UI on an opportunity that already has line items will remove all of the products from your opportunity. You receive a warning message when attempting this from the standard UI - "You have modified the price book for this opportunity. All products related to this opportunity will be permanently deleted. Continue?".

  • Thanks. It does still give an error message when you try to change price books using my code. – John N. Apr 24 '15 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.