I have a Validation Rule
on the Opportunity
object where in if the Opportunity
has a OpportunityLineItem
certain custom fields cannot be edited from the UI. Now I have an Apex Trigger
on the OpportunityLineItem
where if I try to Add/Edit/Delete an OpportunityLineItem
, it will calculate those fields on the Opportunity
record.
I already created a Hierarchy Custom Setting
called MyCustomSetting__c
that has a checkbox field called Skip_Validation__c
. Why can I not bypass the Validation Rule
?
Validation Rule
HasOpportunityLineItem &&
(ISCHANGED( Total_List_Price__c)) ||
(ISCHANGED( Total_End_User_Price__c)) ||
(ISCHANGED( Total_End_User_Price_Discount__c)) ||
(ISCHANGED( Total_Standard_Price__c)) ||
(ISCHANGED( Total_Standard_Price_Discount__c)) ||
(ISCHANGED( Total_SPR_Price__c)) ||
(ISCHANGED( Total_SPR_Price_Discount__c)) ||
(ISCHANGED( Total_Price__c)) ||
(ISCHANGED( Total_Price_Discount__c)) ||
(ISCHANGED( Total_SPR_Margin__c)) && NOT($Setup.myCustomSetting__c.Skip_Validation__c)
Trigger
trigger TotalOpportunityProductTrigger on OpportunityLineItem (before insert, before update, before delete , after insert, after update, after delete) {
LPGCalculatorSettings__c objCalSetting = LPGCalculatorSettings__c.getValues('RollupTriggerSwitch');
MyCustomSetting__c tempSkipValidation = MyCustomSetting__c.getInstance();
if(tempSkipValidation == null) {
tempSkipValidation = new MyCustomSetting__c();
}
tempSkipValidation.Skip_Validation__c = true;
upsert tempSkipValidation;
if(objCalSetting.value__c){
TotalOpportunityProductTriggerHandler tp = new TotalOpportunityProductTriggerHandler();
if(Trigger.isInsert && Trigger.isAfter){
tp.TotalField(Trigger.new);
}
else if (Trigger.isUpdate && Trigger.isAfter){
tp.TotalField(Trigger.new);
}
else if(Trigger.isDelete && Trigger.isAfter){
tp.TotalField(Trigger.old);
}
}
tempSkipValidation.Skip_Validation__c = false;
update tempSkipValidation;
}
I already checked and reviewed the Apex order of execution and I don't think that it will affect my code.
before
triggers fire beforeValidation Rules
. It can be done.That is, you are trying to solve problem X, and you think solution Y would work, but instead of asking about X when you run into trouble, you ask about Y.