How send a ticket attachment using the Sendreply API in Zoho Desk

How send a ticket attachment using the Sendreply API in Zoho Desk

API document references : you make use of the Upload file API and gather the attachment ID. This ID is be passed with the Send email Reply API to deliver responses with the attachment intact.

Code template is as below:
  1. // ORGID
  2. ORGID = "XXXXXXX"; // Masked Org ID
  3. ApiUrl = "https://desk.zoho.com/api/v1";

  4. // Get Contact data
  5. getData = zoho.desk.getRecordById(ORGID, "tickets", TicketID);
  6. ContactID = getData.get("contactId");
  7. getContact = zoho.desk.getRecordById(ORGID, "contacts", ContactID);

  8. // update ticket with contact's phone number
  9. updateTicket = zoho.desk.update(ORGID, "tickets", TicketID, {"phone": getContact.getJSON("phone")});

  10. // Get thread attachments
  11. getthreadattachment = zoho.desk.getRelatedRecordById(ORGID, "threads", "XXXXXXX", "tickets", TicketID).getJSON("attachments");
  12. attachss = List();

  13. if(getthreadattachment != null && getthreadattachment.size() > 0)
  14. {
  15.   for each attach in getthreadattachment
  16.   {
  17.     attachid = attach.get("id");
  18.     fileName = attach.get("name");

  19.     if(isText(attach))
  20.     {
  21.       attach = attach.toFile(fileName);
  22.     }
  23.     attach = attach.toFile(fileName);
  24.     attach.setParamName("file");
  25.     
  26.     param = Map();
  27.     param.put(fileName, attach);
  28.     
  29.     uploadattachments = invokeurl
  30.     [
  31.       url : ApiUrl + "/uploads"
  32.       type : POST
  33.       parameters: param
  34.       headers: { "Content-Type": "multipart/form-data" }
  35.       connection: "XXXX" // Masked connection type
  36.     ];
  37.     
  38.     newattachid = uploadattachments.get("id");
  39.     attachss.add(newattachid);
  40.     info attachss;
  41.   }
  42. }

  43. // Prepare to render email template
  44. templateId = "XXXXXXX"; // Masked template ID
  45. Render = invokeurl
  46. [
  47.   url : ApiUrl + "/templates/" + templateId + "/render?entityId=" + TicketID
  48.   type : POST
  49.   connection: "XXXX" // Masked connection type
  50. ];
  51. contentEmail = Render.getJSON("body");
  52. Mappy = Map();
  53. Mappy.put("to", "masked_email@example.com"); // Masked recipient email
  54. Mappy.put("content", contentEmail);
  55. Mappy.put("contentType", "html");
  56. Mappy.put("fromEmailAddress", "masked_sender@example.com"); // Masked sender email
  57. Mappy.put("channel", "EMAIL");
  58. Mappy.put("isForward", "false");
  59. Mappy.put("attachmentIds", attachss);// input the attachmentid

  60. sendResponsess = invokeurl
  61. [
  62.   url : ApiUrl + "/tickets/" + TicketID + "/sendReply"
  63.   type : POST
  64.   parameters: Mappy + ""
  65.   connection: "XXXX" // Masked connection type
  66. ];

  67. info sendResponsess;

Explanation:

  1. Fetching Ticket Details: The script retrieves ticket and contact information to update the ticket’s phone number.
  2. Uploading Attachments: It gathers any attachments from the ticket's threads and uploads them to the Zoho Desk system.
  3. Sending Emails: Finally, it prepares an email using a specified template and includes any uploaded attachments before sending.

Feel free to adapt this code for your own ticket handling processes in Zoho Desk! Just remember to replace the masked values with your actual data when implementing.


Happy coding!