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
Tyler HarrisTyler Harris 

Struggling With AND OR Formula logic with Boolean fields

Basically what I want to accomplish is

1) If the field is missing country prevent the record from saving OR
2) If the field is marked "email opt out" equals true, ignore the missing country and save.

Is this possible with formula validation?
 
IF(
OR(
AND(
    AND(Country == "",
    $Profile.Name <> 'System Administrator',
    $User.Id <> "005Q000000O5P3v"),ISNEW(),HasOptedOutOfEmail)),
    true,
    false
)

 
Cory GriffinCory Griffin
AND(
      Country == "",
      $User.Id <> "005Q000000O5P3v",
      NOT(ISNEW()),
      HasOptedOutOfEmail='FALSE',
      $Profile.Name <> 'System Administrator'
      )
The above shows if all of these conditions are met, then the validation rule will get triggered, not allowing user to save.
step by step:
Country is blank
User id is not 005Q000000O5P3v
It's not a NEW record
The email opt out box is not checked
The user is not an administrator
 
Tyler HarrisTyler Harris
Thanks for your reply, but now it's saving no matter what. Do I need Apex for this instead? I want it to pass validation if Country is not blank or "Email Opt Out" is true
Cory GriffinCory Griffin
Possible reasons why it may not be firing.
  • Make sure the "Active" checkbox is checked on the validation rule. 
  • Ensure the user you are testing as is not System Admin profile (line 6) or User.ID 005Q000000O5P3v (line 3)
  • Line 4 prevents a validation check if it is a new record. It won't matter what the other criteria is if it is new.
More simplistic version below:  Try this first, then add additional criteria as needed
AND(
      ISBLANK(Country),
      HasOptedOutOfEmail='FALSE'
      )

 
Tyler HarrisTyler Harris
Now it's validating if I check HasOptedOutOfEmail. It should bypass validation if checked. I used a Marketing profile to test. AND(ISBLANK(Country), HasOptedOutOfEmail = false, $Profile.Name 'HID Business Administrator', $User.Id
Cory GriffinCory Griffin
If the user has opted out of email (HasOptedOutofEmail = True) then the validation should not be applicable, therefore, HasOptedOutofEmail=False is part of the condition that is evaluated.
ISBLANK(country) is only applicable if the other conditions within the AND( ) are true as well, so I believe the above code would be correct.  Did you try using the most basic version and testing, then adding one condition at a time?  
Tyler HarrisTyler Harris
Basically I want Country to be required for all users except a few, but I want to bypass this entirely if a user checks the "HasOptedOutOfEmail"