+ Start a Discussion
jgrenfelljgrenfell 

Excluding trigger updates/inserts from validation rules

Does anyone know of a way to determine that an insert/update is being done by a trigger in a validation rule, for instance if there's a different User for changes made by a trigger? I've hit a couple instances where Triggers fail because they don't pass a validation rule, so I'm hoping there's a way I can exclude a trigger in the validation rule itself, like AND(user != triggeruser, validation rule...).
mtbclimbermtbclimber
There is no explicit way to conditionally "disable" a validation formula. You can, of course, change the validation rule to look for something that is set in your trigger(s) that makes the data "valid" according to your rules.
hemmhemm

We have been able to handle this in the following way:

 

  • Added a Date field (any type would be fine) onto the object called"Validation Override".
  • Via Field Level Security, we made this field only visible to the System Administrator profile.  We could make it visible to no profiles, but we want the the flexibility of using this same trick with Data Loader and in the UI.
  • Our validation rules include the criteria NOT( ISCHANGED( Validation_Override__c ) ).
  • For our triggers that update records that will potentially hit validaiton rules, the code incrememnts the date value.  This bypasses the Validation Rule for that 1 save.


I find it easy to use date fields, but you could use a number field and increment it or a string and just generate a random string each time to put in there.  The point is that you have a field that will change and only System Admins (or code running as System) can modify.