2

How can we bypass validation rule when using the oob merge process. I can't figure out what field identifier to use as my criteria on validation rule that will tell me that user is processing merge and validation rule should be bypass. please advise

2 Answers 2

2

Take the advantage of order of execution of triggers ,validation rules and workflows.

1)Your custom validations run after before triggers

2)Your workflow rules run after your triggers .

3)Triggers and Merge Statement document https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_merge_statements.htm

Using the above conclusion ,create a boolean field on the record named "ByPassValidation" and avoid this from pagelayout .

As per document when merge happens ,after delete trigger on looser record gives a MasterRecordId ,Query records with Ids equivalent to MasterRecordId and update the boolean ByPassValidation=true

Now In validation rules use this flag and AND your current validation with this field .Make sure by default your ByPassValidation is unchecked .

Draft a workflow rule now to again uncheck the ByPassValidation flag .In the entire process there can be chance for trigger to fire once more due to wrokflow making an update ,Use Static boolean to avoid this condition .

1

I tried Mohith's solution, but in after delete trigger, it does not allow to update MasterRecord as its part of the merge operation which is currently in progress.

To overcome this, I created a static SET with master record ids in after delete trigger, and wrote a before update trigger, which checks if the Static SET contains the record id, then it set's the flag to TRUE for the record.

And it worked fine, but another problem was, in merge operation, the workflow does not fire for Master Record, so the flag was not reset, it remains flagged.

To overcome this, I wrote another before update trigger method, which will reset the flag always if its TRUE and not in Static Map.

In this case, the record will be flagged as ByPassValidation after merge, but as soon as user tries to update it, it will reset the flag in before trigger, and all validation rules will be applicable as in regular process.

1
  • Thank you, Adrian!
    – Tarun Jain
    Commented Jun 22, 2021 at 18:58

You must log in to answer this question.

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