8

If I have a trigger that works fine (generally), but which is generating false results due to VALIDATION RULES, how can I tell the trigger to ignore Validation rules (if at all)?

Scenario:

Trigger checks how many OTHER leads have the same email address, adds a marker to the current lead if it finds any at all.

I have validation rules (such as REQUIRING a field "Country_List__c" to be populated) which can be bypassed by, let's say, web-to-lead insertion, but which my trigger is not generating an ERROR FOR but it's falsely pulling a TRUE when checking for OTHER LEADS with the same email address.

I deduced this by editing and saving one of the problem records directly in SF GUI. It ran the trigger fine BECAUSE two Validation rules were handled (I forced fake phone number and I forced a country into Country List).

I'd like the trigger to IGNORE validation rules. Is this possible?

3 Answers 3

7

It is not possible for triggers to ignore validation rules without adding custom logic.

If the request to save a record originates outside of a Standard UI edit page, any before triggers you've defined will run prior to the execution of validation rules.

Unfortunately, once all the before triggers are finished, Salesforce will run, without exception, the System validation rules (ie. required fields, max length, etc.) and User-Defined validation rules (ie. the formula-based rules you've defined).

Here is one strategy to bypass this behavior:

  1. Add a "Skip Validation" Checkbox field to the object
  2. Set the "Skip Validation" field to TRUE in a before trigger.
  3. Add logic to your validation rules so that they do not execute if "Skip Validation" is set to TRUE.
6
  • Thanks. Makes perfect sense to put a backdoor into the validation rules in question. I just wish when I'm touching records via Data Loader that the error were more descriptive. This is my solution, thanks.
    – AMM
    Commented Oct 16, 2012 at 19:21
  • 3
    Add to that to UNCHECK the boolean in the validation rule, of course, so it doesn't permanently bypass that record, even in the GUI. ;-)
    – AMM
    Commented Oct 16, 2012 at 19:22
  • 2
    Validation Rules will run AFTER the before triggers. Check Order of execution salesforce.com/us/developer/docs/apexcode/Content/… Commented Oct 16, 2012 at 19:26
  • Custom validation rule will execute after the before triggers. system validation validations execute before triggers .Thanks Commented Oct 16, 2012 at 19:30
  • 2
    @techtrekker has a point. System Validation checks are only executed prior to BEFORE triggers when the record-save request comes from a Standard UI edit page. System Validation checks are not executed prior to BEFORE triggers when the record-save request comes from Visualforce pages or API calls. Commented May 8, 2013 at 15:19
6

You cannot make the trigger ignore validation rule failures, however you can add exclusions to the Validation Rule.

If you use Lead Source as 'Web to Lead' for Leads that are created via that channel, you could add an exclusion to the Validation Rule

You could add this to the Validation rules that you want to skip

NOT(AND( ISPICKVAL(LeadSource, 'Web to Lead'), ISNEW() ))

6
  • Good idea, though I'm going with a boolean check that will get Unchecked in the workflow after the fact (seen in Mohith's suggestion). Thanks!
    – AMM
    Commented Oct 16, 2012 at 19:23
  • The only thing is that if you use the Lead Source, and it is set to Web to Lead, you save yourself a new field. Commented Oct 16, 2012 at 19:24
  • Custom validation rule will execute after the before triggers.Standard UI validations execute before triggers .Thanks Commented Oct 16, 2012 at 19:27
  • Even using Lead Source, though, it would be a permanent ban on the rule, yes? I mean, unless you have a way to do it selectively, Lead Source X would always avoid the rule, whereas the idea was to be able to avoid the rule "when needed". Am I missing something? I don't want to bypass a possible alternative. ;-) Thanks.
    – AMM
    Commented Oct 17, 2012 at 14:34
  • I've added the ISNEW() check, which means the validation rule will be skipped only when the Lead is inserted and had a Lead Source of 'Web to Lead' Commented Oct 17, 2012 at 14:42
0

I handle this by specifying the user name of the process in the trigger and then use logic in the Validation rule that exempts that user

1
  • 1
    Unfortunately that is not the solution. It has been rectified with a two-field-marker system, thankfully (on the Account so that Opp triggers don't trip over themselves). The need involved BYPASSING the Trigger regardless of WHO trips it FROM THE ACCOUNT, while still enforcing it when editing the Opportunities directly. If they touch an Opp, validate it. If they touch an Account (which through TRIGGERS touches ALL the Opps to read them), let that happen. It was sticky. :-P Thanks.
    – AMM
    Commented Apr 10, 2013 at 13:59

You must log in to answer this question.

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