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
TehNrdTehNrd 

OpportunityLineItem delete trigger doesn't execute on Pricebook change?

When you change a pricebook on an opportunity all products are deleted.

So you would think that any delete triggers on OpportunityLineItem would execute, but they do not.

Is this by design?
LegerdemainLegerdemain
The change you mention is by design as it invokes a cascade delete. The safest alternative to this in the interim would be to perform the logic you want in your line item delete triggers in the opportunity update trigger when you detect a change of pricebook2id since you know that will blow away the line items. For multi-currency, the same would be true for currency code changes so you may want to look for that change as well if applicable.
TehNrdTehNrd
Thank you, that helps alot.

Since you say "interim" does this mean that a cascade delete will someday eventually cause triggers to execute?
mtbclimbermtbclimber
Cascade delete is not something we are currently planning to handle. This is a special case of cascade delete that may be handled at some point though given this workaround it's probably not necessary.
SiriusBlackOpSiriusBlackOp

Oddly, I was just testing some code and I'm not sure you can detect a change in the Pricebook2Id for the opportunity.  I created a Before Update trigger on Opportunity and for some reason when I change the pricebook for any given opportunity (trigger.old[i].Pricebook2Id) is equal to (trigger.new[i].Pricebook2Id).  They both always report the new value.

 

It's acting like it changed it prior to executing the the before update trigger.  What Gives?  How are you supposed to detect a change in the pricebook if the new values are always equal to the old values?  Any ideas or workarounds out there?

SiriusBlackOpSiriusBlackOp

OK.... Figured it out.  You can detect the pricebook change in the New Opportunity Save Behavior, but you Can't in the Old Opportunity Save Behavior.

 

This could be a dangerous issue for some apps.  Look out! :smileysurprised: