A subform is a form within a form. The primary form is called the main form, and the form that is inserted in the main form is called the subform. Subforms are generally used to show data from tables that have a one-to-many relationship. For example you would use a subform if you wanted to display an order with the order details. In this case, the order information would display on the "main" form and the order details would display on the subform. The data in the order table is the "one" side of the relationship. The data in the Item table is the "many" side of the relationship — each order can have more than one item. The sample application "Order Management" illustrates the subform feature.
Creating a Subform
Zoho Creator supports the "SubForm" field type using which you can insert a subform into your mainform. Please find the steps given below to create a subform.
- Add the required Forms to your application from the dashboard page by selecting Create New -> Form option. For example, add the "Items" form to enter the Items ordered and the "Order" form to enter details about each order. Here, "Order" form is the main form and "Items" form is the subform.
- In the edit mode of the main form, simply drag-n-drop the subform field type at the required position in your editor area. For example, drag-n-drop the "Items" form into the "Order" form.
- In the Adding SubForm screen, a dialog box appears where you have to select the Form to be added as the subform field and click on Done. For example, select the "Order Item" form in the "Order Management" application as the subform. Click Done to insert the subform to the main form. You can also select the Blank form option and directly create the subform within the main form.
- The subform will be inserted to the main form as shown below:
- When you access the application, the Subform will be displayed within the main form, as shown below. Here, the "Order form" displays the "Items" subform to add the items ordered. You can use the "Add +" or the "Add New" button to add entries to the main form from the subform.
- The Main Form report, (Order View) will display all the orders with the values of the selected subform fields, as shown below.
- The Column Properties option in the Main Form report is used to configure the subform report and the subform fields to be displayed in the report, as show below.
Creating Relationship between Mainform and Subform
Create a lookup in the subform and relate it to the parent form. Once the lookup field is created in the child form, that field can be used as relationship field between the two forms. When adding the lookup, select the existing subform relation and pick the ID field, as shown in the image attached.
Select the subform report from the Reports tab, click on "Column Properties" and select the lookup field created above and click on "Link " button, to create a link to the main form data.
Set Default Entries and Limit Maximum Entries
You can set the "Default Entries" and "Maximum Entries" for a subform field type from the "Appearence" section under the field properties tab of the subform field.
- The "Set Default Entries" will display the specified number of rows in the subform, when the form is loaded.
- The "Limit Maximum Entries" will limit the number of entries that can be added to a subform.
Export Main Form Data with Subform
Lets assume we have a main form named "Orders" form and a subform named "Items" subform. To export main form data with subform,
- Select the main form report from the Reports tab.
- From the "Column Properties" option enable the "Link" button displayed for the subform, as shown in the screen-shot below, and click "Done" to update the changes.
- Use the following URL format to export data from the main form report. This automatically includes the data of the main form in one tab and also the related subform data in another separate tab within the same excel sheet. The record link id column generated for the main form report can be used to refer the related subform entries.
For example : https://creatorexport.zoho.com/sampleapps/export-data/xls/Order_Report/exportDisplayData=false
Import data into a Form with Subform
To import data from an XLS file into a Form with subform, the XLS file must contain the main form data and subform data in seperate tabs and the columns must be in a specific format.
a. Creating the XLS file in the specific format
An easy workaround to create this XLS file will be to add a sample record into the Form with subform and then export this data in XLS format, as specified in the section Export main form data with subform. For example, the XLS file generated by exporting main form named "Orders" and a subform named "Items", will be in the format as explained below.
- The exported XLS file gets created with 2 Tabs - "Order_Report" tab and "Items_Report" tab. Note that these tab names should match with the view link names in your application exactly.
- The Order_Report tab contains the columns Title, Name, Email etc. as defined in the "Order" form. Similarly the subform "Items_Report" tab also contains the respective columns as the fields specified in "Items" subform.
Screenshot displaying the Order Report tab in the exported xls file
Screenshot displaying the Items Report tab in the exported xls file
- The "Items_Report" tab includes an additional column "REC LINK ID" in addition to the other field columns. This "REC LINK ID" column has a number-id like "8099932" which is unique for each row. This number-id is present in the"Order_Report" tab under the "Items" column. The Rec Link Id links the main form with subform using the number-ID.
b. Importing the data
- Populate the required data into the XLS file created above.
- Access the main form Report in live mode and click on Settings icon - Import Data from the Report header.
- Select the File Type as Excel and the Data Location as Local Drive.
- Browse and give the XLS file created above and import data. (The tab generated for the main form report must be selected in the XLS file). It will import data across the 2 forms - mainform and subform.
Customize Subform field size
To customize the size of the fields within the subform:
- Go to subform Field properties -> Field width customization -> Adjust field width manually
- In the popup window, select the required field size and click on Done to save the changes.
- The subform layout size can be customized from subform Field Properties -> Appearance -> Field Size.
The Subform name represents the entire collection of records in a subform. ID value will be available only after the subform record gets persisted in the database. The expressions <collection>.<subform_name> will return another collection. A row is a pre-defined variable available in on add row, on delete row and on user input action of a subform field. You can access, update and dynamically append or clear values of subform field using a row.
To execute tasks when a row is added or deleted in the subform
The following field action script is supported for subform field types, as shown below.
- On Add Row: To execute tasks when a new row is added to the subform.
- On Delete Row: To execute tasks when an existing row is deleted from the subform.
To execute tasks on user input of subform fields
The following Field Action Script is supported for all the fields within the Subform.
- On User Input : To execute tasks when a value is input by the user. For example, in the script given below, the Unit Price of each item selected is fetched from the "Products" form and updated as the "Unit Price" in the subform.
To add items to a picklist
You can add items to a picklist in a subform using the UI.append task. Refer this help topic for syntax and sample code.
To add items to a picklist in a subform based on selection in main form:
- The “contacts” stores the Employees of each Client.
- The “avtivities” form is the main form which has a lookup to the “Client” on the parent form and the “Employee” field on the subform.
- The single select “Employee” field on the subform can be limited based on the selected client by adding “on Add Row” script on the subform A sample script is given below. The script will be executed when a row is added to the subform.
if(count(contacts[Client == input.client_field]) >0)
emps = contacts[Client == input.client_field];
ls = emps.contact.getAll();
Changing the selected Client after adding the subform rows will not reload the employees list. A script to limit the options is given below. This script should be added to the on user input block of the client_field.
for each y in emp_activity
emps = contacts[Client == input.client_field];
ls = emps.contact.getAll();
To Hide / Disable sub form fields
You can Hide and Disable subform fields in theon load action of main form andon user input action of main form fields.The syntax for hiding and disabling subform field is as follows:
hide <sub form name>.<field name>;
show <sub form name>.<field name>;
enable <sub form name>.<field name>;
disable <sub form name>.<field name>;
The above tasks when executed perform the operation on entire column. For ex, when hide task is applied on subform field, it hides the entire column in the subform grid.
To populate fields in a subform
You can use the following script to populate a field in the subform with the value of the field in the main form. In the "On Add Row" task of the subform, give the script as
row.SubFormField = input.FormField;
To create an HTML view with subform records
The Sample application Subform in HTML View opens a HTML view on submit of a record in the Employee Form. The on add ->on success script added to the Employee Form, calls the HTML view named “Print” using theOpenUrl task. The HTML view prints the record with the subform data.
Copy the application to your account and view the script,
To send subform details in an email
To send subform data in an email, you can iterate the subform data and concatenate with the message of an email. Please find the sample code format given below:
message= “Details of the data added ”;
for each item in <Field_Name> // subform field name in the main form
message=message+”Employee no:”+item.Employee_No;// variable.Field_Name_In_The_Subform
The Sample application Subform Details in Email comprises of a Student Form which is a parent form with subform. A script is added to the on add ->on success of the “Student” form to send email with subform data
To add multiple attachments to a record
You can add multiple attachments to a record, using Subform feature. Create a form with a single “File Upload” field type and add it as a Subform field to your primary form, to add multiple attachments per record.