3

I have a validation rule for the Opportunity object where in if the Opportunity has a OpportunityLineItem certain custom fields cannot be edited from the UI. Now I have a Trigger in theOpportunityLineItemwhere in If I try to Add/Edit/Delete anOpportunityLineItemthat is linked in it'sOpportunityIt will calculate those certain fields in theOpportunity` object.

I already created a bypass checkbox in the Opportunity object which is in it's default value = true.

Can Someone Guide me to complete this? Thank you in advance

This is my Validation Rule in The Opportunity Object

    (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)) ) &&  dummyCheckbox__c == True

and this is my Trigger in the OpportunityLineItem Object

trigger TotalOpportunityProductTrigger on OpportunityLineItem (after insert, after update, after delete) {

Opportunity Oppor = new Opportunity();
LPGCalculatorSettings__c objCalSetting = LPGCalculatorSettings__c.getValues('RollupTriggerSwitch');

if(objCalSetting.value__c){
TotalOpportunityProductTriggerHandler tp = new TotalOpportunityProductTriggerHandler();

if(Trigger.isInsert && Trigger.isAfter){

tp.TotalField(Trigger.new);
oppor.dummyCheckbox__c = False;
}
else if (Trigger.isUpdate && Trigger.isAfter){

tp.TotalField(Trigger.new);
oppor.dummyCheckbox__c = False;
}
else if(Trigger.isDelete && Trigger.isAfter){

tp.TotalField(Trigger.old);
oppor.dummyCheckbox__c = False;
}
}
}

Can someone help me to complete this one? I don't even know if I am in the right path. Thanks

1 Answer 1

1

There are two solution here. Instead of using Validation rule you can do same validation using trigger. It will give you better control.

Second is you need to set checkbox value false for all record not for just a single record

if(Trigger.isInsert && Trigger.isAfter){
//you need to set the checkbox field false here
//but as this code run in After syntax so first you need to query all Opportunity and then set it to false.
// and you need to do this in all if conditions.
tp.TotalField(Trigger.new);

}

This is not a recommend approach. Because you need to handle recursive trigger as well.

So Create a Trigger on Opportunity and put the validation there and a create a static Boolean variable and set it to false from Opportunity Line Item trigger and in Opportunity trigger check if this Boolean variable is false don't run the validation.

8
  • @TushurSharma. I found simpler solution where in I'll just create a workflow rule that will change the dummy checkbox back to false so that the validation rule will run again but I can't seem to make it work. Can you help me?
    – JMDF
    Commented Oct 26, 2016 at 14:50
  • @JMDF check Order Of Execution Validation rule come first then WF so before your WF update the checkbox Validation rule will give error and will stop the execution. So I don't think this approach will work. Commented Oct 26, 2016 at 15:19
  • Well Actually my plan is to do an before insert, before delete , before update where in it will change the dummyCheckbox = True, so that It will bypass the validation rule right and then create a workflow rule that will return the checkbox to false every a record is added, edited , deleted. Do you think this will work? By the way my Trigger is in after insert, after delete, after update. @TusharSharma
    – JMDF
    Commented Oct 26, 2016 at 15:23
  • @JMDF yes it will work but as you update the checkbox then your trigger will run again and before trigger again set it to false. So you need to handle that. Commented Oct 26, 2016 at 15:28
  • Hi @TusharSharma can you help me with this one so that I can trying doing my proposed solution to you? salesforce.stackexchange.com/questions/146052/…
    – JMDF
    Commented Oct 26, 2016 at 15:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .