Ifelse activity in workflow

How can I set the condition for the ifelse activity .
how do I invoke the branching based on the condition.
[112 byte] By [BinduV] at [2007-12-17]
# 1

There are a couple of ways to set the condition for an IfElse branch.

Scenario 1: Rule Condition
1. In the Workflow designer set the ifElseBranchX Condition to System.Workflow.Activities.Rules.RuleConditionReference.
2. Expand the Condition property and set the name of the RuleCondition
3. Click the "..." button on the rule condition to edit the Expression property
4. In the Expression Designer Click Add and select Predicate
5. Enter your Expression

Scenario 2: Code Condition
1. In the Workflow designer set the ifElseBranchX Condition to System.Workflow.Activities.CodeCondition
2. Expand the Condition Property and drop down the choices for the Condition property
3. Select a shared method with the following signature from your workflow.
Function MethodName(sender as Object, e as EventArgs) as Boolean

Of course you can also do all of this in Code or XAML as well.

Hope this helps,
Paul Ballard

PaulBallard at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 2
Thank you Paul

This was very helpful.

I had one more query.

In the case of the scenario 2 : code condition, if the branching is more than two branches,
how do you invoke the relevant branch from the shared method.

for eg : if the "ifelse" branching is only two branches. then the steps mentioned in your reply(scenario 2 : code condition) is followed and the
Function MethodName(sender as Object, e as EventArgs) as Boolean
{
If <condition>
return true;
else
return false;
}

will branch to the relevant flow based onthe condition value.

In the above scenario if the branching is more than two branches, how do I invoke the relevant branch from the method.

thank you,
BinV

BinduV at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 3

Are you talking about a switch case style activity ? Im sure you can do a switch case activity easily ( Derive from Parallel activity , add a Switch property and then invoke only one child based on the ( on the switch ) .. also you would need Children derived from Sequence with an addition property for Case values.

the executor of the SwitchCase activity would then need to look at the children's Case values and decide which branch to execute.

I'll try to come up with a code sample for this one soon.

# 4

When you specify an IfElse activity, the "If Else" switching is handled by the Workflow engine, you merely need to specify the condition for each branch. So for example, if I create a workflow that needs to branch into two if a PurchaseOrder.Status is "Approved" I would add a condition to the first branch that says (either in code or Rules) "If PurchaseOrder.Status = 'Approved' return true else return false". You don't need to specify a condition for the second branch as it will be called if the prior conditions are false.

You can add more branches by right clicking on the IfElse activity and selecting "Add Branch". This is like creating a switch statement with each branch having its own condition that simply returns true or false. Again, you need not specify the condition for the final branch.

Hope this helps,
Paul

PaulBallard at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 5
Actually, this isn't exactly what the Parallel activity is for. The Parallel activity is used when you want two sequences to run at the same time (It literally will spawn the contained activities on another thread). The solution you describe may work but it's a rather long way to go for a simple switch construct.

Paul Ballard

PaulBallard at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 6
Paul did an excellent job describing this above. Let me just add a simple illustration for you. Take the following workflow snippet for instance:

IfElse
IfElseBranch1
IfElseBranch2
IfElseBranch3

This is logically equivalent to writing the following C# code:



if (x)
{
}
else if (y)
{
}
else // OR "else if(z)" if a conditon was specified for the last branch
{
}

where x, y, and z are the conditions corresponding to each branch. The workflow will then handle the execution of your rules as needed, e.g. if x returns false, then it will call y and so on until it gets a true or it runs out of branches.

Thanks!
Angel

AngelAzcarraga at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 7

Even though the elseif approach solves the problem, you will end up evaluating multiple conditions , a switch case is a simpler construct (only based on value cases) thats why programming languages have switch case constructs. So actually writing a custom switch case activity might be useful too.

Paul, by saying "derive from parallel activity" I meant using the ParallelDesigner to get the required branches, not the parallel execution behavior , I mentioned that tht Execute() method actually executes only one child based on the switch.

# 8
Hi,

It would be very interesting to offer to developers an other scenario to the condition for an IfElse branch. This new scenario will be based on the support of custom condition.

You can already set the ifElseBranchX Condition to your custom condition in Code or XOML. But, unfortunately, the workflow designer provides by default only the two condition types that you mention.

To workaround this, I developed a custom IfElse branch that "copy-pastes" the code of the Microsoft IfElse branch in adding the support of my custom condition. Unfortunately, this support is hard-coding... A solution will be to list all classes that implement the Condition class, or to provide a "Browse and Select a .NET type" dialog ...

Regards.
Pascal RECCHIA
raskal at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 9

"A solution will be to list all classes that implement the Condition class, or to provide a "Browse and Select a .NET type" dialog ..."

It is done. You can read all about it :

http://blog.rioterdecker.net/blogs/nezdeboeuf/archive/2006/06/05/64.aspx


Regards.

raskal at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 10

Hey BinduV,

I have an example of setting up the rule condition in an article at MSBICentral called "WF Activities Part I - Practical Use of ExternalMethod, IfElse & CallExternalMethod Activities". It wires up the IfElse Activity based on a range of zip codes. If the condition is met then the branch is invoked. Else the other branch is invoked. Hope this helps.

BayerWhite at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 11
Thank you Bayer
BinduV at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 12

Raskal, that sounds exactly like what I need. Unfortunately your blog is down. I tried it from our network and from our web server hosted in the states.

hendrikswanepoel at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 13

Hendrik, at the present time, my blog seems to be working fine again.

raskal at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 14

Raskal, I ended up writing an IfElseActivity of my own, due to an additional requirement I had. My requirement was to select multiple conditions for the branch, and I also wanted to pass parameters to the conditions. So I implemented an ifelse activity that takes conditions in the form of activities that implement an interface called IConditionActivity.

You can go have a look and download the solution at: http://dotnet.org.za/hendrik/archive/2006/07/04/53953.aspx

hendrikswanepoel at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...

Software Development for Windows Vista

Site Classified