Sunday, March 18, 2012

Using An ‘OR’ Condition In Workflows

The Problem

One of the problems with workflows is that,while you can group conditions with an OR in Advanced Find, you do not have the same ability within workflows.

Here are the advanced find grouping buttons.

image

Here is the workflow box for a ‘Check Condition’ step.

image

We can list multiple conditions but these are automatically grouped with an ‘AND’ clause i.e. all must be fulfilled.

This issue has come up in the forums in the past and, usually I recommend the creation of two workflows: one for each condition. While this approximates an ‘OR’ it is a lot of work.

The problem with this solution is we now have two workflows to maintain which means more overhead.

An Alternative Solution

De Morgan’s Law tells us that ‘A OR B’ is logically equivalent to NOT (NOT A AND NOT B). So how does this help us?

Well, to deal with the first ‘NOT’ we use the ‘Default Action’, which gives us an ‘Otherwise’ to our ‘If-Then’

image

Therefore, if we say ‘if (NOT A AND NOT B) then do nothing otherwise do x’ we have a winner.

So, in practice, let us say we want the workflow to trigger if either of one of two tickboxes are ticked. For an advanced find it is east to find records where Tickbox A or Tickbox B has been ticked.

image

For our workflow it is going to read thus:

image

If we trigger this off of the two fields changing, if neither of the fields are ticked, nothing happens but if either or both are ticked our action happens, which is what we want.

Doing the Maths

If you have a more complex problem, this is how to approach it.

First of all we have De Morgan’s Law: A OR B = NOT (NOT A AND NOT B)

Let us define our logical tests:

A: Tickbox A = Yes
B: Tickbox B = Yes

Therefore:

NOT A: Tickbox A = No
NOT B: Tickbox B = No

So Tickbox A = Yes OR Tickbox B = Yes is equivalent to NOT (Tickbox A = No AND Tickbox B = No) and this is expressed as we saw above using an if-then-otherwise step.

Conclusions

Using this technique, while a little harder to read (although the comment line can help) we maintain just one workflow, saving on future administration.  The technique is also applicable to any condition which there is a straightforward ‘opposite’ version of and can be extended to multiple attributes, not just two, without having to resort to creating multiple, mostly identical workflows.

5 comments:

Eric Ung said...

Nice logic. Thank you Leon, Go Physics or is it Mathmatics.

Maman said...

Very helpful, very clever and you've really made my morning with a bit of brain gymnastics.

@renegadeuvfunk said...

Good tip, thanks! Surprisingly there is still not an OR statement available in workflows even in CRM 2013.

Unknown said...

Good article though I've a more complex problem here.

How do I translate following condition in the workflow?

(A OR B) AND C

cheers

Aaron

Leon Tribe said...

In this case we're stuffed because when we invert it, we end up with another OR. The best bet in this case is two workflows; one checking for A AND C and the other checking for B AND C.