[Zoho CRM] Send Mail Merge Document from Zoho CRM

[Zoho CRM] Send Mail Merge Document from Zoho CRM

Please map "recId" to the respective Id field of the module you want. For example, if you're doing for the Contacts module, map it to "Contact Id" field in the Edit Arguments in the custom function builder.
  1. //get info
  2. recDet = zoho.crm.getRecordById("Contacts",recId);
  3. firstName = recDet.get("First_Name");
  4. email = recDet.get("Email");
  5. totalDemerits = ifnull(recDet.get("Total_Demerits"),"");
  6. info totalDemerits;
  7. //determine which to use. I have conditions, whereby based on the demerit value, I will be using different template. This is just an example
  8. if(totalDemerits >= 10 && totalDemerits < 20)
  9. {
  10. templateID = "c4w7309a4fa9070534c09963c7d8a29fc5c86";
  11. }
  12. else if(totalDemerits >= 20 && totalDemerits < 30)
  13. {
  14. templateID = "3hojn7181a237794b4d0bb2d63d23013cc3f9";
  15. }
  16. else if(totalDemerits >= 30)
  17. {
  18. templateID = "3hojn814e1d41a94d4cd0952b7a0cb25e4229";
  19. }
  20. info templateID;
  21. fields = zoho.writer.getMergeFields(templateID,"zwriter");
  22. info fields;
  23. emailIDs = List();
  24. emailIDs.add(email);
  25. data = Map();
  26. data.put("id",recId);
  27. data.put("Total_Demerits",totalDemerits);
  28. data.put("Student.Name",firstName);
  29. data.put("Name",firstName);
  30. emailSubject = "Warning Letter For " + firstName;
  31. mergedata = Map();
  32. mergedata.put("merge_data",{"data":data});
  33. mergedata.put("subject",emailSubject);
  34. mergedata.put("output_format","pdf");
  35. mergedata.put("from_email","putyourfromemailhere@aplikasi.com");
  36. //make sure the from address that you put here can be used by you/admin to send out email. It cannot be a random address.
  37. mergedata.put("from_name","Put Your Sender Name Here");
  38. mergedata.put("recipient_email",emailIDs);
  39. mergedata.put("attachment_name","Warning Letter - " + firstName);
  40. mergeURL = "https://zohoapis.com/writer/api/v1/documents/" + templateID + "/merge/email";
  41. resp = invokeurl
  42. [
  43. url :mergeURL
  44. type :POST
  45. parameters:mergedata
  46. connection:"zwriter"
  47. ];
  48. info resp;
Legends:
Template ID you could get when you open the merge mail template in CRM
How to get the API name of the fields in your selected mail merge template and to use them in the data mapping.
Your sender details. Please make sure you are authorized to use this email address

The connection "zwriter" has to be created first. It has to have the relevant parameters to Zoho Writer. When creating the connection, I chose Zoho OAuth service and find parameters that are relevant to Zoho Writer.

Good luck