[SalesForce] Multiple OR (||) Conditions in If Statement

Can anyone explain why the following If statement does not work with multiple OR conditions. The goal is when this trigger runs, if the user's Id who is updating the record is not equal to a specific Id, then an error is thrown. The If clause works only when I compare the current user's Id with a single specific Id.

    String currentUserId = UserInfo.getUserId();

    if (currentUserId != 'XXXXXXXXX' || currentUserId != 'YYYYYYYYY' || currentUserId != 'ZZZZZZZZZ') {
        Trigger.new[0].addError('Only specific users are able to change the Account Classification Field'); 
    }

{EDIT} FYI: I know I can accomplish this by using the following code, but I am curious as to why the OR operator is not working as I had expected.

    if (currentUserId != 'XXXXXXXXX') {
        if (currentUserId != 'YYYYYYYYY') {
            if (currentUserId != 'ZZZZZZZZZ') {
                Trigger.new[0].addError('Only specific users are able to change the Account Classification Field'); 
            }
        }
    }

Best Answer

"OR" means "true if either value is true". If the ID is "XXXXXXXXX", for example, it will NOT be "YYYYYYYYY", therefore OR will result in a true value. You need to use "AND" (&&) instead, meaning "true only if both values are true."


Side note: It's not enough to just put an error on Trigger.new[0]; there may be multiple records in the trigger context, and this can result in a "too many retries" error during bulk data operations.