How to set up a callback function in CRM to add Zoho Sign document details that is triggered by the Zoho Sign webhook?
Scenario: Within a Custom module in ZohoCRM, I have set up an automation to send documents to Zoho Sign. Each time I want to view the documents, I need to view them at Zoho Sign. Can I set up a callback function in CRM to add Zoho Sign document details that is triggered by the Zoho Sign webhook?
- Custom Modules allow the user to send customized attachments (completion certificate, signed copy of the document) as per the user's requirement.
- When you have sent documents to sign using Automation/Workflows. These docs will only be available in Zoho Sign but will not be updated back in crm. Once you have setup this code, this will help you to create/update Zoho Sign docs entry in CRM.
- You can view the set up function by clicking here.
Paste the code that is given below.
Note: In this example code, the Custom Module name is "Commercial_Terms". Use the replaced module name when using the code.
- crmAPIRequestMap = crmAPIRequest.toMap();
- //crmAPIRequest is the parameter passed to this function
- request_body = crmAPIRequestMap.get("body");
- info request_body;
- requestMap = request_body.toMap();
- response = Map();
- response.put("status_code",200);
- response.put("Content-Type","application/json");
- hasRequests = requestMap.containKey("requests");
- if(requestMap.containKey("requests"))
- {
- requestStr = requestMap.get("requests");
- requestObj = requestStr.toMap();
- notificationObj = requestMap.get("notifications").toMap();
- response.put("body",{"what we got":requestObj});
- if(requestObj.containKey("request_status"))
- {
- reqStatus = requestObj.get("request_status");
- requestId = requestObj.get("request_id");
- response.put("body",{"request_id":requestId});
- info "Record found";
- document_ids = requestObj.get("document_ids");
- records = zoho.crm.searchRecords("Commercial_Terms","(Sign_Request_Id:equals:" + requestId + ")");
- if(records.size() > 0 && reqStatus.equals("completed") && notificationObj.get("operation_type").equals("RequestCompleted"))
- {
- info "Record found";
- leadid = records.get(0).get("id");
- for each document in document_ids
- {
- document_id = document.get("document_id");
- //Signed Document Download
- fileResponse = invokeurl
- [
- url :"https://sign.zoho.com/api/v1/requests/" + requestId + "/documents/" + document_id + "/pdf"
- type :GET
- connection:"Zoho Sign"
- ];
- docName = fileResponse.getFileName();
- fileResponse.setFileName(docName);
- zoho.crm.attachFile("Commercial_Terms",leadid,fileResponse);
- }
- //Completion Certificate Download
- fileResponse2 = invokeurl
- [
- url :"https://sign.zoho.com/api/v1/requests/" + requestId + "/completioncertificate"
- type :GET
- connection:"Zoho Sign"
- ];
- // Attaching the documents to the Custom Module
- zoho.crm.attachFile("Commercial_Terms",leadid,fileResponse2);
- response.put("body",{"attached to record":leadid});
- }
- }
- else
- {
- response.put("body",{"no request status":requestObj});
- }
- }
- else
- {
- info "Error";
- response.put("body",{"error":"No requests key"});
- }
- return {"crmAPIResponse":response};