1

I have a validation rule that I only want to fire if the stage is changed. So if the opportunity is already in that stage I don't want the rule to fire if I change other values in the opp. I've used the ISCHANGED(StageName) functionality - and it is still firing on existing opps when I'm not making changes to the stage name.

Record_Type_Name__c="A" &&
ISBLANK(Multipicklist_Field) &&
(ISCHANGED(StageName) &&
ISPICKVAL(StageName, "Needs Analysis") || ISPICKVAL(StageName, "Decision Making"))

2 Answers 2

1

Sometimes it helps to separate the logic out a bit like this:

ISBLANK(Multipicklist_Field) &&
( ISCHANGED(StageName) &&
  ( ISPICKVAL(StageName, "Needs Analysis") || 
    ISPICKVAL(StageName, "Decision Making")
  )
)

I did it and I could see another bracket was needed - I've added.

4
  • found I left of a closing bracket... fixed Aug 16, 2023 at 19:28
  • 1
    The vital component here was putting parenthesis around the stagename checks here. In your (OP's) original formula, the structure was making it so that Salesforce was evaluating the ISCHANGED(StageName) && ISPICKVAL(StageName, "Needs Analysis") part first rather than evaluating ISPICKVAL(StageName, "Needs Analysis") || ISPICKVAL(StageName, "Decision Making") as a single, cohesive unit. This is because && has higher precedence than || (think of it like math, multiplication is done before addition, && is done before ||).
    – Derek F
    Aug 16, 2023 at 19:32
  • And I think this is an example of why I prefer using AND() and OR() over && and ||. There can still be some shenanigans around chaining AND()s and OR()s, but it would've been less prone to semantic error in this case.
    – Derek F
    Aug 16, 2023 at 19:36
  • Yes good point. Also, it's easier to line things up so you can see the logical structure. Aug 16, 2023 at 19:59
2

The heart of the issue here is that && gets evaluated before ||. && has the higher precedence.

So instead of evaluating as you intended

  • StageName is changing and
  • StageName is 'Needs Analysis' or 'Decision Making'

The formula is being evaluated as

  • StageName is changing and StageName is 'Needs Analysis'
  • or StageName is 'Decision Making'

So this part of the formula would result in true if your StageName was Decision Making, regardless of whether StageName was changing or not.

Operator precedence, much like in math, determines what happens when a statement is ambiguous (i.e. it can be interpreted in more than one way).

You can override precedence using parenthesis (just like in math), but I'd argue that AND() and OR() would've been better to use here (as opposed to && and ||) because I think it's a more visual representation of how the evaluation will occur. That, and it'd be harder to make the same precedence mistake in this case.

AND(
    Record_Type_Name__c = "A",
    ISBLANK(Multipicklist_Field),
    ISCHANGED(StageName),
    OR(
        ISPICKVAL(StageName, "Needs Analysis")
        ISPICKVAL(StageName, "Decision Making")
    )
)

You must log in to answer this question.

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