Print

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
Source(s)
Category: Zoho :: Article: 781