1

I'm having some issues with the following validation rule:

AND( 
OR(
ISCHANGED(Sales_Rep__c),
ISCHANGED(Secondary_Rep__c)
),

$User.Id<>"005A0000005sOAv", /*User1 */ 
$User.Id<>"005A0000007TTqR" /*User2 */ 

)

The intended function is that it is supposed to restrict the editing of either sales rep field, unless you're one of the 2 listed users trying to edit it. If you are one of these two, editing of the field is allowed.

Initial error resolved, was comma.

Issue to be resolved: Is there a way to reference a user in a validation rule other than by their ID? In Apex I know I can query. How should I go about not hard-coding in a validation rule?

3
  • 1
    You don't need coma after $User.Id<>"005A0000007TTqR", /*User2 */ . Also, hardcoding Ids is not recommended
    – o-lexi
    Aug 12, 2016 at 19:30
  • That resolved the issue, thank you. @Oleksiy - How would I go about referencing users in a validation rule without hardcoding an ID?
    – Genko
    Aug 12, 2016 at 19:31
  • 1
    Use a custom permission instead. help.salesforce.com/apex/…
    – gorav
    Aug 12, 2016 at 21:19

1 Answer 1

3

As long as you validate based on the running user, my previous answer applies here as well.

There are a few approaches you can take here, which I will list in order of preference. These should work just about anywhere merge fields can be used.


Create a Custom Permission

You can check if someone has a Custom Permission in a formula as follows:

$Permission.My_Custom_Permission__c

Steps:

  1. Create Custom Permission
  2. Add it to a Permission Set
  3. Assign that Permission Set to any User you wish to exempt from validation

Hierarchy Custom Setting

If you don't find the above palatable for some reason, you can also check a Hierarchy Custom Setting in a formula as follows:

$My_Hierarchy_Setting__c.Is_Exempt_From_Validation_X__c

Steps:

  1. Create Hierarchy Custom Setting
  2. Add exemption field
  3. Set Org Wide Default for this field to false
  4. Set value to true for any User you wish to exempt from validation

Alias

You can check against an Alias in a way that in all environments as follows:

$User.Alias = "analias"

Username

Somewhat less clean than Alias, but you would check against a Username as follows:

BEGINS($User.Username, "[email protected]")

User Id

I would not adopt this approach unless you have compelling reasons none of the above are a good fit:

$User.Id = "00550000000lxVg"
1
  • 1
    For whatever reason that other one didn't come up when I was searching for the issue.. odd. Thanks for the information! I went ahead and clicked yes on "Question already answered?" when it popped up, to link out to the other post as well.
    – Genko
    Aug 12, 2016 at 22:09

Not the answer you're looking for? Browse other questions tagged .