You can do it either with logical operators or with formula functions.
https://help.salesforce.com/HTViewHelpDoc?id=customize_functions.htm
Here's what I call "programmer's syntax" because it uses the pipes (||
) and ampersands (&&
); operators are easier to read for developers.
IF($User.Alias = 'JohnDoe' || $User.Alias = 'JBloggs' || $User.Alias = 'FooBar',
value_if_true,
value_if_false
)
You can also use "Excel syntax" where you have OR()
function that can take any amount of parameters.
IF(OR($User.Alias = 'JohnDoe', $User.Alias = 'JBloggs', $User.Alias = 'FooBar'),
value_if_true,
value_if_false
)
Try not to use user aliases in business logic. User can change his own alias, rendering your check useless. See if you can base the check on
$Profile
,$UserRole
or maybe create some checkbox field on the user record, make it editable only for SysAdmins...In this particular scenario - there's a way to check at runtime whether user has the right to delete Accounts. Check Using apex:relatedList in a Visualforce page when the user profile denies access to child objects (you'll need the "isDeletable" version).
Edit (re: comments about !
and !=
)
It is mentioned in the first link I've used you know ;)
!=
and sometimes <>
is what programmers use to write "not equal to". It looks similar enough to "slashed equal sign" (≠) but doesn't require remembering some weird code for that character. This convention is pretty much uniform across many programming languages.
As far as you're concerned - the syntax of formulas and Visualforce expressions uses them interchangeably:
=
and==
for "equals"!=
and<>
for "not equals` (the later one works in Excel too)
So if you write IF($User.Alias = 'JohnDoe', 'Allow', 'Deny')
you mean that only he will see "Allow".
There's also one more thing that might confuse you - negation operator: !
standing in front of any expression will convert true ↔ false. This can also be achieved by wrapping the expression you want in NOT(some true/false expression here)
.
(The exclamation mark that's means the merge field begins is not meant as negation. So {!some expression}
means just the logical (or numeric, or text) value of that expression. To negate it you either do {!!expression}
or {!NOT(expression)}
.