How do I set up a callback function in CRM to add Zoho Sign document details?

How do I set up a callback function in CRM to add Zoho Sign document details?

Scenario: I have set up an automation in ZohoCRM to send documents to Zoho Sign. Each time I want to view the documents, I need to view them at Zoho Sign. It is difficult to view the document status this way. Can I set up a callback function in CRM to add Zoho Sign document details?

  1. When documents are sent for signature from CRM using Zoho Sign's extension, the documents will be updated in CRM. However, in other instances such as automation and workflow, this will not be done.
  2. You need to create a custom callback function, which can be done by following these steps. 
Paste the following code:

  1. crmAPIRequestMap = crmAPIRequest.toMap();
  2. request_body = crmAPIRequestMap.get("body");
  3. info request_body;
  4. requestMap = request_body.toMap();
  5. response = Map();
  6. response.put("status_code",200);
  7. response.put("Content-Type","application/json");
  8. hasRequests = requestMap.containKey("requests");
  9. if(requestMap.containKey("requests") && requestMap.containKey("notifications"))
  10. {
  11. notificationMap = requestMap.get("notifications").toMap();
  12. requestStr = requestMap.get("requests");
  13. requestObj = requestStr.toMap();
  14. response.put("body",{"what we got":requestObj,"notification":notificationMap});
  15. if(requestObj.containKey("request_status") && notificationMap.containKey("operation_type"))
  16. {
  17. operationType = notificationMap.get("operation_type");
  18. //if(requestObj.get("request_status").equals("completed"))
  19. if(operationType.equals("RequestCompleted") || operationType.equals("RequestSigningSuccess") || operationType.equals("RequestRecalled") || operationType.equals("RequestRejected") || operationType.equals("RequestExpired"))
  20. {
  21. requestStatus = "Out for Signature";
  22. if(operationType.equals("RequestCompleted"))
  23. {
  24. requestStatus = "Signed";
  25. }
  26. else if(operationType.equals("RequestRecalled"))
  27. {
  28. requestStatus = "Recalled";
  29. }
  30. else if(operationType.equals("RequestRejected"))
  31. {
  32. requestStatus = "Declined";
  33. }
  34. else if(operationType.equals("RequestExpired"))
  35. {
  36. requestStatus = "Expired";
  37. }
  38. requestId = requestObj.get("request_id");
  39. //Check if already exists
  40. zsignrecord = zoho.crm.searchRecords("zohosign__ZohoSign_Documents","(zohosign__ZohoSign_Document_ID:equals:" + requestId + ")");
  41. if(zsignrecord.size() > 0 && zsignrecord.get(0).containKey("zohosign__DeleteEdit_Preview_or_Position_Signature_Fields"))
  42. {
  43. info "Record already present";
  44. response.put("body",{"message":"Record already present, updating"});
  45. if(zsignrecord.get(0).get("zohosign__DeleteEdit_Preview_or_Position_Signature_Fields") == false)
  46. {
  47. //Needs to be updated
  48. zsrecordId = zsignrecord.get(0).get("id");
  49. recordInfo = {"zohosign__Document_Status":requestStatus};
  50. resp = zoho.crm.createRecord("zohosign__ZohoSign_Documents",zsrecordId,recordInfo);
  51. if(resp.containKey("id"))
  52. {
  53. response.put("body",{"message":"Updated record"});
  54. }
  55. }
  56. else
  57. {
  58. return {"crmAPIResponse":response};
  59. }
  60. }
  61. else
  62. {
  63. response.put("body",{"request_id":requestId});
  64. associatedModule = "Contacts";
  65. associatedModuleKey = "zohosign__Contact";
  66. actions = requestObj.get("actions");
  67. email = actions.get(0).get("recipient_email");
  68. records = zoho.crm.searchRecords("Contacts","(Email:equals:" + email + ")");
  69. if(records.size() <= 0)
  70. {
  71. records = zoho.crm.searchRecords("Leads","(Email:equals:" + email + ")");
  72. if(records.size() > 0)
  73. {
  74. associatedModule = "Leads";
  75. associatedModuleKey = "zohosign__Lead";
  76. }
  77. }
  78. if(records.size() > 0)
  79. {
  80. info "Record found";
  81. leadid = records.get(0).get("id");
  82. recordInfo = {"Name":requestObj.get("request_name"),associatedModuleKey:leadid,"zohosign__Document_Status":requestStatus,"zohosign__ZohoSign_Document_ID":requestId,"zohosign__Date_Completed":today,"zohosign__Module_Name":associatedModule,"zohosign__Module_Record_ID":leadid};
  83. //Create ZohoSign Record in CRM
  84. resp = zoho.crm.createRecord("zohosign__ZohoSign_Documents",recordInfo);
  85. if(resp.containKey("id"))
  86. {
  87. zsDocId = resp.get("id");
  88. respDoc = zoho.sign.downloadDocument(requestId);
  89. zoho.crm.attachFile("zohosign__ZohoSign_Documents",zsDocId,respDoc);
  90. response.put("body",{"attached to record":zsDocId});
  91. //Create zoho sign recipients record for all recipients
  92. for each  action in actions
  93. {
  94. recpStatus = "Waiting for Signature";
  95. if(action.get("action_status").equals("SIGNED"))
  96. {
  97. recpStatus = "Signed";
  98. }
  99. else if(action.get("action_status").equals("DECLINED"))
  100. {
  101. recpStatus = "Cancelled";
  102. }
  103. recpRecordInfo = {"Name":action.get("recipient_name"),"zohosign__Email":action.get("recipient_email"),"zohosign__ZohoSign_Document":zsDocId,"zohosign__Recipient_Order":action.get("signing_order"),"zohosign__Recipient_Type":action.get("action_type"),"zohosign__Recipient_Status":recpStatus};
  104. zoho.crm.createRecord("zohosign__ZohoSign_Recipients",recpRecordInfo);
  105. }
  106. }
  107. else
  108. {
  109. response.put("body",{"error":resp});
  110. }
  111. }
  112. }
  113. }
  114. else
  115. {
  116. response.put("body",{"not completed":requestObj});
  117. }
  118. }
  119. else
  120. {
  121. response.put("body",{"no request status":requestObj});
  122. }
  123. }
  124. else
  125. {
  126. info "Error";
  127. response.put("body",{"error":"No requests key"});
  128. }
  129. return {"crmAPIResponse":response}