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

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;





it is "With Sharing", the other way round. If you specify with Sharing, user permission will be respected, otherwise they will be ignored.


ahab - I'm not sure that's correct. According to the Apex Developer's Guide "without sharing" keywords ensure that sharing rules are not enforced. "with sharing" keyboards enforce sharing rules. What I"m not clear on from the guide is what happens if you don't declare either sharing -- the text below implies to me that the default is "with sharing" unless called from a different class.

Use the with sharing keywords when declaring a class to enforce the sharing rules that apply to the current user. For

public with sharing class sharingClass {
// Code here


Classes, Objects, and Interfaces Using the with sharing or without sharing Keywords
Use the without sharing keywords when declaring a class to ensure that the sharing rules for the current user are not
enforced. For example:
public without sharing class noSharing {
// Code here


If a class is not declared as either with or without sharing, the current sharing rules remain in effect. This means that if the
class is called by a class that has sharing enforced, then sharing is enforced for the called class.



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.


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:


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.