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?
- 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.
- You need to create a custom callback function, which can be done by following these steps.
Paste the following code:
- crmAPIRequestMap = crmAPIRequest.toMap();
- 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") && requestMap.containKey("notifications"))
- {
- notificationMap = requestMap.get("notifications").toMap();
- requestStr = requestMap.get("requests");
- requestObj = requestStr.toMap();
- response.put("body",{"what we got":requestObj,"notification":notificationMap});
- if(requestObj.containKey("request_status") && notificationMap.containKey("operation_type"))
- {
- operationType = notificationMap.get("operation_type");
- //if(requestObj.get("request_status").equals("completed"))
- if(operationType.equals("RequestCompleted") || operationType.equals("RequestSigningSuccess") || operationType.equals("RequestRecalled") || operationType.equals("RequestRejected") || operationType.equals("RequestExpired"))
- {
- requestStatus = "Out for Signature";
- if(operationType.equals("RequestCompleted"))
- {
- requestStatus = "Signed";
- }
- else if(operationType.equals("RequestRecalled"))
- {
- requestStatus = "Recalled";
- }
- else if(operationType.equals("RequestRejected"))
- {
- requestStatus = "Declined";
- }
- else if(operationType.equals("RequestExpired"))
- {
- requestStatus = "Expired";
- }
- requestId = requestObj.get("request_id");
- //Check if already exists
- zsignrecord = zoho.crm.searchRecords("zohosign__ZohoSign_Documents","(zohosign__ZohoSign_Document_ID:equals:" + requestId + ")");
- if(zsignrecord.size() > 0 && zsignrecord.get(0).containKey("zohosign__DeleteEdit_Preview_or_Position_Signature_Fields"))
- {
- info "Record already present";
- response.put("body",{"message":"Record already present, updating"});
- if(zsignrecord.get(0).get("zohosign__DeleteEdit_Preview_or_Position_Signature_Fields") == false)
- {
- //Needs to be updated
- zsrecordId = zsignrecord.get(0).get("id");
- recordInfo = {"zohosign__Document_Status":requestStatus};
- resp = zoho.crm.createRecord("zohosign__ZohoSign_Documents",zsrecordId,recordInfo);
- if(resp.containKey("id"))
- {
- response.put("body",{"message":"Updated record"});
- }
- }
- else
- {
- return {"crmAPIResponse":response};
- }
- }
- else
- {
- response.put("body",{"request_id":requestId});
- associatedModule = "Contacts";
- associatedModuleKey = "zohosign__Contact";
- actions = requestObj.get("actions");
- email = actions.get(0).get("recipient_email");
- records = zoho.crm.searchRecords("Contacts","(Email:equals:" + email + ")");
- if(records.size() <= 0)
- {
- records = zoho.crm.searchRecords("Leads","(Email:equals:" + email + ")");
- if(records.size() > 0)
- {
- associatedModule = "Leads";
- associatedModuleKey = "zohosign__Lead";
- }
- }
- if(records.size() > 0)
- {
- info "Record found";
- leadid = records.get(0).get("id");
- 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};
- //Create ZohoSign Record in CRM
- resp = zoho.crm.createRecord("zohosign__ZohoSign_Documents",recordInfo);
- if(resp.containKey("id"))
- {
- zsDocId = resp.get("id");
- respDoc = zoho.sign.downloadDocument(requestId);
- zoho.crm.attachFile("zohosign__ZohoSign_Documents",zsDocId,respDoc);
- response.put("body",{"attached to record":zsDocId});
- //Create zoho sign recipients record for all recipients
- for each action in actions
- {
- recpStatus = "Waiting for Signature";
- if(action.get("action_status").equals("SIGNED"))
- {
- recpStatus = "Signed";
- }
- else if(action.get("action_status").equals("DECLINED"))
- {
- recpStatus = "Cancelled";
- }
- 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};
- zoho.crm.createRecord("zohosign__ZohoSign_Recipients",recpRecordInfo);
- }
- }
- else
- {
- response.put("body",{"error":resp});
- }
- }
- }
- }
- else
- {
- response.put("body",{"not completed":requestObj});
- }
- }
- else
- {
- response.put("body",{"no request status":requestObj});
- }
- }
- else
- {
- info "Error";
- response.put("body",{"error":"No requests key"});
- }
- return {"crmAPIResponse":response}