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
SFDC DevlSFDC Devl 

Validation rule doesn't fire when manually updating a record but fires when trigger updates

Hello,

I have a validation rule to prevent users (other than profiles in formula) editing Contacts with certain criteria (criteria field) except two fields (field1 and field2). 

OR( 
AND( 
AND( 
$Profile.Id <> "xyz", 
$Profile.Id <> "xxx", 
), 
NOT(ISCHANGED(Field1__c)), 
NOT(ISCHANGED(Field2__c)), 
NOT(ISNEW()), 
NOT(ISCHANGED(criteriafield__c)), 
NOT(ISPICKVAL(criteriafield__c,'')) 
), 

AND( 
AND( 
$Profile.Id <> "xyz", 
$Profile.Id <> "xxx", 
), 
NOT(ISCHANGED(Field1__c)), 
NOT(ISCHANGED(Field2__c)), 
NOT(ISNEW()), 
ISCHANGED(Criteriafield__c), 
NOT(ISPICKVAL(PRIORVALUE(criteriafield__c),'')) 

)

I am testing as a user whose profile is NOT in the formula. When I manually edit Field1 or Field 2, I am able to update the contact successfully but when testing as same user, trigger update fails due to the above validation rule.

I have an Apex trigger which updates conList. conList = [SELECT Field1 FROM Contact WHERE ID IN: IdList];    

Am I missing something? I know how to prvent validation rules from trigger but I want to understand what is the difference between manually updating Vs trigger updating as a same user.

Can someone help me?

Thanks!
PratikPratik (Salesforce Developers) 
Hi,

Validation rule will fire when you manually update the record, it's not that it will fire when trigger is executed.

Check all your validation criterias, if criterias met, validation rule will be fired on manual update.

Thanks,
Pratik
SFDC DevlSFDC Devl

Thank you for your response! I know validation rules behaviour is same for manual update or system update. I hope below info helps.

The purpose of validation rule is to allow certain profiles (EXCEPT the profiles in formula) edit 2 fields on Contact and they should NOT be able to edit rest of the fields on Contact. Validation rules excludes those fields. I know there is a flaw here, when user tries to edit other fields along with those two fields (field1,field2), they will be able to save the record but I am ok with it becuase we dont have a need to put these fields on page layout. These fields are only updated by Apex trigger.

The validation rule I mentioned is not supposed to fire even when I am manually updating the Fields or trigger updating them as a user (whose profile is not included in the formula).

It is not firing when manually updating which is correct. When I am udating the same fields via trigger validation rule fires and update is failed which is incorrect.

When I monitor the debug logs, I observed validation rule behavior is different
Manual update - Validation rule PASS - CORRECT
Trigger update - Validation rule FAIL - INCORRECT
PratikPratik (Salesforce Developers) 
Hi,

Trigger runs in System mode i.e it will not check for permissions (as you are mentioning profiles). 
Validation Rule runs in User mode i.e. they will check for current user permissions.

As you are mentioning the profile in your validation rule, consider above point and try the scenarios based on that for testing.

Hope this will guide you1

Thanks,
Pratik