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
BrianWKBrianWK 

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.

crop1645crop1645

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

BrianWKBrianWK

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?

ahab1372ahab1372

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

BrianWKBrianWK

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
example:

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.

 

ahab1372ahab1372

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.

BrianWKBrianWK

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:)

ahab1372ahab1372

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.