IF-ELSE statements as conditions on rules in InfoPath 2010

Applies to: InfoPath 2010

Learn how to convert if-else statements into conditions on action rules in InfoPath 2010.

ADVERTISEMENTS

A condition on a rule in InfoPath determines when a rule should run.

If you are familiar with if-else statements in programming languages, you may want to know how to convert such statements into conditions on rules in InfoPath 2010.

Suppose you have two text boxes (field1 and field2) and a button on an InfoPath form and want the value of field2 to be set based on the value of field1 when the button is clicked.

If you want to convert the following if-else statement

if (field1 == "1")
   field2 = "one"
else
   field2 = "none"

into conditions on rules, you would have to:

  1. Add an Action rule to the button with a Condition that says:

    field1 = "1"
    and an action that says:

    Set a field's value: field2 = "one"
    and select the Don't run remaining rules if the condition of this rule is met check box. This rule defines the first part of the if-else statement and will stop all other rules from running if the condition is met. Rules run from top to bottom in InfoPath 2010, so if you do not stop other rules from running if the condition is met, any other rules that come after this rule might overwrite the results of the action from this rule.
  2. Add a second Action rule to the button with an action that says:

    Set a field's value: field2 = "none"
    This rule does not require a condition, because it will only run if the condition on the first rule is not met.

If you want to convert the following if-else statement

if (field1 == "1")
   field2 = "one"
else if (field1 == "2")
   field2 = "two"
else
   field2 = "none"

into conditions on rules, you would have to create 3 action rules, add conditions to the first two rules and stop all rules from running if any of the conditions is met.

So:

  1. Add an Action rule to the button with a Condition that says:

    field1 = "1"
    and an action that says:

    Set a field's value: field2 = "one"
    and select the Don't run remaining rules if the condition of this rule is met check box.
  2. Add a second Action rule to the button with a Condition that says:

    field1 = "2"
    and an action that says:

    Set a field's value: field2 = "two"
    and select the Don't run remaining rules if the condition of this rule is met check box.
  3. Add a third Action rule to the button with an action that says:

    Set a field's value: field2 = "none"

Also See

Chapter 4 in InfoPath 2010 Cookbook or InfoPath 2013 Cookbook explain the basics of using rules in great deal. In addition, since all of the recipes in both books are codeless, almost everything is done using rules, so you'll have plenty of examples to work with.

 


Related InfoPath Articles:

 

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services