Ifelse activity in workflow
how do I invoke the branching based on the condition.
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
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
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.
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
Paul Ballard
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
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.
"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.
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.
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.
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