You need to sign in to do that
Don't have an account?

Can a trigger/class ignore validation rules?
I have a field that I want to be 90% controlled by some APEX code - but I still want the user to be able to select 1 value. All other values would be set by the code.
The issue I"m having is setting a validation rule that will either be ignored when updated via the trigger/class or have the Trigger/Class simply ignore the validation rule.
I thought I could get this by using "Without Sharing" but I must have mis understood that function.
Any thoughts? The closest I got is to put a hidden field that gets turned on by the trigger to ignore the validation, and then have a workflow or an after trigger than turns that field back off. Seems a bit much.
My solution when faced with this is to eliminate the Validation Rule and do the consolidated validation in a Before trigger - one that tosses an error if the <validation condition> fails.
Although you lose the easy point-and-click feature to rapidly change validation rules, you gain by writing a test method that can cover all your cases and on subsequent deployments you have greater confidence you haven't broken something inadvertently
Crop1645 - There's an idea. Do you customize the error? What do you use to do that? I assume it's part of the
Try{ Action;
}Catch{
setup?
it is "With Sharing", the other way round. If you specify with Sharing, user permission will be respected, otherwise they will be ignored.
if you do not define the sharing at all, it defaults to "without sharing", unless your class is called by another class that has "With Sharing".
In other words: if you never write anything, it will be "without sharing".
If you do specify "With sharing", it will be applied to the class itself and all other classes called from that class.
http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/pages_security_tips_data_access_control.htm
Data Access Control
[...]
When using an Apex class, the built-in profile permissions and field-level security restrictions are not respected during execution. The default behavior is that an Apex class has the ability to read and update all data with the organization. Because these rules are not enforced, developers who use Apex must take care that they do not inadvertently expose sensitive data that would normally be hidden from users by profile-based permissions, field-level security, or organization-wide defaults. This is particularly true for Visualforce page
Also here:
http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_classes_keywords_sharing.htm
Apex scripts generally run in system context, that is, the current user's profile-based permissions, field-level security, and sharing rules are not taken into account during script execution.
Ahab - thanks for the clarification.
It's still all about data accessibly and visibility. I originally thought it would let me ignore the validation rules applied to a field - but that's not the case. Instead, it's controls what data is visible and accessible to Apex based on the running user.
Unless I'm still missing something:)
one thing that might work (haven't tried) is making the picklist dependend. You can then make only a few values available to the users. but I am not sure if this will throw an error when you try to assigne values through Apex which are not made available in the dependency setup.
Another option is that you assign values to the picklist field that are not defined in the picklist. What I mean is that the picklist only has one value, e.g. "value1", but on your code you assign a string value "value2". AFAIR that works, at least if you run it in system context. Disadvantage is that reports will not respect any non-alphabetical sort order you have defined in the picklist.