An article to save time where a customer wants to click a button to generate a merged document in Writer and attach it to the CRM record.
Why?
Our use-case is that we have a client who has to go to the CRM record, click on Mail Merge, which opens Writer, then has to click on various settings and run the Mail Merge; then has to click on "execute function after merge" and select the function to run (this would initially attach the file to the CRM record). This wasn't working really as there are issues around tweaking the function and getting it to understand basic mapping and attachments.
How?
As per our use-case above, we are going to create a button off the contact record which when the user clicks on it will generate a PDF of the merged template and attach it to the CRM contact record under "Attachments".
As a quick outline of the below:
- Create a connection to Workdrive/Writer
- Get the Merged Template ID (hexadecimal)
- Create the all-in-one button and function to generate and attach the file.
First Create a CRM connection
I'm going to create a Zoho OAuth connection with open scopes just to get a working example; I can then reduce the scopes permissions afterwards when locking it down for security:
- Login to ZohoCRM > Setup > Developer Space > Connections > Zoho OAuth
- Give your connection a name, I'm calling mine "Joels Connector"
- Select the Scopes: ZohoWriter.documentEditor.ALL and ZohoWriter.Merge.ALL
- For good measure, I'm going to include the scopes: WorkDrive.files.ALL, WorkDrive.workspace.ALL
- Click on Create and Connect > Accept
Get the Template ID
The following snippet is a separate function to run (standalone) and will return to you the templates and the field references:
copyraw
You need to select the id of the template that you want to use for the next step (will be an alphanumeric long string).r_Templates = invokeUrl [ url: "https://zohoapis.com/writer/api/v1/templates" type: GET connection: "joels_connector" ]; info r_Templates; // // yields something like: /* { "total_count": 5, "templates": [ { "created_time": "2019-08-28T16:30:33Z", "modified_time_ms": 1567009834477, "owner_id": "12345678", "last_opened_time": "2019-08-28T16:30:33Z", "open_url": "https://writer.zoho.com/writer/template/a1b2c3d4", "last_opened_time_ms": 1567009833936, "type": "template", "thumbnail_url": "https://writer.zoho.com/writer/thumbnail/a1b2c3d4e", "created_by": "joel", "created_time_ms": 1567009833936, "modified_time": "2019-08-28T16:30:34Z", "preview_url": "https://writer.zoho.com/writer/zwpreview/a1b2c3d4e5f6g", "creator_id": "23456789", "name": " New Template", "id": "aaaa1111bbbb2222cccc3333dddd4444eeee5", "permalink": "https://docs.zoho.com/file/a1b2c3d4", "status": "active" }, { "created_time": "2019-08-28T16:30:33Z", "modified_time_ms": 1567009834477, "owner_id": "34567890", "last_opened_time": "2019-08-28T16:30:33Z", "open_url": "https://writer.zoho.com/writer/template/z9y8x7w6", "last_opened_time_ms": 1567009831234, "type": "template", "thumbnail_url": "https://writer.zoho.com/writer/thumbnail/z9y8x7w6v", "created_by": "mylesser", "created_time_ms": 156700981111, "modified_time": "2019-08-28T16:30:34Z", "preview_url": "https://writer.zoho.com/writer/zwpreview/z9y8x7w6v5", "creator_id": "45678901", "name": " New Template", "id": "jjjj7777kkkk8888llll9999mmmm0000nnnn1", "permalink": "https://docs.zoho.com/file/a7iyybadb", "status": "active" } ] } */
- r_Templates = invokeUrl
- [
- url: "https://zohoapis.com/writer/api/v1/templates"
- type: GET
- connection: "joels_connector"
- ];
- info r_Templates;
- //
- // yields something like:
- /*
- {
- "total_count": 5,
- "templates": [
- {
- "created_time": "2019-08-28T16:30:33Z",
- "modified_time_ms": 1567009834477,
- "owner_id": "12345678",
- "last_opened_time": "2019-08-28T16:30:33Z",
- "open_url": "https://writer.zoho.com/writer/template/a1b2c3d4",
- "last_opened_time_ms": 1567009833936,
- "type": "template",
- "thumbnail_url": "https://writer.zoho.com/writer/thumbnail/a1b2c3d4e",
- "created_by": "joel",
- "created_time_ms": 1567009833936,
- "modified_time": "2019-08-28T16:30:34Z",
- "preview_url": "https://writer.zoho.com/writer/zwpreview/a1b2c3d4e5f6g",
- "creator_id": "23456789",
- "name": " New Template",
- "id": "aaaa1111bbbb2222cccc3333dddd4444eeee5",
- "permalink": "https://docs.zoho.com/file/a1b2c3d4",
- "status": "active"
- },
- {
- "created_time": "2019-08-28T16:30:33Z",
- "modified_time_ms": 1567009834477,
- "owner_id": "34567890",
- "last_opened_time": "2019-08-28T16:30:33Z",
- "open_url": "https://writer.zoho.com/writer/template/z9y8x7w6",
- "last_opened_time_ms": 1567009831234,
- "type": "template",
- "thumbnail_url": "https://writer.zoho.com/writer/thumbnail/z9y8x7w6v",
- "created_by": "mylesser",
- "created_time_ms": 156700981111,
- "modified_time": "2019-08-28T16:30:34Z",
- "preview_url": "https://writer.zoho.com/writer/zwpreview/z9y8x7w6v5",
- "creator_id": "45678901",
- "name": " New Template",
- "id": "jjjj7777kkkk8888llll9999mmmm0000nnnn1",
- "permalink": "https://docs.zoho.com/file/a7iyybadb",
- "status": "active"
- }
- ]
- }
- */
The button function
Create a button off the contacts record by going to ZohoCRM > Setup > Customization > Modules and Fields > Contacts > Links and Buttons > Create Button where the function arguments will be p_ContactID = Contacts.Contact_ID
copyraw
v_OutputMessage = ""; r_ContactRecord = zoho.crm.getRecordById("Contacts", p_ContactID); // // get merge field names (for reference) v_TemplateID = "aaaa1111bbbb2222cccc3333dddd4444eeee5"; r_Fields = zoho.writer.getMergeFields(v_TemplateID, "joels_connector"); info r_Fields; // // map data to these fields m_Data = Map(); m_Data.put("Contact.First_Name", r_ContactRecord.get("First_Name")); m_Data.put("Contact.Last_Name", r_ContactRecord.get("Last_Name")); m_Data.put("Issue.Date", zoho.currentdate.toString("MMMM, dd yyyy")); m_Data.put("Organization.Name", r_ContactRecord.get("Account_Name").get("name")); // m_MergedData = Map(); m_MergedData.put("merge_data", {"data":m_Data}); // // to generate and attach r_DownloadResponse = zoho.writer.mergeAndDownload("aaaabbbbccccddddeeeeffff000011112222333344445555", "pdf", m_MergedData, "joels_connector"); r_AttachResponse = zoho.crm.attachFile("Contacts", p_ContactID, r_DownloadResponse); if(r_AttachResponse.get("code")=="SUCCESS") { v_OutputMessage = v_OutputMessage + "Generated and attached file to CRM record. "; } // // to generate and send via email m_MergedData.put("subject","Document for " + r_ContactRecord.get("Salutation") + " " + r_ContactRecord.get("Last_Name")); m_MergedData.put("message","Please find attached your document"); r_SendResponse = zoho.writer.mergeAndSend("aaaabbbbccccddddeeeeffff000011112222333344445555", "pdf", r_ContactRecord.get("Email"), m_MergedData, "joels_connector"); if(!isNull(r_SendResponse.get("records"))) { v_Grammar = if(r_SendResponse.get("records").size()==1,"","s"); v_OutputMessage = v_OutputMessage + "Generated and sent " + r_SendResponse.get("records").size() + " file" + v_Grammar + " to " + r_ContactRecord.get("Email") +". "; } // // return response to user return v_OutputMessage;
- v_OutputMessage = "";
- r_ContactRecord = zoho.crm.getRecordById("Contacts", p_ContactID);
- //
- // get merge field names (for reference)
- v_TemplateID = "aaaa1111bbbb2222cccc3333dddd4444eeee5";
- r_Fields = zoho.writer.getMergeFields(v_TemplateID, "joels_connector");
- info r_Fields;
- //
- // map data to these fields
- m_Data = Map();
- m_Data.put("Contact.First_Name", r_ContactRecord.get("First_Name"));
- m_Data.put("Contact.Last_Name", r_ContactRecord.get("Last_Name"));
- m_Data.put("Issue.Date", zoho.currentdate.toString("MMMM, dd yyyy"));
- m_Data.put("Organization.Name", r_ContactRecord.get("Account_Name").get("name"));
- //
- m_MergedData = Map();
- m_MergedData.put("merge_data", {"data":m_Data});
- //
- // to generate and attach
- r_DownloadResponse = zoho.writer.mergeAndDownload("aaaabbbbccccddddeeeeffff000011112222333344445555", "pdf", m_MergedData, "joels_connector");
- r_AttachResponse = zoho.crm.attachFile("Contacts", p_ContactID, r_DownloadResponse);
- if(r_AttachResponse.get("code")=="SUCCESS")
- {
- v_OutputMessage = v_OutputMessage + "Generated and attached file to CRM record. ";
- }
- //
- // to generate and send via email
- m_MergedData.put("subject","Document for " + r_ContactRecord.get("Salutation") + " " + r_ContactRecord.get("Last_Name"));
- m_MergedData.put("message","Please find attached your document");
- r_SendResponse = zoho.writer.mergeAndSend("aaaabbbbccccddddeeeeffff000011112222333344445555", "pdf", r_ContactRecord.get("Email"), m_MergedData, "joels_connector");
- if(!isNull(r_SendResponse.get("records")))
- {
- v_Grammar = if(r_SendResponse.get("records").size()==1,"","s");
- v_OutputMessage = v_OutputMessage + "Generated and sent " + r_SendResponse.get("records").size() + " file" + v_Grammar + " to " + r_ContactRecord.get("Email") +". ";
- }
- //
- // return response to user
- return v_OutputMessage;
Additional
- Change Name of filecopyraw
// // to generate and attach r_DownloadResponse = zoho.writer.mergeAndDownload(v_TemplateID,"pdf",m_MergedData,"ab_writer"); v_Filename = "My Test Document"; r_DownloadResponse.setFileName(v_Filename + ".pdf"); r_AttachResponse = zoho.crm.attachFile("Quotes",p_QuoteID,r_DownloadResponse);
- //
- // to generate and attach
- r_DownloadResponse = zoho.writer.mergeAndDownload(v_TemplateID,"pdf",m_MergedData,"ab_writer");
- v_Filename = "My Test Document";
- r_DownloadResponse.setFileName(v_Filename + ".pdf");
- r_AttachResponse = zoho.crm.attachFile("Quotes",p_QuoteID,r_DownloadResponse);
- Use a subform (eg. Product Line Items) If I call my subform "Quoted Items" and specify this having the fields "Ref, Item, SKU, Description, Quantity, RRP, Discount, Total". Add the subform to your document and ensure that none of the fields have a slightly transparent red background (if they do, then add the field again and delete the one highlighted in red - alternatively give it a unique name in the record). Then map the fields using the dot syntax, add these to a list, and then submit as the subform name, like in the following example (note I have applied a regex to use US/UK numbering format with 2 decimals and thousandth separator, eg. 1,000.00):
copyraw
l_TemplateQuotedItems = List(); v_Index = 1; if(!isnull(r_LineItems.get("data"))) { l_LineItems = r_LineItems.get("data").get(0).get("Quoted_Items"); for each r_LineItem in l_LineItems { m_NewLineItem = Map(); m_NewLineItem.put("Quoted_Items.Ref", v_Index); m_NewLineItem.put("Quoted_Items.Item", r_LineItem.get("Product_Name").get("name")); m_NewLineItem.put("Quoted_Items.SKU", r_LineItem.get("Product_Name").get("Product_Code")); m_NewLineItem.put("Quoted_Items.Description", r_LineItem.get("Description")); v_Quantity = (ifnull(r_LineItem.get("Quantity"),1).toDecimal().round(2)).toString().replaceAll(("(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)"),","); m_NewLineItem.put("Quoted_Items.Quantity", v_Quantity); v_RRP = (ifnull(r_LineItem.get("List_Price"),0).toDecimal().round(2)).toString().replaceAll(("(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)"),","); m_NewLineItem.put("Quoted_Items.RRP", v_RRP); v_DiscountPercent = (ifnull(r_LineItem.get("Discount_Percent"),0).toDecimal().round(2)).toString().replaceAll(("(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)"),","); m_NewLineItem.put("Quoted_Items.Discount", v_DiscountPercent + "%"); v_LineTotal = (ifnull(r_LineItem.get("Net_Total"),0).toDecimal().round(2)).toString().replaceAll(("(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)"),","); m_NewLineItem.put("Quoted_Items.Total", v_LineTotal); l_TemplateQuotedItems.add(m_NewLineItem); v_Index = v_Index + 1; } } m_Data.put("Quoted_Items",l_TemplateQuotedItems);
- l_TemplateQuotedItems = List();
- v_Index = 1;
- if(!isnull(r_LineItems.get("data")))
- {
- l_LineItems = r_LineItems.get("data").get(0).get("Quoted_Items");
- for each r_LineItem in l_LineItems
- {
- m_NewLineItem = Map();
- m_NewLineItem.put("Quoted_Items.Ref", v_Index);
- m_NewLineItem.put("Quoted_Items.Item", r_LineItem.get("Product_Name").get("name"));
- m_NewLineItem.put("Quoted_Items.SKU", r_LineItem.get("Product_Name").get("Product_Code"));
- m_NewLineItem.put("Quoted_Items.Description", r_LineItem.get("Description"));
- v_Quantity = (ifnull(r_LineItem.get("Quantity"),1).toDecimal().round(2)).toString().replaceAll(("(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)"),",");
- m_NewLineItem.put("Quoted_Items.Quantity", v_Quantity);
- v_RRP = (ifnull(r_LineItem.get("List_Price"),0).toDecimal().round(2)).toString().replaceAll(("(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)"),",");
- m_NewLineItem.put("Quoted_Items.RRP", v_RRP);
- v_DiscountPercent = (ifnull(r_LineItem.get("Discount_Percent"),0).toDecimal().round(2)).toString().replaceAll(("(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)"),",");
- m_NewLineItem.put("Quoted_Items.Discount", v_DiscountPercent + "%");
- v_LineTotal = (ifnull(r_LineItem.get("Net_Total"),0).toDecimal().round(2)).toString().replaceAll(("(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)"),",");
- m_NewLineItem.put("Quoted_Items.Total", v_LineTotal);
- l_TemplateQuotedItems.add(m_NewLineItem);
- v_Index = v_Index + 1;
- }
- }
- m_Data.put("Quoted_Items",l_TemplateQuotedItems);
Source(s)
- Zoho Writer: Creating workflow rules in Zoho CRM with Writer's mail merge tasks
- Zoho Writer API v1 Documentation
- Zoho Deluge: Community Forum: A peek into Deluge's file functions
Category: Zoho :: Article: 781