Table of Contents
Zoho Creator supports Form Objects that can be passed as argument to a user-defined Function. A Form Object represents a collection of records in a Form. In the following function named "calculateNetPay", emp is the form object which represents a collection of records in Employee Form. This form object is passed as argument to the function to process the employee records by iterating the collection.
Void calculateNetPay(Employee emp)
for each emp1 in emp
Sample 1: Create a collection variable and pass this variable as form object. For example, in the sample code given below, empRec is the collection variable that holds the records in the Employee form with the specified criteria. You can pass the employee result set with sort and range property.
empRec = Employee[Designation == input.Designation] sort by Added_Time range from 1 to 5;
Sample 2 : In this format you cannot pass the sort and range property along with employee result set. Here, "ID" refers to the record ID.
Sample 3 : This is very simple way of passing the current record as form object value.
The function with form object as argument can be configured as custom action in the view. The sample application "Custom Action Example" demonstrates this scenario.
About the application :
The sample application "Custom Action Example" comprises of the following forms and views.
- Employee form to enter employee details
- Employee view that displays the employee data.
- Promotion and Pay hike form which is a stateless form to enter the pay hike in percentage.
A Custom action named "PayHike & Promotion" is configured for the "Employee View" to calculate the pay hike for selected records in this view. The custom action invokes the function named "showPayHikeForm" which passes the Employee form as Form object argument to the function.
The custom action named PayHike & Promotion is configured for the Employee view, with function showPayHikeForm as shown in the screen-shot given below. The option show action in view header is selected, to display the custom action in the view header. The option Execute action for collection of records, is selected, in which case the custom action will be called only once for executing a collection of records. This option is supported only for functions with formobjects as arguments.
The custom action is displayed in the view header as shown below:
The function "showPayHikeForm" is defined with the Employee form as argument to the function. The function forms a querystring with the employee ID of the selected records in the Employee view and opens the stateless form named "Promotion and Pay Hike form " appended with the query string (selected employee ids). The function code is given below for reference.
void employee.showPayHikeForm(Employee employees)
querystring = "";
for each employee in employees
querystring = querystring + "empid=" + employee.ID + "&";
openUrl("#Form:Salary_HikeForm?" + querystring, "popup window", "height=100,width=100");
"on click" script
The above function "showPayHikeForm" opens the Promotion and Pay Hike form ( Stateless form ) with the query string (selected employee ids). The following script is added to the "on click" button of this form. When a user submits the hike percentage, the querystring is updated in the empid field of this form ( this field is configured as a lookup field - multi-select type and hidden in the form).
In the following code,
- | is the list that contains the employee ids.
- For each employee id in the list, the employee record is fetched from the Employee form and updated with the new salary and new designation.
- The Employee view is displayed with the new salary and designation.
l = input.empid;
for each emp in l
employeeinfo = Employee [empcode == emp];
newsalary = (employeeinfo.Salary * (1 + input.Pay_Hike / 100));
employeeinfo.Salary = newsalary;
if (input.Designation != "-Select-")
employeeinfo.Designation = input.Designation;
openUrl("#View:Employee_View", "same window");