Build and Merge HTML Table into a Zoho Writer Template

Core Idea

Suppose you have a subform called Items in Deals. On a click of a button, you want to generate a PDF document via Zoho Writer with the subform rows merged into a table. If the number of rows in the table is fixed, you can create the table on the Writer template, set plain text merge fields as the table values, and merge it with Deluge. However, if the number of rows in the table varies, you need to dynamically create the table in Deluge, then merge the entire table into the Writer template via a Rich Text Field.


Create a Rich Text Merge Field on Your Writer Template

On the side toolbar, go to Fields –> Merge Fields (Under Dynamic Fields) –> Click on Create Fields –> Add a New Field (Rich Text) In this example, the rich text field label is “table”.

Write Your Deluge Function

Get the Subform

Change “Items” to your respective subform name.

This is just an example. For your application, it could be a subform, related list, or any other types of list.
					deal = zoho.crm.getRecordsbyId("Deals",dealid);
items = deal.get("Items");

Build the Table

The meat of the script starts here. Once you have your list (in this example, Items subform), do the following:

Set the Headers for the Table & Define the Counters

  • headers: Start building your table here by creating the headers and customizing the table properties (cellpading/cellspacing/width/background color/etc).
    • In this table example, the headers are Name, SKU, Serial Number and Quantity.
  • bodytext: Set to an empty string. This will be used to build the rows of the table.
  • n: Set it as 0. This will be used later to alternate the table row colors.
					headers = "<table cellpadding=8 cellspacing=4 style=width:100%><tr bgcolor=#F3F6F9><td><b><font color=000000>Name</font></b></td><td><b><font color=000000>SKU</font></b></td><td><b><font color=000000>Serial Number</font></b></td><td><b><font color=000000>Quantity</font></b></td></tr>";
bodytext = "";
n = 0;

Iterate through the List & Build the Table Rows

  • Get the Name, SKU, Serial Number and Quantity values for each element of the list.
  • Insert the script that alternates row colors. You can customize the row color to anything you desire, just specify the hex code.
  • At each iteration, we +1 to n.
  • Then, define hex with an if condition that sets that value in an alternating pattern with the isOdd function.
  • Build the table row with the bodytext variable.
  • Finally, outside the loop, combine the headers and bodytext and add a command to close the table.
					for each  i in items
	name = i.get("Name");
	SKU = i.get("SKU");
	serial = i.get("Serial_Number");
	quantity = i.get("Quantity");
	// This gives the table rows alternating colours
	n = n + 1;
		hex = "FFFFFF";
		hex = "F3F6F9";
	// Build the bodytext for the table
	bodytext = bodytext + "<tr bgcolor=" + hex + "><td>" + name + "</td><td>" + SKU + "</td><td>" + cereal + "</td><td>" + quantity + "</td></tr>";
fulltable = headers + bodytext + "</table>";
info fulltable;
  • You now have the full table that should look like this:

Merge the fulltable variable into the Rich Text Field

Your HTML table is ready. Just merge it into the Rich Text Merge Field in your Zoho Writer template, and you’re good to go!
Click here to copy these scripts. For more Zoho-wizardry, check out our GitHub page.

Contact Us!

Book a free 30-minutes consultation with a Zoho expert or send us an email

Related Resources