VisualForce IF Statement – need to know syntax for “or”

salesforcesyntaxvisualforce

I need help with the right VisualForce syntax to use "or" logic. The code I'm using is:

<apex:page action="{!if($User.Alias !='Item1',
    null,
    urlFor($Action.Account.Delete, $CurrentPage.Parameters.id, [retURL='/001'], true)
    )
    }"
  standardController="Account">
     <apex:pageBlock >
        <apex:PageMessage summary="You are not allowed to delete Accounts"
            severity="Warning"
            strength="3"/>
        <apex:pageMessages />
    </apex:pageBlock>
    </apex:page>

On the first line:

{!if($User.Alias !='Item1',

I need to specify more than one item using "or" logic, so that the the IF statement will be evaluated if any of the specified "Items" are true. I'm going for something like this, but I don't know what the right syntax is:

{!if($User.Alias !='Item1', OR 'Item2', OR 'Item3',

Thanks for your assistance and patience!

Best Answer

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
)

  1. 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...

  2. 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)}.