Control Statements

Control Statements

 Overview

A program consists of a number of statements which are usually executed in sequence.

Programs can be much more powerful if we can control the order in which statements are run. To achieve this deluge script offers developers the

if,else if,else constructs.

 Conditional Execution - If, else if, else

The 'If' construct in deluge is the same as that of other languages. It conditionally executes a set of statements depending on the value of the boolean expression.

 Syntax

if(<if-boolean-expression>)
{
<statements>
}
else if(<elseif-boolean-expression-1>)
{
<statements-1>
}
else if(<elseif-boolean-expression-2>)
{
<statements-2>
}
..
..
..
..
else if(<elseif-boolean-expression-n>)
{
<statements-n>
}
else
{
<statements>
}

An If construct should adhere to the following rules:

  • It should have an 'If' condition
  • It can have zero or more 'else if' conditions
  • It can have an optional 'else' part

When an <If Construct> is encountered during script execution, the condition specified by the <if-boolean_expression> is evaluated. If the condition is met, the statement in the <if statements> block are executed, but if the condition was not met, then the program flow skips the statements in the <if statements>block and searches for any else or else if keywords.

When an else if part is encountered, the condition specified by <boolean_expression-x> is evaluated and if the condition evaluates to true, the statements in <elseif statements-x> block is executed. If the <if condition> and all the <else if conditions> fails then the presence of <else> is searched and the statements in <else statements>block is executed.

 Example - Free flow scripting

1. Let us first take an example of a simple if-condition. We have a form with fields Field_1 (Number Field Type),Field_2 (Number Field Type) and Field_3 (Decimal Field Type). If Field_1 and Field_2 are not null, Field_3 should evaluate ((Field_1*Field_2)/12). To achieve this, you can add the following code in the required Automate-> Workflow->Custom Function ->Create screen:

if(input.Field_1 != null && input.Field_2 != null) 

input.Field_3 == ((input.Field_1 * input.Field_2 ) / 12)) 
}

2. Let us take the example. The deluge code added to the Automate-> Workflow->Custom Function ->Create block of the New_Applicant form is given below:

opening = New_Opening [Position_Name == input.Applied_For];
if (opening.Status == "Closed")
{
sendmail
(
To : input.Email_ID 
From : zoho.adminuserid 
Subject : "Reg application for job posted at recruitment.zoho.com" 
Message : "The job profile " input.Applied_For " for which you have applied 
is not currently open " 
)
}
else if (opening.Experience == input.Experience)
{
sendmail
(
To : "manager-recruitment@adventnet.com" 
From : zoho.adminuserid 
Subject : "Applicants resume matches job profile" 
Message : input.Applicant_Name " matches the job profile<br> Contact Info: "  
input.Email_ID 
)
}
else
{
sendmail
(
To : input.Email_ID 
From : zoho.adminuserid 
Subject : "Reg application for job posted at recruitment.zohoc )

Code Explanation:

  • In the code, we first fetch the record from the New_Opening form, whose Position_Name matches with the position applied for.
  • if status of the position is "Closed" the statements inside the if block is executed.
  • if experience for the position is same as the experience specified, the statements inside the else if block is executed.
  • if the above two conditions fail, the statements inside the else block is executed.

 Conditional IF statements

If boolean-expression is TRUE, returns expression1; otherwise it returns expression2 .

<variable> = IF( <boolean-expression>, <expression1>, <expression2>)

Example:

str = IF( input.text=="test", "test-new", " ");

For NULL check, the simplified version of conditional IF is given below. If expression1 is not NULL , IFNULL() returns expression1 ; otherwise it returns expression2 .

<variable> = IFNULL( <expression1>, <expression2>)

 Conditional expressions in Formula field

Please refer this help topic  for more information on using conditional expressions in a Formula field.

Share this post :

FacebookTwitter

Still can't find what you're looking for?

Write to us: support@zohodeveloper.com