How To: Configure a simple or complex If-Then-Else expression using the IF Function

You use the IF function to evaluate a condition and do one thing if the condition is true, or a different thing if the condition is false. Commonly referred to as If-Then-Else, your logic inside an expression can be simple or complex, and the result from the function represents whether or not the condition(s) evaluated to true or false.

This topic shows you how to configure the IF function within a workflow step to set a workflow's Folio, using the simple approach as well as the more advanced rule editor. The folio value depends on whether the If-Then-Else is true or false. In the first simple example, if the Expense Claim Region field is United States of America, the workflow folio will be set to Claims - USA, otherwise the Folio will be set to Claims - Global. In the second part of this example, we will use an advanced expression to set the value to Claims - USA, Claims - Europe or Claims - Global depending on the region field's value.

Using an IF function to evaluate a condition to set a workflow 's Folio

Scenario

Your company uses a Expense Claim workflow to manage employee claim submissions. The employee submits the form which sends a task to a manager for approval. To make the expense claim reviewer’s job easier, they need to quickly identify the claim tasks by region by looking at the workflow folio.

Steps

In this scenario, you begin by building a SmartBox SmartObject to store expense claim details when submitted. Next, you create an item view and a form based on the SmartObject. Then you create a workflow and configure the IF function on the Set Folio step to determine the value to show as the workflow folio. In this case there are two scenarios, one simple and one more complex.

Simple scenario (step 5.1)

For claims submitted by employees from the United States of America, the folio shows Claims - USA. Any other regions show as Claims - Global.

Complex scenario (step 5.2)

For claims submitted by employees from the United States of America, the folio shows Claims - USA. For claims from France and Germany, the folio shows Claims - Europe. Any other regions show as Claims - Global.

Setup Steps

The following steps set up the scenario from start to finish. If you want to skip the setup steps and view the topic step, go to Configure the IF Function.

  1. Create a SmartObject that contains expense claim details including expense type, amount, date, descriptions, payee, submitted by, and region.
    1. Create a new SmartObject in K2 Designer. Name the SmartObject
      Expense Claim Details.
      Select the SmartBox option. This option creates a table in the K2 database. Click Create.
    2. Click Add to add the properties shown in the image below. Make sure to change the data type where necessary. Click OK to return to the SmartObject properties. Click Finish to save the SmartObject and exit.
    3. If you are not familiar with creating a SmartBox SmartObject, see How To: Create A SmartBox SmartObject.

  2. Generate an item view from your SmartObject.
    1. Create a view from the SmartObject. Right-click the Expense Claim Details SmartObject, then select Generate Views.
    2. On the Generate Options screen, check the Item box to create an item view. Rename the view
      Expense Claim Details
      then click OK to save your settings.
    3. If you are not familiar with creating views, see How To: Create an Item View and a List View.

  3. Generate and customize a form from the item view.
    1. Create a form from the view. Right-click the Expense Claim Details view, then select Generate Forms.
    2. On the Generate Form screen, rename the view
      Expense Claim
      then click OK to save your settings.
    3. Locate the Expense Claim form and select Edit. On the Layout tab, change the view title to Expense Claim. Click Finish.
    4. If you are not familiar with creating forms, see How To: Create a Form.

  4. Create a workflow called Expense Claims. You also configure the workflow to start when the Expense Claim form is submitted and add a Set Folio step to the canvas.
    1. From the K2 Designer click New and select Workflow.
    2. Name this workflow Expense Claims and click Create.
    3. The Expense Claims workflow opens ready for you to start building it.

      If you are not familiar with creating workflows, see How To: Create a Workflow.

    4. Next you need to configure the workflow to start when the Expense Claim form is submitted. Select the Start step, expand the Configuration Panel and click Start with a Form.
    5. Browse to and select the Expense Claim form. Step through the Start Workflow from a Form wizard by clicking Next and select the Do it for me! option. Complete the wizard and click Save and Finish.

      For more information on starting a workflow with a form, see Start a Workflow with a Form.

    6. The workflow starts when the Save button is clicked on the form. Expand the Context Browser to view the Expense Claim Details references.

      For more information on references, see Using References.

    7. From the Toolbox, click the Basic category and drag the Set Folio step onto the canvas.

Configure the IF Function

The following section describe how to configure a If-Then-Else expression using the If function. Expand the sections below to see examples of configuring a Simple (step 5.1) or a Complex (step 5.2) scenario.

  1. Configure a Simple scenario

    The following step describes how to drag the If function into the Folio field on the Set Folio step. In this step, you configure a simple If-Then-Else expression, using the Smartfield Composer, to set a value for the workflow folio. The If functions evaluates the value of the Region field on the submitted form. In this simple scenario, claims submitted by employees from the United States set the folio to Claims - USA. Any other regions set the folio to Claims - Global.

    1. Select the Set Folio step. Expand the Context Browser and select the Functions tab. From the Logical section, click and drag the IF function into the Type/Drop field.
    2. The Smartfield Composer opens. Here you configure the If-Then-Else logic inside an expression. The condition checks the value of the Region field entered by the submitter. Select the Fields tab and expand the Expense Claim Details reference. Drag the Region field into the left-hand If field.
    3. Next, specify the value to test the left-hand field against. In this example, we want to check whether the Region value is United States of America From the drop-down select equals (=) as the operator. Type United States of America as the value to check for. Then specify what the return value should be if the condition evaluates to true. In this example, in the Then field, type Claims - USA.
    4. The final thing to do is specify what happens if the condition evaluates to false. In this example, for any other values in the Region field, the folio is set to Claims - Global. In the Then field type Claims - Global.
    5. Click X to close the Smartfield Composer. Your If function is configured.
  2. Configure a Complex scenario

    The following step describes how to drag the If function into the Folio field on the Set Folio step. In this step, you configure a complex If-Then-Else expression, using the Smartfield Composer, to set a value for the workflow folio. The If functions evaluates the value added in the Region field on the submitted form. In this scenario, claims submitted by employees from the United States shows Claims - USA as the folio name on the task. Claims submitted from France or Germany shows Claims - Europe. Any other regions show as Claims - Global.

    1. Select the Set Folio step. Expand the Context Browser and select the Functions tab. From the Logical section, click and drag the IF function into the Type/Drop field.
    2. The Smartfield Composer opens. Here you configure the If-Then-Else logic inside an expression. The condition checks the value of the Region field entered by the submitter. Select the Fields tab and expand the Expense Claim Details reference. Drag the Region field into the left-hand If field.
    3. Next, specify the value to test the left-hand field against. In this example, we want to check whether the Region value is United States of America From the drop-down select equals (=) as the operator. Type United States of America as the value to check for. Then specify what the return value should be if the condition evaluates to true. In this example, in the Then field, type Claims - USA.
    4. Next specify what happens if the condition is not met. In this example you need to configure the rule for France and Germany as well as other region values. Click Edit in Rules Designer to create a more complex expression.
    5. The Rules Designer opens. From the Rules Toolbar, drag an IF/THEN statement into the ELSE block. This adds an additional If-Then-Else statement.
    6. Use the Quick Insert Tool bar (at the bottom of the Rules Designer) to add a condition to the IF block.
    7. Select Fields tab and expand the Expense Claim Details reference. Drag the Region reference into the left-hand side of the ELSE-IF section.
    8. From the drop-down select equals (=) as the operator. Type France as the value.
    9. From the Rules Toolbar, drag a Condition into the IF block. Change the operator from AND to OR. This adds an additional condition to define the criteria.
    10. Configure this condition as shown below, to add a conditional check for Germany.
    11. Next specify what happens if the condition evaluates to true. In this example, if the region is either France or Germany, the folio is set to Claims - Europe. Use the Quick Insert Tool bar to add a Return to the THEN block. Type Claims - Europe as the value.
    12. The final thing to do is specify what happens if the condition evaluates to false. For any other region values, the folio is set to Claims - Global. Use the Quick Insert Tool bar to add a Return to the ELSE block. Type Claims - Global as the value.
    13. The complex rule is now complete. Click Done Editing to close the Rules Designer.
    14. Notice the Complex label added to the Else field. This shows that you configured a more complex rule that can only be edited in the Rules Designer. To see it, click Edit in Rules Designer.
    15. Click X to close the Smartfield Composer. Your If function is configured.
  1. Configure and complete the rest of the workflow. In this step you also deploy the workflow.
    1. From the Toolbox click the Basic category and drag a Task step onto the canvas. Connect the step to the Set Folio step and change the step name to Manager Approval.
    2. In this scenario, the Manager Approval task is sent to Codi. She can either approve or reject the task. On the Task tab, in the Actions section add Approve and Decline.
    3. On the Recipients tab browse and select Codi.

      For more information on the Task step, see Task step.

    4. From the Task step, click and drag a line to create a Placeholder step. Click this placeholder to create the Decision step.

      For more information on the Decision step, see Decision step.

    5. From the Toolbox, click the Basic category and drag the Send Email step onto the canvas. Connect the step to the Approved task action. Configure the email as shown below.
    6. Drag another Send Email step onto the canvas and connect it to the Rejected task action. Configure the step as shown below.

      For more information on the Send Email step, see Send Email step.

    7. From the Toolbox, click the Logic category and drag the End step onto the canvas. Connect the step to both Send Email steps.

      For more information on the End step, see End step.

    8. The final thing to do is deploy your workflow. From the File menu select Deploy.

      For more information on deploying a workflow, see Deploy a Workflow.

Test the solution.

In this section of the topic you test the solution for both the Simple (step 7.1) and Complex (step 7.2) solutions.

  1. Test the Simple scenario

    In this step you run and complete the Expense Claim form. The workflow references the Expense Claims SmartObject and the If function evaluates the value entered into the Region property. If the Region value is United States of America the workflow folio is set to Claims - USA. If any other value is entered, the workflow folio is set to Claims - Global.

    1. For the first test, run the Expense Claim form and enter the details as shown below. Make sure to specify the Region as United States of America. Click Save to submit the form.
    2. Log in to Codi's K2 Workspace and open the task inbox.
    3. The If function evaluates the value you entered in the Region field. Because you entered United States of America, the condition is met and the workflow folio gets set as Claims - USA.
    4. For the second test, run the Expense Claim form and enter the details as shown below. Make sure to specify the Region as Australia. Click Save to submit the form.
    5. Log in to Codi's K2 Workspace and open the task inbox. The If function evaluates the value you entered in the Region field. Because you entered Australia, the workflow folio gets set as Claims - Global.
  2. Test the Complex scenario

    In The Smartfield Composer opens. Here you configure the If-Then-Else logic inside an expression. If the Region value is United States of America the workflow folio is set to Claims - USA. If the Region value is either France or Germany the workflow folio is set to Claims - Europe. If any other value is entered, the workflow folio is set to Claims - Global.

    1. For the first test, run the Expense Claim form and enter the details as shown below. Make sure to specify the Region as United States of America. Click Save to submit the form.
    2. Log in to Codi's K2 Workspace and open the task inbox.
    3. The If function evaluates the value you entered in the Region field. Because you entered United States of America, the condition is met and the workflow folio gets set as Claims - USA.
    4. For the second test, run the Expense Claim form and enter the details as shown below. Make sure to specify the Region as Germany. Click Save to submit the form.
    5. Log in to Codi's K2 Workspace and open the task inbox. The If function evaluates the value you entered in the Region field. Because you entered Germany, the workflow folio gets set as Claims - Europe.
    6. For the final test, run the Expense Claim form and enter the details as shown below. Make sure to specify the Region as China. Click Save to submit the form.
    7. Log in to Codi's K2 Workspace and open the task inbox. The If function evaluates the value you entered in the Region field. Because you entered China, the workflow folio gets set as Claims - Global.
Review

The IF function allows you to evaluate a condition and then do one thing if the condition is true, or a different thing if the condition is false. Commonly referred to as If-Then-Else, your logic inside an expression can be simple to complex, and the result represents whether or not the condition is true. For more information see IF Function.