Hi guys,
Let me share a tip to develop original business form and document such as invoice or receipt in Zoho creator.
Problem:
When you have to write a template of invoice with HTML in Zoho creator / Deluge script, what you would do is to write a function to return HTML strings so that the invoice would be generated dynamically with client name, product and whatever.
Here is an example of codes from generateInvoice function of
Order Management app provided by Zoho officially.
This function will be called inside a Page.
- string Invoice.generateInvoice(string recId) {
- output = output + "<div class='invDiv'>";
- output = output + "<div class='invHead'>Invoice</div>";
- //Customer Details
- output = output + "<div style='width: 80%;margin: 30px auto;margin-bottom: 0px;text-align:right;'><span class='keyCol'>Date: </span><span class='valCol'>" + zoho.currentdate + "</span></div>";
- output = output + "<table class='tableSize' align='center'><tr><td style='width:50%;vertical-align: top;'>";
- output = output + "<table border='1' align='left' cellspacing='0' style='border-color:#EEE;width:95%;margin:12px 0px 0px 0px' id='billingAdd'>";
- output = output + "<tr align='left'><td class='rowHead' style='width:40%'>Name</td><td class='rowVal' style='width:60%'> " + customerRow.First_Name + " " + customerRow.Last_Name + "</td></tr>";
- ......
- return output;
- }
Honestly, this way of developing a template doesn't seem the best way to me.
Solution and its benefits:
Build a lightweight and simple API to return HTML / CSS string and call it from the script instead of building HTML there.
Here is bundled sets of codes to build the API in a minute in my github repository.
You can write template of business documents with
Pug (formerly known as "Jade") which is a template engine for Javascript.
The syntax is simple and modern.
Also bizform-generator can be hosted on AWS (Labmda and API Gateway) in a minute thanks to "Serverless framework".
Sample codes in Zoho Creator:
1. Build a function in Zoho creator to call the bizform-generator you have built
- string Docs.generateWithAPI(int recID, string templateName)
- {
- output = "";
- params = Map();
- // build your own templateParams as per templateName with your app's actual forms and fields. The codes below is just a sample.
- if(templateName == "example") {
- order = Orders[ID == recID];
- clientName = order.clientName;
- note = order.note;
- items = List();
- for each item in order.Items {
- itemMap = Map();
- itemMap.put("name", item.name);
- itemMap.put("amount", item.amount);
- itemMap.put("price", item.price);
- items.add(itemMap);
- }
- templateParams = {"clientName":clientName, "items":items, "note", note};
- } else if (templateNmae == "otherTemplate") {
- // build templateParams for "otherTemplate"
- }
- params.put("templateParams",templateParams);
- params.put("templateName",templateName);
- response = postUrl("https://subdomain.amazonaws.com/dev/bizform",params.toString());
- htmlStr = response.getJSON("html");
- output = htmlStr;
- return output;
- }
2. Prepare a generalized Page for any document
- htmlpage APIDoc(id, template_name)
- displayname = "Document"
- print = true
- content
- <%{
- %>
- <%=thisapp.Docs.generateWithAPI(id.toLong(),template_name)%>
- <%
- }%>
3. Prepare a function to show the specific document "Invoice"
- void Docs.viewInvoice(int recID)
- {
- templateName = "example";
- openUrl("#Page:APIDoc?id=" + recID + "&template_name=" + templateName + "&zc_Header=false","Popup Window");
- }
In this way, you could have as many kinds of templates as you need easily by adding your templates in the API and building corresponding templateParams in Docs.generateWithAPI.
Example of pug template are
here.
See
README to see how to deploy and host the API etc...
Please let me know if you have any questions or feedbacks. Thanks.