If your business signs contracts with customers, sending them right at the closing stage of a deal is one of the most strategic moves a sales team can make. However, sending contracts to customers, waiting for signatures, and manually tracking the documents can take days in traditional sales workflows. This inefficiency indicates that you're leaving money on the table and risking lost opportunities. Salespeople must focus on selling, not on paperwork.
What if you could instantly share documents with the right people, get them e-signed using Zoho Sign, and attach them to the corresponding records, cutting turnaround time from days down to hours or even minutes? This is where a custom function or two can be helpful, particularly when combined with a workflow rule.
All you need to do is create a custom function that sends a mail merge document for e-signing via Zoho Sign. Link this function with a workflow rule that triggers when a deal is marked as closed won. Next, create a standalone function to retrieve and attach the signed documents from Zoho Sign to the corresponding deal record in your CRM. Read on to learn more.
Permissions and availability
- Users with the Manage Extensibility permission can create connections and write custom functions.
- Users with the Modules Customization permission can add custom fields.
- Users with the Manage Automation permission can create and update workflow rules.
- Users with the Manage Sandbox permission can manage the sandbox.
Requirements
- Create a Zoho OAuth connection with the required scopes for your Zoho CRM, as detailed in the "Create Connection" section below. Learn more
- Create a mail merge template for the Contacts module, including first and last names. This template will automatically be sent to the related contact for signature upon deal closure. Learn more
- Add a Single Line field in the Deals module. The field will hold the unique sign resource ID required to attach the signed document to the correct deal. Learn more
- Create a workflow rule for the Deals module that triggers every time a deal is marked as closed won. Learn more
- Write a custom Deluge function and link it up with the workflow rule. Learn more
- Write another standalone Deluge function that retrieves and attaches signed documents from Zoho Sign to the related deal record in CRM.
- Create a webhook in Zoho Sign using the API URL of the standalone function that triggers upon signing a document. Learn more
- Test the solution in a sandbox before deploying it to your production environment. Learn more
Create a connection
The first step is to create an OAuth connection in your Zoho CRM account.
- Go to Setup > Developer Hub > Connections.
- Click Create Connection.
- Select Zoho OAuth under Default Services.
- Specify a Connection Name.
You must enter this name in the code snippet below. - Select the following Scopes to request access:
- ZohoCRM.settings.mailmerge.CREATE
- ZohoWriter.documentEditor.ALL
- ZohoWriter.merge.ALL
- ZohoSign.documents.ALL
- ZohoCRM.modules.ALL
- Click Create and Connect.
- Click Connect, then click Accept to confirm authorization of access for the scope requested by the client.
Create a mail merge template
Create a mail merge template for the Contacts module.
- Navigate to Setup > Customization > Templates > Mail Merge > New Template.
You can also choose to import templates from MS Word or PDF. - On the Create New Template page, do the following:
- Select Contacts from the module dropdown list.
- Provide a name for the template. For example: "eSign contract".
You will enter this name in the code snippet below. - Select the folder where the template will be stored.
- Click Create.
A Zoho Writer document will open on a new tab. - Create a mail merge template that includes the necessary merge fields.
Note: Make sure you add a signer field and configure the signers for the document. - Click Done.
Add a custom field
Add a custom single line field to the Deals module.
- Navigate to Setup > Customization > Modules and Fields.
- Click the Deals module to open the layout editor.
- Drag and drop the Single Line field from the New Fields tray to the desired section of the layout.
- Name the single line field (e.g. "Sign Resource ID"), define its properties as required, and click Done.
- Once you have finished, click Save and Close.
Create a workflow rule
Write and connect a custom function with a workflow rule.
- Navigate to Setup > Workflow Rules > +Create Rule.
- Select the Module as Deals from the drop-down list.
- Provide a name for the rule. For example: "eSign documents and attach to deals".
- Add a description, if required, then click Next.
- In the Execute this workflow rule based on section, do the following:
- Choose Record Action, then select Edit from the drop-down.
- Choose Specific field(s) gets modified from the dropdown, then enter the condition "When <Stage> is modified to <the value> <Closed Won>".
- Click Next.
- In the Which deals would you like to apply the rule to? section, select All Deals, then click Next. You can also select <Stage> is modified to <any WON stages> if multiple stages indicate the won stage.
- Under Instant Actions, select Function and Write your own.
- Provide a Name for the function, as well as a description, if necessary.
- On the Deluge Script Editor, do the following:
- Copy and paste the code given below.
- Click Edit Arguments.
- Choose Deals - Deal Id, name it dealid, and click Save.
- Click Save & Execute script.
- Click Save on the workflow rule page.
The code
deals = zoho.crm.getRecordById("Deals",dealid);
contact = ifnull(deals.get("Contact_Name"),"").get("id");
contactdetails = zoho.crm.getRecordById("Contacts",contact);
email = ifnull(contactdetails.get("Email"),"");
contactname = contactdetails.get("Full_Name");
id = contact;
module = "Contacts";
url = "https://www.zohoapis.com/crm/v7/" + module + "/" + id + "/actions/sign_mail_merge"; signerdata = List();
signmap1 = Map();
signmap1.put("recipient",{"type":"email","value":email}); signmap1.put("recipient_name",contactname);
signmap1.put("action_type","sign");
signerdata.add(signmap1);
form = Map();
form.put("file_name","Document to Sign");
form.put("signers",signerdata);
form.put("mail_merge_template",{"name":"templatename"});
ls = list();
ls.add(form);
mp = Map();
mp.put("sign_mail_merge",ls);
documentObj = invokeurl
[
url :url
type :POST
parameters:mp + "" connection:"connectioname"
];
info documentObj;
signmail = documentObj.get("sign_mail_merge");
info signmail;
details = signmail.getJSON("details").getJSON("sign_resource_id");
info details;
update = zoho.crm.updateRecord("Deals",dealid,{"Sign_Resource_ID":details});
info update;
Notes
- Make sure to use the accurate API names for their corresponding fields (e.g., "Sign Resource ID") in the code snippet. Learn more
- Replace "templatename" with the name of the mail merge template you specified when creating it for contacts (see the "Create a mail merge template" section above).
- Replace "connectionname" with the name of the connection you specified when creating it (see the "Create a connection" section above).
Write a standalone function
Write a standalone custom Deluge function that retrieves and attaches signed documents from Zoho Sign to the related deal in CRM.
- Navigate to Setup > Developer Space > Functions > +New Function.
- Provide a name and display name for the function. For example: "Attach signed document from Sign to Deal".
- Select the category of the function as Standalone.
- Copy and paste the code provided below.
- Click Edit Arguments.
- Enter the parameter name as crmAPIRequest, select the type as string, and click Save.
- Click Save on the Deluge Script Editor.
- Search the function, hover over it, click the ellipsis next to its name, and select REST API.
- Toggle ON the API Key option and copy the generated URL.
- Paste the URL into a notepad for use in Zoho Sign, then click the Save button.
The code
crmAPIRequestMap = crmAPIRequest.toMap();
request_body = crmAPIRequestMap.get("body");
info request_body;
requestMap = request_body.toMap();
requestStr = requestMap.get("requests");
requestObj = requestStr.toMap();
notificationObj = requestMap.get("notifications").toMap();
response = Map();
response.put("body",{"what we got":requestObj});
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");
dealRecords = zoho.crm.searchRecords("Deals","(Sign_Resource_ID:equals:" + requestId + ")");
if(dealRecords.size() > 0)
{
info "Deal record found";
dealId = dealRecords.get(0).get("id");
for each document in document_ids {
document_id = document.get("document_id");
fileResponse = invokeurl
[
url :"https://sign.zoho.com/api/v1/requests/" + requestId + "/documents/" + document_id + "/pdf"
type :GET
connection:"connectionname" ];
docName = fileResponse.getFileName();
fileResponse.setFileName(docName);
dealsattach = zoho.crm.attachFile("Deals",dealId,fileResponse);
info dealsattach;
}
}
else
{
info "Error";
response.put("body",{"error":"No matching deal found for this requests key"});
}
return {"crmAPIResponse":response};
Notes
- Make sure to use the accurate API names for their corresponding fields (e.g., "Sign Resource ID") in the code snippet. Learn more
- Replace "connectionname" with the name of the connection you specified when creating it (see the "Create a connection" section above).
Create a webhook
The final step is to create and enable a Zoho Sign webhook, which gets triggered upon signing documents.
- Log in to Zoho Sign as an Administrator.
- Navigate to Settings > Developer Settings > Webhooks from the left navigation pane.
- Click Create webhook.
- On the Create webhook page, do the following:
- Paste the API Key URL from the standalone function into the Callback URL field.
- Provide a name for the webhook.
- Choose document signing as the callback event.
- Click Save to activate the webhook.
Notes
- Webhooks are available only in the Enterprise, API, and Zoho One plans.
- Each account is limited to two webhooks.
Tip
- Configure and test the function in a sandbox to ensure that further development doesn't disrupt your production environment.
Test the solution
- Navigate to the Deals module.
- Open a deal record to view its details.
- On the Deal Details page, set its stage to Closed Won and click Save.
- This will trigger the workflow rule, sending a signing link to the contact's email associated with the deal.
- Open the signing link in the email to complete the document signing.
- Check whether the signed document was uploaded to the Attachments tab of the deal record.
Did you find this useful? Try it out and let us know how it works. Share this with your team if they'd benefit from it! If you have questions, please don't hesitate to contact us.
More info
- ModuleDeals
- Trigger PointWorkflow Rule
- EditionEnterprise and above
- ComplexityHigh
- Implementation Time60 minutes

Looking for a custom solution?
Contact us, we will help enhance your productivity at lightning speed.
SUBMIT REQUEST