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:
1. 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.
2. 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.
3. distinct(<field_link_name>)
Returns: Distinct values a field holds in a collection of records.
Return type: List.
Applicable field types: All field types.
4. 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.
5. 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.
6. 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.
7. 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,
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,
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,
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;
- }