function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PreussenBlauPreussenBlau 

Apex Trigger Stopped Cold - Validation Rule is the Culprit

We have an apex trigger that updates account records with some contact data.  We also have a validation rule on the account record which prevents users from updating one field based on the absence of a value in another field.  Some of our account records are already in violation of the validation rule since the user area didn't want us removing data to comply with the rule but instead wanted to force users to enter more data when the go to update an account via the gui.

The issue occurs when a user updates a contact.  The apex trigger is activated on the contact update and tries to update the corresponding account record with data.  But the update fails if the validation rule kicks in and the existing account data is in violation of the validation rule.

Is it possible to add a line of formula code to a validation rule to recognize a trigger (as one would recognize a user profile NOT($Profile.Name = "System Administrator"), or role) and thereby avoid activating the validation rule?
PreussenBlauPreussenBlau
I've since recognized that the apex trigger is NOT prevented from udpating the account record with the contact data.  So there is not data issue.

The only result is an unneccesary email notification of the trigger update and the validation rule violation.  It would be nice to be able to avoid this but it's not a big deal.
TehNrdTehNrd
We have the same issue but in our case it is an OpportunityLineItem trigger updating Opportunity so this is happening alot. What is really need is a "isApex" function for validation rules. This would allow you to control if a validation rule is enforce when a record is modified by an Apex update, insert statement.

Most of the time our validation rules are for when a user edits a record but we want Apex to be able to update the record regardless of the validation rule. But if we wanted the rule to be enforced with apex we could do that too if there was an "isApex" function.

From a system standpoint I'm not sure if this is possible but it would take a thorn out of my side as any updates that are blocked with the rule and I have to update manually and that sort of defeats the purpose of apex.


Message Edited by TehNrd on 03-07-2008 01:43 PM
TehNrdTehNrd
FYI....

http://ideas.salesforce.com/article/show/10078198/ISAPEX_Function_for_Validation_Rules http://ideas.salesforce.com/article/show/10078198/ISAPEX_Function_for_Validation_Rules



Message Edited by TehNrd on 03-10-2008 01:00 PM
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.