Choose where you’d like to start

Collection variable

Note:
  • Collection variable is not to be confused with the collection data type. To learn about the collection data type, which is used to hold an index-value collection or a key-value collection, refer to this page.
  • This help page is applicable only to Zoho Creator.

Overview

When you fetch a collection of one or more records, you get a collection variable.

Declaring a collection variable

<collection_variable> = <form> [<criteria>];

Example

In the following example, the variable named employeesJoinedToday contains records of employees who have joined today. Joining_Date is the link name of a field in the Employees form.

employeesJoinedToday = Employees [Joining_Date == zoho.currentdate];

Note:

  • When the "sort" param is not specified, the fetched records are often sorted based on "added time" (oldest first) system field. However, there is no guarantee to this order and there is no guarantee that the order will remain constant over time. If you are particular about the sequence of the records, it is advisable to use the sort param.
  • The Name and Address fields do not work with the sort by param.

Aggregate functions

You can use the following syntax to perform various functions:

<collection_variable> = <form> [<criteria>].<method>;

(or)

<collection_variable>.<method>;

Following are the methods that can be performed:

  • avg(<field_link_name>) ​

    Returns: Average of all values of a specified field in a collection of records.
    Applicable field types and the return type: Percent, Currency, Decimal, Formula(decimal) and Formula(bigint and decimal) field types will return Decimal data-type. Number and Formula(bigint) field types will return Bigint data-type.

  • count(<field_link_name>) ​

    Returns: Count of records in a collection when the <field_link_name> is not specified. Else, it returns the number of values the specified field holds.
    Return type: Bigint.
    Applicable field types: Multi select, Checkbox, Drop-down and Radio fields will return the count of choices selected. All other field types will return the count of records in which the specified field is not null. 

  • distinct(<field_link_name>) ​

    Returns: Distinct values a field holds in a collection of records.
    Return type: List.
    Applicable field types: All field types.

  • maximum(<field_link_name>) ​

    Returns: Largest value of a specified field in a collection of records
    Applicable field types and the return type: Percent, Currency, Decimal, Formula(decimal) field types will return Decimal data-type. Number and Formula(bigint) field types will return Bigint data-type. Date and Date-Time field types will return Timestamp data-type.

  • median(<field_link_name>) ​

    Returns: Median value of a specified field in a collection of records sorted in ascending order.
    Applicable field types and the return type: Percent, Currency, Decimal, Formula(decimal) field types will return Decimal data-type. Number and Formula(bigint) field types will return Bigint data-type.​

  • minimum(<field_link_name>) ​

    Returns: Smallest value of a specified field in a collection of records.
    Applicable field types and the return type: Percent, Currency, Decimal, Formula(decimal) field types will return Decimal data-type. Number and Formula(bigint) field types will return Bigint data-type. Date and Date-Time field types will return Timestamp data-type.

  • sum(<field_link_name>) ​

    Returns: Total of all values of a specified field in a collection of records.
    Applicable field types and the return type: Percent, Currency, Decimal, Formula(decimal) field types will return Decimal data-type. Number and Formula(bigint) field types will return Bigint data-type.

Fetch field value from the first record

To fetch a field value from the first record in a collection, use the following code:

<variable> = <collection_variable>.<field_link_name>;

where,

<field_link_name>

Link name of the field whose value has to be returned from the first record in the collection.

Example

The following syntax returns the employee id of the first record in collection variable employeesJoinedToday, and stores it the variable named empID. Employee_ID is the link name of a field.

empID = employeesJoinedToday.Employee_ID;

Fetch values of a field from all the records in a collection

<variable> = <collection_variable>.<field_link_name>.getAll();

where,

<field_link_name>

Link name of the field whose values have to be returned from all the records in the collection.

Example

The following syntax returns the employee id field values from all the records in collection variable employeesJoinedToday, and stores it the variable named empID. Employee_ID is the link name of a field.

empID = employeesJoinedToday.Employee_ID.getAll();

Iterating through records in a collection variable

To iterate through the records in a collection variable, use the following code:

for each <record_variable> in <collection_variable>
  {
     fieldValue = <record_variable>.<field_link_name>;
  }

where,

<record_variable>

Variable to hold the record for each iteration.

<field_link_name>

Link name of the field whose value has to be returned.

fieldValue

Variable to hold the specified field's value for each iteration.

The data-type of the returned value will depend on the field type. 

This table lists the applicable data-type for each field.

Example

The following code iterates through the records in collection variable employeesJoinedToday, and sends an email to each email address present in the collection. Email is the link name of the email field type.

for each employee in employeesJoinedToday
  {
      sendmail
         [
           from: zoho.adminuser
           to: employee.Email
           subject: "Zylker : Offer Letter"
           message: "Welcome to Zylker. Please find your offer letter attached with this email."
         ]  
   }

Updating field value in a collection variable

  • To update a field value in the first record in a collection, use the following syntax:

    <collection_variable>.<field link name> = <expression>;

    where,

    <field_link_name>

    Link name of the field whose value has to be updated.

    <expression>

    Updated value to be assigned to the field. The data-type of this value must match the data-type of the field. Any mismatch in the data-type will stop the execution during runtime.

    See the table below to understand more about the applicable values for each field type.

  • ​​To update a field value in every record in a collection, use the below code:

    for each <record_variable> in <collection_variable>
    {
    <record_variable>.<field_link_name> = <expression>;
    }

    where,

    <record_variable>

    Variable to hold the record for each iteration

    <field_link_name>

    Link name of the field whose value has to be updated.

    <expression>

    Updated value to be assigned to the field. The data-type of this value must match the data-type of the field. Any mismatch in the data-type will stop the execution during runtime.

    See the table below to understand more about the applicable values for each field type.

Example

The following syntax updates the Batch Sequence("number" field type) field with the value 118 for all the records in the collection variable employeesJoinedToday.

for each emp in employeesJoinedToday
  {
    emp.Batch_Sequence = 118;
  }

Applicable values for each field type

Field typeData-typeApplicable notes
NameText

You must specify only the link name of the "Name" field when assigning it a value copied from another "Name" field.

In all other cases, you have to give the values for its sub-fields separately as given below:

<field>.prefix: denotes the "prefix" field.

<field>.first_name: denotes the "first_name" field.

<field>.last_name: denotes the "last_name" field.

<field>.suffix: denotes the "suffix" field.

You can set value for the above mentioned sub-fields even if they are not selected in the field's property.

Note:

  • If more than one Name fields are present in the form, the link names of the subsequent name sub fields will be appended by a number starting from 1. So, if we take the example of the subfield <field>.prefix, the link name of the first such field will be <field>.prefix, the link name of the second such field will be <field>.prefix1, the link name of the third such field will be <field>.prefix2, and so on. 
EmailText

The format of the specified email address must be valid. If the format of the specified email address is invalid, execution will be stopped during runtime.

The number of characters in the value should not exceed the length specified in the "Character maximum" field property. If it exceeds, execution will be stopped during run-time.

AddressText

You must specify only the link name of the "Address" field when assigning it a value copied from another "Address" field.

In all other cases, you have to give values for its sub-fields separately as given below:

<field>.address_line_1: denotes the "Address Line 1" field.

<field>.address_line_2: denotes the "Address Line 2" field.

<field>.district_city: denotes the "City / District" field.

<field>.state_province: denotes the "State / Province" field.

<field>.postal_Code: denotes the "Postal Code" field.

<field>.country: denotes the "Country" field.

<field>.longitude: denotes the "longitude" field.

<field>.latitude: denotes the "latitude" field.

You can set values for the above mentioned sub-fields even if they are not selected in the field's property.

Note:

  • If more than one address fields are present in the form, the link names of the subsequent address sub fields will be appended by a number starting from 1. So, if we take the example of the subfield <field>.country, the link name of the first such field will be <field>.country, the link name of the second such field will be <field>.country1, the link name of the third such field will be <field>.country2, and so on. 
PhoneText

Value can be specified with or without the country code, even if the "Country code options" and "Default country code" field properties are not selected.

Special characters like (), and - are supported to accept different phone number formats of various countries.

Single LineTextThe number of characters in the value should not exceed the length specified in the "Character maximum" field property. If it exceeds, execution will be stopped during run-time.
Multi LineTextThe value can be plain text of maximum 64 kb size.
NumberNumberIf the number of digits specified in the value exceeds the length specified in the "Max Digits" field property, the extra digits will be trimmed from the right.
DateDate-time

All dates in a week can be specified as value, irrespective of the selected days in "Allowed Days" property.

Time value, if specified, will not get added.

Drop DownText

You can specify a new choice apart from pre-defined choices even if the "Allow Other Choice" field property is not selected.

If a new choice is specified as the value, it only gets saved in the record and is not added as a new choice to be selected from the form UI. And that choice cannot be retrieved back if modified.

RadioText

You can specify a new choice apart from existing choices even if the "Allow Other Choice" field property is not selected.

If a new choice is specified as the value, it only gets saved in the record and is not added as a new choice to be selected from the form UI. And that choice cannot be retrieved back if modified.

Multi SelectText list

You can specify a new choice apart from the pre-defined choices.

If a new choice is specified as the value, it only gets saved in the record and is not added as a new choice to be selected from the form UI. And, if you change that choice and update the record, the choice will no longer be available.

CheckboxText list

You can specify a new choice apart from the pre-defined choices.

If a new choice is specified as the value, it only gets saved in the record and is not added as a new choice to be selected from the form UI. And, if you change that choice and update the record, the choice will no longer be available.

Decision boxBoolean

You must specify the value as True or False, with or without double quotes.

If any other value is specified, execution will be stopped during run time.

Rich TextTextThere is no restriction to the number or type of characters that can be specified as the value.
UrlText

Value must be specified in the following format (title, linkname and target are optional params) :

<a href= \"<URL>\" title = \"<TITLE>\" target = \"_blank\"><linkname></a>

If a url is specified directly, the URL field will get saved with a null value.

ImageText

Value must be specified in the following format (title, linkname and target are optional params) :

<a href= \"http://<LINKNAME>\" title =\"<TITLE>\" target = \"_blank\"><img src = \"<URL>\" title =\"<TITLE>\"></img></a>

You must specify the image url as the value, even if the "Browse Options -Link" field property is not selected.

PercentDecimal

If the number of digits specified in the value exceeds the length specified in the "Max Digits" field property, the extra digits will be trimmed from the right.

If the specified decimal points exceed the number specified in "Decimal Points" field property, the extra decimal points will be trimmed from the right. 

CurrencyDecimal

If the number of digits specified in the value exceeds the value specified in the "Max Digits" field property, the extra digits will be trimmed from the right.

If the specified decimal points exceed the number specified in "Decimal Points" field property, the extra decimal points will be trimmed from the right. 

DecimalDecimal

If the number of digits specified in the value exceeds the value specified in the "Max Digits" field property, the extra digits will be trimmed from the right.

If the specified decimal points exceed the number specified in "Decimal Points" field property, the extra decimal points will be trimmed from the right.

Date-TimeDate-time

All days and hours can be specified as value, irrespective of the selected days and hours in "Allowed Days" and "Allowed Hours" property. 

If Time value is not specified, 00:00:00 will be set as the time value.

Lookup

(Display type - Dropdown/Radio Button)

Number

You must specify the record id of the lookup form as the value. 

Lookup

(Display type - Multi Select/Checkbox)

Number listYou must specify the record ID of the lookup form as the value.
File UploadFile UploadOnly a file value fetched from another File Upload field type should be specified as a value.
AudioAudioOnly an audio file fetched from another Audio field type can be specified as a value.
VideoVideoOnly a video file fetched from another Video field type can be specified as a value.
FormulaNumber/Text/DecimalFormula field is a read-only field type.
UsersText

You can specify username of any user or developer added in your current app. 

You can view the list of users and developers in your account by navigating to Settings > Users. From this page, you can also add more users and developers to your account.

If you enter an incorrect username, execution will be stopped during run-time.

You must only specify the username even if the "Field type" field property is set to Email Id.

Integration (Zoho CRM)Text

You must specify the record id in the selected module as the value. If you enter an incorrect id, execution will be stopped during run-time. 

Learn how to fetch the ID of a record after creating it or fetching it.

Integration (Zoho Recruit)Text

You must specify the record id in the selected module as the value. If you enter an incorrect id, execution will be stopped during run-time.

Learn how to fetch the ID of a record after creating it or fetching it.

Integration (Salesforce)Text

You must specify the record id in the selected module as the value. If you enter an incorrect id, execution will be stopped during run-time.

Learn how to fetch the ID of a record after creating it or fetching it.

Integration (Zoho Books)Text

You must specify the record id in the selected module as the value. If you enter an incorrect id, execution will be stopped during run-time.

Learn how to fetch the ID of a record after creating it or fetching it.

Integration (Quickbooks)Text

You must specify the record id in the selected module as the value. If you enter an incorrect id, execution will be stopped during run-time.

Learn how to fetch the ID of a record after creating it or fetching it.

Note:

  • A collection variable can be passed as a form object in a function. 

Get Started Now

Execute