# # Formula mode

To start using formulas, you need to be in formula mode. Formula mode needs to be switched on at the field level, and most input fields support formula mode. To toggle from text mode into formula mode, click on the text/formula mode switch button. This also changes the type icon of the input field into the `fx`

icon.

*When the input field is toggled into formula mode, the string type input field changes icon from String type to fx type*

# # Using the formula editor

In formula mode, the formula editor provides a set of whitelisted formulas available for data transformation/manipulation, and provides additional help on how to use these formulas.

## # Filter formulas displayed by data type

The formula editor should always display the type specific formulas for your datapills. Refer to this article for more on the different types of datapills.

When a string type datapill, the pill `Full name`, is dropped into an input field in formula mode, the formula editor prompts for a period `.`

to be added behind the pill, as per Ruby syntax. Subsequently, the editor shows the list of formulas applicable to strings.

*Formula editor showing the list of string formulas*

Similarly, when a date type datapill is dropped into the input field in formula mode, the formula editor prompts for a period `.`

to be added behind the pill, subsequently displaying the list of formulas applicable to dates.

*Formula editor showing the list of date formulas*

When there is no datapill within the input field, the formula list will only display formulas and operators which are independent of datapills or datapill types.

*Formula editor showing the list of default formulas*

## # Formula hints and syntax

When a formula is selected to be used, it will be auto-completed in the input field. An explanation of the formula's function, as well as its syntax, will be provided.

*When the split formula is selected, it auto-populates into the input field, and detailed formula help is displayed*

## # Text VS formula mode

In text mode, text and pills can be mapped into the input fields easily, and they will result in a text that looks exactly like what is formed. On the other hand, text needs to be explicitly formatted in formula mode.

Let's use an example of a recipe that sends an email to new leads in Salesforce. If a new lead called Madison Diaz is created, we wish to send the following email:

```
Hi Madison,
Thanks for joining our mailing list! We're excited to have you!
```

In text mode, the **Message** can simply be formed like this:

*Email in text mode. Example recipe*

In formula mode, text must have proper string syntax:

*Email in formula mode. Example recipe*

## # Conditionals

You can conditionally execute formulas using Ruby's ternary syntax (popular shortcut for if-else statements). Ternary syntax are of the form:

```
condition ? expression1 : expression2
```

### # Behaviour

#### # `condition`

A boolean expression that evaluates to `true`

or `false`

.

#### # `expression1`

Returns this expression if `condition`

is `true`

.

#### # `expression2`

Returns this expression if `condition`

is `false`

.

### # Conditional formula example

In the following example, we conditionally pass in either the `Full name` or `First name` into the `Message` input field.

*Ternary formula that passes in Full name as the input when Full name is present, and passes in First name as the input when Full name is not present*

Here is a detailed explanation of what the ternary formula does:

- "
`Full name`.present?" will check if the`Full name`pill has a value . If it has a value, it evaluates to`true`

. If it has no value, it evaluates to`false`

. - The second ? in the formula separates the condition to evaluate from the expressions to return.
- If there is a value in the
`Full name`pill when the job is ran, the value for`Full name`will mapped to the**Message**input. - If there is no value in the
`Full name`pill when the job is ran, the value for`First name`will be mapped to the**Message**input. Of course, if there's also no value in this`First name`pill, the job will fail at this step, as**Message**is a required input field.

For more information on Ruby's ternary syntax, check out this article.

## # Safe Navigation Operator

The Safe Navigation Operator `&.`

is an operator with two arguments. It returns a `nil`

value if the first argument (often a data pill) has a `nil`

value. Otherwise, it performs the operation specified by the second argument (often a function). It enables you to write simpler expressions to handle `nil`

values.

Before

After

For example, the Created Date data pill in the above field needs to be converted to a date data type using the `to_date`

formula. If you use it with the `.`

operator, you may receive an error if the data pill contains a nil value. This would require a complex expression to work around this logic as shown the "before" section above; however, with the `&.`

operator, it is easy to handle this situation with a simple formula expression.

Example 1: User wants to split an employees full name into their first and last name and then separate these by a comma. It is possible that the Full Name data pill may have a nil value. The `&.`

operator makes it easy to write a simple expression for this.

Example 2: In this recipe, the customer wants to include the country code data as a text string in the message field. In order to handle case where the data pill may contain a nil value, they use the &. operator in the formula mode syntax.

For more information on the safe navigator operator, check out this article.