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
zen_njzen_nj 

is there a way that validation rule can be ignored when an action is done via a trigger?

I have set up validation rules for our Case objects so that in certain scenario (i.e. when certain fields in the Case is a specific value), then only specific Users can Close out the case. The validation rule will essentially check that if Status is Closed, and if the $User.Department is not a specific value, then the validation rule will force an error and let the User trying to close out the case know he is not authorized to do this.

However, based on user request, I know have designed triggers that if certain action occurs, it will then try to close out certain Cases. And what's happening is that in many situation, the User that is initiating the action does not have the proper $User.Department value. And so when the trigger fires off, it will get stopped by the validation rule.

But the logic we want is that if the action that is causing the Case to be closed is coming from a trigger, then the validation rule can be ignored.  Is there some global variable I can use in my validation rule so that instead of something like:

AND(
ISPICKVAL(Status,"Closed"),
$User.Department != "FIX Account Manager",
)

I can add something like:
AND(
ISPICKVAL(Status,"Closed"),
$GlobalVariableIndicatingItscomingFromTrigger=false,
$User.Department != "FIX Account Manager",
)


I know in SQL code, we can have @@NESTLEVEL and other variables to provide information on transaction level. So was wondering if there is something in salesforce apex environment that would contain similar info that I can use to bypass validation rules when the action that is triggering the validation rule is coming from a trigger?