[SalesForce] Bypass validation rule on parent object while updating parent object from Child object trigger

I have an issue in bypassing the validation rules, looked into all possible solution provided in the stackExchange for last one week, nothing is working for me. Please help me here.

Background:
– Child Object "Opportunity-service" has lookup relationship with Opportunity object.
– When inserting/updating "Opportunity-Service" record, a field "All-services" in Opportunity object is updated .
– Opportunity object has standard field "Close date" it has validation rule.
– Opportunity-service object has visualforce page and extention.
– Opportunity-service object has a trigger called 'OpportunityserviceTrigger'
– Opportunity object has a trigger 'OpportunityTrigger'.
– OpportunityServiceTrigger updates the Opportunity object – It has update DML operation.

Issue:
– When I insert or update Opportunity-service, it updates opportunity object('All-service' field) in opportunityServiceTrigger, performs validation rule on CloseDate in Opportunity object and operation fails.

** 2 Solutions I tried:**

Solution-1:

  • Created a customsettings field checkbox -'BypassCloseddateValRule' , default value is false(Unchecked). In opportunity-service trigger, I make checkbox BypassCloseddateValRule to true and update the opportunity. At end of the trigger, I make BypassCloseddateValRule to false.
    It is not working. Looks like it performs validation rules twice. First one goes throug fine. Second time, it fails

Solution2:
– Created a checkbox on Opportunity object – 'BypassCloseddateValRule'. Default value is false. I make this checkbox 'true' and update the opportunity in OpportunityServiceTrigger. Created workflow rule on opportunity object to update this field as 'false'.
It is also not working.

Can someone, please help me here.

Business context:

Below is the validation rule on closedate on opportunity object.
AND(
OR(
ISPICKVAL(StageName, "1 – Qualification"),
ISPICKVAL(StageName, "2 – Proposal Under Development"),
ISPICKVAL(StageName, "3 – Proposal Delivered"),
ISPICKVAL (StageName, "4 – Sales Presentation")),
CloseDate < TODAY(),
$Setup. Expected_Commitment_Date_Check__c = FALSE,
)

When new opportunity created/edited, it should check for following validation rule

For stages 1 to 4, opportunity close date must be greater than today. I have added a custom-setting field Expected_Commitment_Date_Check__c to bypass the validation if opportunity updated from OpportunityServiceTrigger.

We want users to have this validation rule error message while they are working on opportunity page when they make update/insert.
When users create/update any related object like opportunity-service, it should ignore this validation rule. Otherwise user will be overwhelmed by seeing opportunity validation rule error on Opportunity-service VF page.

Best Answer

The problem looks clear to me as you are facing a challenge with the small point in order of execution of salesforce.

To understand this you need to refer to order of execution in salesforce.

Triggers and Order of Execution

If you go to the link you'll find out in step 4 the custom validations are run once again after the before triggers which fails your logic.

STEP 4: Runs most system validation steps again, such as verifying that all required fields have a non-null value, and runs any user-defined validation rules. The only system validation that Salesforce doesn't run a second time (when the request comes from a standard UI edit page) is the enforcement of layout-specific rules.

To avoid the problem I would request you to not make the checkbox BypassCloseddateValRule, you created to bypass the validation rule to FALSE in the trigger.

You should do this change in a later step after which custom validations are not called, i.e. in STEP 7 (After Trigger) or STEP 11 (Workflow field Update).

Because custom validation rules will not be called after those steps.

STEP 12: If the record was updated with workflow field updates, fires before update triggers and after update triggers one more time (and only one more time), in addition to standard validations. Custom validation rules and duplicate rules are not run again.

I believe this will solve your problem. Let us know if you face any other challenge.

Thank You.

Related Topic