# Variable Operation

Source: https://help.channelautomation.com/flow-builder/variable-operation

- - 1.1 [Clear Custom Field / Clear JSON](https://uchat-com-au.atlassian.net/wiki/spaces/UKB/pages/669909001/Action+Step+-+Variable+Operation#Clear-Custom-Field-/-Clear-JSON)
    - [1.2 input value](#input-value)
    - [1.3 Math Formula](#math-formula)
- [2 Set Text Variable](#set-text-variable)
    - [2.1 trim text](#trim-text)
    - [2.2 sub string](#sub-string)
    - [2.3 replace string / replace string case sensitive](#replace-string-replace-stringcase-sensitive)
    - [2.4 to lower / upper case](#to-lower-upper-case)
    - [2.5 generate random text](#generate-random-number)
    - [2.6 url encode / decode](#url-encode-decode)
    - [2.7 base64 encode / decode](#base64-encode-decode)
    - [2.8 to URL friendly slug](#to-url-friendly-slug)
    - [2.9 get text before/after](#get-text-length)
- [3 Set Number Variable](#set-number-variable)
    - [3.1 get text length](#get-text-length)
    - [3.2 generate random number](#generate-random-number)
    - [3.3 add / subtract / multiply / divide / modulus / power / natural logarithm / square root](#add-subtract-multiply-divide)
    - [3.4 round](#round)
    - [3.5 floor / ceil](#floor-ceil)
- [4 Set DateTime Variable](#set-datetime-variable)
    - [4.1 from formatted text](#from-formatted-text)
    - [4.2 add minutes / hours / days / weeks / months](#add-minutes-hours-days)
 

---

Channel Automation provides you with various built-in operations for variable modification.

There are 6 types of variable in Channel Automation:

| **Type** | **Storage** | **Example** | **Operation** |
|---|---|---|---|
| Text | letters, words, sentences... | Hi, Channel Automation. | cutting, change case, encode,        decode... |
| Number | number | 123.45 | + - x ÷, mod, power, log, root, round... |
| Boolean | either "yes" or "no" | yes | assign |
| Date | date | 2021-03-30 | format, add months/weeks/days |
| DateTime | date and time | 2021-03-30 16:52:00 | format, add                                       months/weeks/days/hours/minutes |
| JSON | a series of variables | {"name":"jack", "age":"10"} | load, get, update, remove, count,   sum, average, sort, shuffle,               reverse... |

![](https://cdn.eniston.com/bases/b65dacaf-ae39-4733-871d-f10cfe13378f/attachments/inked341219af-686c-4272-8e89-b951a3eed416_1677765159.jpg)

Click "Basic Actions" in the Action step and here are the "Set Variable Value" and "Clear Variable Value".

### Clear Custom Field / Clear JSON

To clear text, number, boolean, date, datetime variable, please use the **Clear Custom Field** action.

To clear JSON variable, please use the **Remove All Items** operation in the **JSON Operation**.

### input value

"input value" operation is used to assign value to a variable. This operation works for any variable type.

TIP - "input value" for text variable can be used to connect several text variables. See the below picture for where to put original value and operated value.

![](https://cdn.eniston.com/bases/b65dacaf-ae39-4733-871d-f10cfe13378f/attachments/4f723450-9110-4bb5-a16a-3609c8bb280e_1677765228.png)

### Math Formula

![](https://cdn.eniston.com/bases/b65dacaf-ae39-4733-871d-f10cfe13378f/attachments/96a59c63-8e3a-498f-96e2-7f26b9395955_1677765334.png)

With this operation, you can now do formula calculation directly in the Action Step - "Set Variable Value" and Condition Step - "Value".

Math operations and function supported:

+, -, \*, e, pi, PI, abs(), min(), max(), ceil(), floor(), log(), pow(), round(), sqrt(), sin(), cos(), tan()

Please note that If the formula is invalid, the system will return 0 to number variable and empty to text variable.

## Set Text Variable

### trim text

| **Description** | **e.g. Before** | **e.g. After** |
|---|---|---|
| remove spaces before or after the text | " abc" | "abc" |

![](https://cdn.eniston.com/bases/b65dacaf-ae39-4733-871d-f10cfe13378f/attachments/b8b7e6cd-0d01-4234-86fd-8c4d77c4cc0c_1677765466.png)

### sub string

| Description | e.g. Before | e.g. After |
|---|---|---|
| get part of the text | "Hi, Channel Automation!" | "Channel Automation" |

 

![](https://cdn.eniston.com/bases/b65dacaf-ae39-4733-871d-f10cfe13378f/attachments/inked0a031db5-2da4-41ad-815a-89015de8b213_1677765610.jpg)

 

 

 

Note - Index starts from 0. Every character counts including space and punctuation.

 

 

![](https://cdn.eniston.com/bases/b65dacaf-ae39-4733-871d-f10cfe13378f/attachments/inkeda28c05e3-9951-495c-94fe-352af91fd426_1677765663.jpg)

### replace string / replace string case sensitive

| Description | e.g. Before | e.g. After |
|---|---|---|
| replace part of the text | "Hi, Channel Automation!" | "Hello, Channel Automation!" |

 

![](https://cdn.eniston.com/bases/b65dacaf-ae39-4733-871d-f10cfe13378f/attachments/inked29e1a7c7-a383-4ee9-b1ba-e196549ba108_1677765767.jpg)

 

Note - In case sensitive situation, you have to put exactly "Hi" rather than "hi", "HI" or "hI".

 

![](https://cdn.eniston.com/bases/b65dacaf-ae39-4733-871d-f10cfe13378f/attachments/inked07f19eb6-86c5-4c64-ba2e-836bd13b884d_1677765811.jpg)

### to lower / upper case

 

| Type | e.g. Before | e.g. After |
|---|---|---|
| to lower case | "Hi, Channel Automation!" | "hi, channel automation!" |
| to upper case | "Hi, Channel Automation!" | "HI, CHANNEL AUTOMATION!" |

 

 

 

 

![](https://cdn.eniston.com/bases/b65dacaf-ae39-4733-871d-f10cfe13378f/attachments/inked62fb8154-1745-4469-abee-14e6550856ee_1677765939.jpg)

### generate random text

| Description | e.g. Before | e.g. After |
|---|---|---|
| as the name | "code: XXXX-####-xxxx" | "code: UBWT-3657-lkzb" |

 

![](https://cdn.eniston.com/bases/b65dacaf-ae39-4733-871d-f10cfe13378f/attachments/734de48b-7d76-4b97-9d91-bb451d3e39d9_1677766012.png)

 

This feature is useful when you need to generate one-time verification code or reference code.

 

### url encode / decode

 

| Description | e.g. Before | e.g. After |
|---|---|---|
| encode | "Hi, Channel Automation!" | "Hi%2C%20Channel%20Automation!" |
| decode | "Hi%2C%20Channel%20Automation!" | "Hi, Channel Automation!" |

 

 

 

 

 

![](https://cdn.eniston.com/bases/b65dacaf-ae39-4733-871d-f10cfe13378f/attachments/inked9174bbd1-68d4-43af-84aa-bd2bb307fda0_1677766171.jpg)

### base64 encode / decode

| Description | e.g. Before | e.g. After |
|---|---|---|
| encode | "Hi, Channel Automation!" | "SGksIFVDaGF0IQ==" |
| decode | "SGksIFVDaGF0IQ==" | "Hi, Channel Automation!" |

 

![](https://cdn.eniston.com/bases/b65dacaf-ae39-4733-871d-f10cfe13378f/attachments/inked18b762ab-98dc-4253-ab69-0037e90527be_1677766239.jpg)

### to URL friendly slug

| Description | e.g. Before | e.g. After |
|---|---|---|
| replace spaces with hyphens and          remove the rest signs | "it's a good day" | "its-a-good-day" |

 

![](https://cdn.eniston.com/bases/b65dacaf-ae39-4733-871d-f10cfe13378f/attachments/6eeb2d8f-ad90-47e4-bf10-c791aff49d79_1677766293.png)

### get text before/after

Description: get part of the text.

| Type | e.g. Before | e.g. After |
|---|---|---|
| get text before another text | name: Channel Automation, city: Melbourne | name |
| get text before last occur of another text | name: Channel Automation, city: Melbourne | name: Channel Automation, city |
| get text after another text | name: Channel Automation, city: Melbourne | Channel Automation, city: Melbourne |
| get text after last occur of another text | name: Channel Automation, city: Melbourne | Melbourne |

 

![](https://cdn.eniston.com/bases/b65dacaf-ae39-4733-871d-f10cfe13378f/attachments/inked1e66f52c-96e3-4aed-b363-2e83111d8d79_1677766436.jpg)

 

## Set Number Variable

### get text length

| Description | e.g. Before | e.g. After |
|---|---|---|
| as the name | 500.59 | 6 |

 

 ![](https://cdn.eniston.com/bases/b65dacaf-ae39-4733-871d-f10cfe13378f/attachments/532f8523-c0b7-42ac-b8f5-3856fdc53f12_1677766520.png)

 

### generate random number

 

| Description | e.g. Before | e.g. After |
|---|---|---|
| as the name | n/a | 56 |

 

![](https://cdn.eniston.com/bases/b65dacaf-ae39-4733-871d-f10cfe13378f/attachments/4eba5eaf-200c-45df-a477-15e6ee4bd66d-1-_1677766925.png)

 

### add / subtract / multiply / divide / modulus / power / natural logarithm / square root

 

| Description | e.g. Before | e.g. After |
|---|---|---|
| as the name | 7 | 8 |

 

![](https://cdn.eniston.com/bases/b65dacaf-ae39-4733-871d-f10cfe13378f/attachments/6ba917d8-698f-4a95-a74e-32a0ce642268_1677766995.png)

TIP - in "natural logarithm", when you are calculating log2(8) = ? put 8 as "Value" and 2 as "Number".

 

### round

| Description | e.g. Before | e.g. After |
|---|---|---|
| as the name | 7.8693 | 7.87 |

 

![](https://cdn.eniston.com/bases/b65dacaf-ae39-4733-871d-f10cfe13378f/attachments/91347720-b0a7-4171-b6a5-40d96be068f6_1677767606.png)

### floor / ceil

Description: get approximate integer.

| Description | e.g. Before | e.g. After |
|---|---|---|
| floor | 6.55 | 6 |
| ceil | 6.55 | 7 |

## ![](https://cdn.eniston.com/bases/b65dacaf-ae39-4733-871d-f10cfe13378f/attachments/9c1399ad-a79f-4a63-91da-751022ff7f06_1677770089.png)

## Set DateTime Variable

### from formatted text

| Description | e.g. Before | e.g. After |
|---|---|---|
| get DataTime value from formatted text | 00:00:00 30th Aug, 2020 | 2020-08-30 00:00:00 |

 

![](https://cdn.eniston.com/bases/b65dacaf-ae39-4733-871d-f10cfe13378f/attachments/32bac012-bc50-44c9-9eb6-111e2bdd62c4_1677767673.png)

### add minutes / hours / days / weeks / months

| Description | e.g. Before | e.g. After |
|---|---|---|
| as the name | 2021-01-01 00:00:00 | 2021-01-01 00:01:00 |

 

 

 

 

 

 ![](https://cdn.eniston.com/bases/b65dacaf-ae39-4733-871d-f10cfe13378f/attachments/11293a2f-c06b-4903-b236-aa19e0f0e8c9_1677767727.png)

 
