For Zoho Services only:


I'm actually part of something bigger at Ascent Business Solutions recognized as the top Zoho Premium Solutions Partner in the United Kingdom.

Ascent Business Solutions offer support for smaller technical fixes and projects for larger developments, such as migrating to a ZohoCRM.  A team rather than a one-man-band is always available to ensure seamless progress and address any concerns. You'll find our competitive support rates with flexible, no-expiration bundles at http://ascentbusiness.co.uk/zoho-support-2.  For larger projects, check our bespoke pricing structure and receive dedicated support from our hands-on project consultants and developers at http://ascentbusiness.co.uk/crm-solutions/zoho-crm-packages-prices.

The team I manage specializes in coding API integrations between Zoho and third-party finance/commerce suites such as Xero, Shopify, WooCommerce, and eBay; to name but a few.  Our passion lies in creating innovative solutions where others have fallen short as well as working with new businesses, new sectors, and new ideas.  Our success is measured by the growth and ROI we deliver for clients, such as transforming a garden shed hobby into a 250k monthly turnover operation or generating a +60% return in just three days after launch through online payments and a streamlined e-commerce solution, replacing a paper-based system.

If you're looking for a partner who can help you drive growth and success, we'd love to work with you.  You can reach out to us on 0121 392 8140 (UK) or info@ascentbusiness.co.uk.  You can also visit our website at http://ascentbusiness.co.uk.

Zoho CRM & Zoho Writer: Button to Merge Template, Send, and Attach

What?
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:
  1. Create a connection to Workdrive/Writer
  2. Get the Merged Template ID (hexadecimal)
  3. 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:
  1. Login to ZohoCRM > Setup > Developer Space > Connections > Zoho OAuth
  2. Give your connection a name, I'm calling mine "Joels Connector"
  3. Select the Scopes: ZohoWriter.documentEditor.ALL and ZohoWriter.Merge.ALL
  4. For good measure, I'm going to include the scopes: WorkDrive.files.ALL, WorkDrive.workspace.ALL
  5. 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
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"
    }
  ]
}
*/
  1.  r_Templates = invokeUrl 
  2.  [ 
  3.      url: "https://zohoapis.com/writer/api/v1/templates" 
  4.      type: GET 
  5.      connection: "joels_connector" 
  6.  ]
  7.  info r_Templates; 
  8.  // 
  9.  // yields something like: 
  10.  /* 
  11.  { 
  12.    "total_count": 5, 
  13.    "templates": [ 
  14.      { 
  15.        "created_time": "2019-08-28T16:30:33Z", 
  16.        "modified_time_ms": 1567009834477, 
  17.        "owner_id": "12345678", 
  18.        "last_opened_time": "2019-08-28T16:30:33Z", 
  19.        "open_url": "https://writer.zoho.com/writer/template/a1b2c3d4", 
  20.        "last_opened_time_ms": 1567009833936, 
  21.        "type": "template", 
  22.        "thumbnail_url": "https://writer.zoho.com/writer/thumbnail/a1b2c3d4e", 
  23.        "created_by": "joel", 
  24.        "created_time_ms": 1567009833936, 
  25.        "modified_time": "2019-08-28T16:30:34Z", 
  26.        "preview_url": "https://writer.zoho.com/writer/zwpreview/a1b2c3d4e5f6g", 
  27.        "creator_id": "23456789", 
  28.        "name": " New Template", 
  29.        "id": "aaaa1111bbbb2222cccc3333dddd4444eeee5", 
  30.        "permalink": "https://docs.zoho.com/file/a1b2c3d4", 
  31.        "status": "active" 
  32.      }, 
  33.      { 
  34.        "created_time": "2019-08-28T16:30:33Z", 
  35.        "modified_time_ms": 1567009834477, 
  36.        "owner_id": "34567890", 
  37.        "last_opened_time": "2019-08-28T16:30:33Z", 
  38.        "open_url": "https://writer.zoho.com/writer/template/z9y8x7w6", 
  39.        "last_opened_time_ms": 1567009831234, 
  40.        "type": "template", 
  41.        "thumbnail_url": "https://writer.zoho.com/writer/thumbnail/z9y8x7w6v", 
  42.        "created_by": "mylesser", 
  43.        "created_time_ms": 156700981111, 
  44.        "modified_time": "2019-08-28T16:30:34Z", 
  45.        "preview_url": "https://writer.zoho.com/writer/zwpreview/z9y8x7w6v5", 
  46.        "creator_id": "45678901", 
  47.        "name": " New Template", 
  48.        "id": "jjjj7777kkkk8888llll9999mmmm0000nnnn1", 
  49.        "permalink": "https://docs.zoho.com/file/a7iyybadb", 
  50.        "status": "active" 
  51.      } 
  52.    ] 
  53.  } 
  54.  */ 
You need to select the id of the template that you want to use for the next step (will be an alphanumeric long string).

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;
  1.  v_OutputMessage = ""
  2.  r_ContactRecord = zoho.crm.getRecordById("Contacts", p_ContactID)
  3.  // 
  4.  // get merge field names (for reference) 
  5.  v_TemplateID = "aaaa1111bbbb2222cccc3333dddd4444eeee5"
  6.  r_Fields = zoho.writer.getMergeFields(v_TemplateID, "joels_connector")
  7.  info r_Fields; 
  8.  // 
  9.  // map data to these fields 
  10.  m_Data = Map()
  11.  m_Data.put("Contact.First_Name", r_ContactRecord.get("First_Name"))
  12.  m_Data.put("Contact.Last_Name", r_ContactRecord.get("Last_Name"))
  13.  m_Data.put("Issue.Date", zoho.currentdate.toString("MMMM, dd yyyy"))
  14.  m_Data.put("Organization.Name", r_ContactRecord.get("Account_Name").get("name"))
  15.  // 
  16.  m_MergedData = Map()
  17.  m_MergedData.put("merge_data", {"data":m_Data})
  18.  // 
  19.  // to generate and attach 
  20.  r_DownloadResponse = zoho.writer.mergeAndDownload("aaaabbbbccccddddeeeeffff000011112222333344445555", "pdf", m_MergedData, "joels_connector")
  21.  r_AttachResponse = zoho.crm.attachFile("Contacts", p_ContactID, r_DownloadResponse)
  22.  if(r_AttachResponse.get("code")=="SUCCESS") 
  23.  { 
  24.      v_OutputMessage = v_OutputMessage + "Generated and attached file to CRM record.  "
  25.  } 
  26.  // 
  27.  // to generate and send via email 
  28.  m_MergedData.put("subject","Document for " + r_ContactRecord.get("Salutation") + " " + r_ContactRecord.get("Last_Name"))
  29.  m_MergedData.put("message","Please find attached your document")
  30.  r_SendResponse = zoho.writer.mergeAndSend("aaaabbbbccccddddeeeeffff000011112222333344445555", "pdf", r_ContactRecord.get("Email"), m_MergedData, "joels_connector")
  31.  if(!isNull(r_SendResponse.get("records")) 
  32.  { 
  33.      v_Grammar = if(r_SendResponse.get("records").size()==1,"","s")
  34.      v_OutputMessage = v_OutputMessage + "Generated and sent " + r_SendResponse.get("records").size() + " file" + v_Grammar + " to " + r_ContactRecord.get("Email") +".  "
  35.  } 
  36.  // 
  37.  // return response to user 
  38.  return v_OutputMessage; 

Additional
  • Change Name of file
    copyraw
    // 
    // 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);
    1.  // 
    2.  // to generate and attach 
    3.  r_DownloadResponse = zoho.writer.mergeAndDownload(v_TemplateID,"pdf",m_MergedData,"ab_writer")
    4.  v_Filename = "My Test Document"
    5.  r_DownloadResponse.setFileName(v_Filename + ".pdf")
    6.  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);
    1.  l_TemplateQuotedItems = List()
    2.  v_Index = 1
    3.  if(!isnull(r_LineItems.get("data"))) 
    4.  { 
    5.      l_LineItems = r_LineItems.get("data").get(0).get("Quoted_Items")
    6.      for each r_LineItem in l_LineItems 
    7.      { 
    8.          m_NewLineItem = Map()
    9.          m_NewLineItem.put("Quoted_Items.Ref", v_Index)
    10.          m_NewLineItem.put("Quoted_Items.Item", r_LineItem.get("Product_Name").get("name"))
    11.          m_NewLineItem.put("Quoted_Items.SKU", r_LineItem.get("Product_Name").get("Product_Code"))
    12.          m_NewLineItem.put("Quoted_Items.Description", r_LineItem.get("Description"))
    13.          v_Quantity = (ifnull(r_LineItem.get("Quantity"),1).toDecimal().round(2)).toString().replaceAll(("(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)"),",")
    14.          m_NewLineItem.put("Quoted_Items.Quantity", v_Quantity)
    15.          v_RRP = (ifnull(r_LineItem.get("List_Price"),0).toDecimal().round(2)).toString().replaceAll(("(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)"),",")
    16.          m_NewLineItem.put("Quoted_Items.RRP", v_RRP)
    17.          v_DiscountPercent = (ifnull(r_LineItem.get("Discount_Percent"),0).toDecimal().round(2)).toString().replaceAll(("(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)"),",")
    18.          m_NewLineItem.put("Quoted_Items.Discount", v_DiscountPercent + "%")
    19.          v_LineTotal = (ifnull(r_LineItem.get("Net_Total"),0).toDecimal().round(2)).toString().replaceAll(("(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)"),",")
    20.          m_NewLineItem.put("Quoted_Items.Total", v_LineTotal)
    21.          l_TemplateQuotedItems.add(m_NewLineItem)
    22.          v_Index = v_Index + 1
    23.      } 
    24.  } 
    25.  m_Data.put("Quoted_Items",l_TemplateQuotedItems)

Source(s)
Category: Zoho :: Article: 781

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.