3

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.

2
  • 1
    What is your validation rule and the business purpose for it? Know what it is and why it is there will help drive an accurate answer
    – Eric
    Oct 15, 2015 at 14:41
  • @Eric Business context is added to original question. As comment doesn't allow more information
    – Razu
    Oct 15, 2015 at 16:16

3 Answers 3

2

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.

3
  • I was looking at debug log, it has following code - CODE_UNIT_FINISHED|OpportunityTrigger on Opportunity trigger event BeforeUpdate for all validation rules. I don't see any AFTERUPDATE event being executed. Not sure why it behaves wiered.
    – Razu
    Oct 15, 2015 at 22:00
  • That is the problem Razu, You are having all operation in your before trigger on Opportunity which fires the custom validations for next time. I would say either add a after update block and in that block you make your BypassCloseddateValRule to false or you use a workflow rule which makes your BypassCloseddateValRule to false because after that custom validations will not fire. Dont make this BypassCloseddateValRule to false in your before context. Oct 16, 2015 at 7:34
  • Were you able to fix the problem Razu? Oct 18, 2015 at 5:44
0

I would suggest you to change the Validation rule check if there is any change to CLosed date field then only fire.

Like for example `"PRIORVALUE(CloseDate)!=CloseDate)"

0

I have reset the custom setting field 'BypassCloseddateValRule' after the save() command in custom controller of 'Opportunity-Serivce'. It worked fine.

Following flow fixed the issue.

  1. Custom controller - 'Opportunity-Service'.
  2. Save() function inside the custom controller
  3. Opportunity-Service trigger - set the 'BypassCloseddateValRule', Update Opportunity Object
  4. Opportunity Trigger
  5. Reset - 'BypassCloseddateValRule' inside the custom controller
  6. end of Custom controller - 'Opportunity-Service'

Following Flow - that was causing the issue

1.Custom controller - 'Opportunity-Service'. 2.Save() function inside the custom controller 3.Opportunity-Service trigger - set the 'BypassCloseddateValRule', Update Opportunity Object function, 4.Opportunity Trigger 5. reset the 'BypassCloseddateValRule' inside the Opportunity-service trigger after opportunity object update. 6.end of Custom controller - 'Opportunity-Service'

You must log in to answer this question.

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