No, simulating another user/profile is not possible in the scenario you describe (and an undesirable platform feature for 3rd party code security reasons). You should always build backdoors into hard-fail validation rules for this reason. I prefer doing this to enforcing the logic via triggers because then the validation rules are still maintained via clicks admin, and you can do things like add error messages to specific fields.
The pattern I follow for Sys Admin manual overrides is to exclude the privileged profile(s) from the validation rule altogether (a la twamley's note). But that doesn't get you what you need, which is a code-only override.
For situations where only code is allowed to bypass a validation rule, you've mentioned the option of a hidden field but I'd suggest a significant tweak to it. I create a (18,0) numeric field called something like "Apex Updated" that is hidden from page layouts and read-only for FLS for user profiles. In any validation rule you want a code backdoor to, you just add a check for NOT(ISCHANGED(Apex_Updated__c)). Then, in your code, to bypass those validation rules you just set Apex_Updated__c to System.currentTimeMillis(). This has the advantage of not having to reset fields, or worse yet, missing the workflow to reset them and ending up with records that are permanently exempt from validation rules.
You say it's a backup plan, but it's just one custom field, one line of Apex code (to set the field when a bypass is desired), and one line of validation rule modification (to bypass the rule when the field is changed).
Use a hierarchical custom setting.
The setting just has two fields.
Location -> Lookup to user/profile
Skip_Validation__c -> Set it to true to skip validation.
Update your validation rules to not fire when Skip validation = true for the custom setting.
&& NOT($Setup.MyCustomSetting__c.Skip_Validation__c)
As the last step, your trigger shuts off validation at the start and enables it at the end.
MyCustomSetting__c tempSkipValidation =
MyCustomSetting__c.getInstance();
if(tempSkipValidation == null) {
tempSkipValidation = new MyCustomSetting__c();
}
tempSkipValidation.Skip_Validation__c = true;
upsert tempSkipValidation;
//WHATEVER TRIGGER NORMALLY DOES HERE
tempSkipValidation.Skip_Validation__c = false;
update tempSkipValidation;
Best Answer
I used the info from these links to get the answer. I hope this helps people that stumble on this issue in the future: