3. Use Case - Export Subform Rows to Zoho Sheet in a Single Click!
Zylker manufactures medical instruments, and its sales representatives frequently need to share bulk order details with distributors and hospital partners using Zoho Sheets. These details such as product names, quantities, and prices are captured in a Subform on the Purchase Order Detail(Standard) Page.
To simplify this, the admin wants to add a custom button called "Export Products" on the Purchase Order Detail Page. When clicked, it should export the Purchase Items from the Subform and move the content to the specified sheet.
4. Solution
To export subform rows to Zoho Sheet with a single click, you can add a
custom button to the
Detail Page of the record. When clicked, a
Client Script will be triggered to fetch the subform data from the Detail Page and pass it to a Function that uses
zoho.sheet.createRecords() to insert data into Zoho Sheet.
Here’s how to implement this:
- Add “Export Products” custom button on the Detail Page.
- Add the script to collect subform rows which invokes the Function.
- Write a Function to create entry in Zoho Sheets
A. Add “Export Products” custom button on the Detail Page.
- Go to Setup → Modules and Fields under Customization.
- Select a Module as Purchase Order.
- Click on Buttons → Then click + New Button.
- Enter Button Name and select Action Type as "Client Script"
- Choose Button Position and Layout details as shown in the following image.
- Click Create in Configured Client Script, enter the script, and click Add.
- Select the profiles for which these buttons should be visible.
- Click Save.
B. Add the Script to collect subform rows and invoke the Function
Use the following Client Script when you configure the custom button.
- var casesheetid = ZDK.Client.getInput([{ type: 'text', label: 'Enter the Sheet ID' }], 'Sheet details', 'OK', 'Cancel');
- if (casesheetid == null) {
- ZDK.Client.showAlert("Enter the *Case Sheet ID - Import* to import data");
- }
- var purchase_items = ZDK.Page.getField('Purchase_items).getValue();
- ZDK.Client.showLoader({type: 'page', template:'spinner', message: 'Export in progress, please wait'});
- var c=1;
- if (sheetid == null) {
- ZDK.Client.hideLoader();
- ZDK.Client.showAlert("Enter the *Case Sheet - Export* ID to export data");
- }
- purchase_items.forEach((r,i) => {
- try {
- resp = ZDK.Apps.CRM.Functions.execute("csvWrite", { "Product_Name": r.Product_Name.name, "List_Price": r.List_Price, "Discount": r.Discount, "Total": r.Total,"Sheetid":casesheetid }); }
- catch (error) {
- c = 0;
- ZDK.Client.hideLoader();
- ZDK.Client.showAlert("Unexpected issue occured while adding data in row number "); }
- });
- ZDK.Client.hideLoader();
- if (c) {
- ZDK.Client.showMessage("Export completed. Please check the Sheet");
- }
- else
- {
- ZDK.Client.showMessage("Unexpected error "); }}
In this code, several ZDKs are used for various purposes. Click on the ZDK hyperlinks to learn more.