Some businesses require prospective customers to sign agreements, contracts, disclaimer documents, or other relevant files before closing a deal. These documents are typically sent to customers as email attachments. If your business follows this practice, you want to automate this process to ensure everything runs smoothly. You can accomplish this by writing a custom function in your CRM.
All you need to do is add a blueprint transition that mandates your salespeople to attach a PDF file when they reach a specific deal stage. After that, write a custom function and link it as part of the custom action in the transition. Keep reading to find out more.
Permissions and availability
- Users with Manage Extensibility permission can create connections and write custom functions.
- Users with Manage Templates permission can create and update email templates.
- Users with Manage Automation permission can create and update Blueprint.
- Users with Manage Sandbox permission can manage the sandbox.
Requirements
- Create a Zoho OAuth connection with the required scopes for your Zoho CRM account, as detailed in the "Create Connection" section below. Learn more
- Create a custom email template for the Deals module to send emails containing attachments. Learn more
- Create a blueprint for the Deals module that sends out emails upon completing the transition. Learn more
- Write a custom Deluge function that downloads the file from the deal record and attaches it to the email. Learn more
- Test the workflow rule 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.
- Navigate to Setup > Developer Space > Connections.
- Click Create Connection.
- Select Zoho OAuth under Default Services.
- Specify a Connection Name.
You will need to enter this name in the code snippet below. - Select the following Scopes to request access.
- ZohoCRM.modules.ALL
- ZohoCRM.settings.ALL
- ZohoCRM.Files.CREATE
- ZohoCRM.send_mail.all.CREATE
- ZohoCRM.templates.email.READ
- ZohoCRM.modules.attachments.all Learn more
- Click Create and Connect.
- Click Connect, then click Accept to confirm authorization of access for the scopes requested by the client.
Create an email template
The next step is to add the custom email template that will be sent to your prospective customers.
- Navigate to Setup > Customization > Templates > Email > +New Template.
- Select Deals from the dropdown list, then click Next.
- Choose Blank in the Template Gallery to create a template from scratch, or select a pre-built template and customize it using the drag-and-drop editor.
- Once finished, click Save Now and copy the template's ID from the address bar, which you must enter in the code snippet below.
Create a Blueprint
The final step is to create a blueprint and connect it with a custom function.
- Navigate to Setup > Blueprint > +Create Blueprint.
- Provide a name for the blueprint. For example: "Send email with an attachment".
- Select the module as Deals from the dropdown list.
- Choose the layout and field (e.g. stage) for the process.
- Add a description, if required, then click Next.
- In the Blueprint Editor, drag and drop the States (stages) and connect the nodes between them.
- Click on the + button between two states and create a transition.
- Select the After tab on the transition and then choose +Custom Actions.
- Select New Function and then select Write your own.
- Provide a name for the function, as well as a description, if necessary.
- In the Deluge Script Editor, do the following:
- Copy and paste the code provided below.
- Click Edit Arguments.
- Choose Deals - Deal Id, then name it dealId.
- Click the + icon to add more arguments.
- Choose Contacts - Email, then name it email.
- Click Save & Execute Script.
- Click Publish on the blueprint page.
The code
//Get module attachments
alist = List();
attachments = zoho.crm.getRelatedRecords("Attachments","Deals",dealId);
for each record in attachments
{
file_id = record.get("id");
downloadFile = invokeurl
[
url :"https://www.zohoapis.com/crm/v5/Deals/" + dealId + "/Attachments/" + file_id
type :GET
connection:"connectioname"
];
downloadFile.setParamName("file");
response = invokeurl
[
url :"https://www.zohoapis.com/crm/v5/files"
type :POST
files:downloadFile
connection:"connectioname"
];
info response;
fid = response.get("data").get(0).get("details").get("id");
attachmap = Map();
attachmap.put("id",fid);
alist.add(attachmap);
}
sendto = Map();
sendto.put("email",email);
tolist = List();
tolist.add(sendto);
sendfrom = Map();
sendfrom.put("email","fromemail");
tempmp = Map();
tempmp.put("id","xxxxx");
mp = Map();
mp.put("from",sendfrom);
mp.put("to",tolist);
mp.put("template",tempmp);
mp.put("attachments",alist);
mp.put("org_email",true);
dlist = List();
dlist.add(mp);
data = Map();
data.put("data",dlist);
info data;
sendmail = invokeurl
[
url :"https://www.zohoapis.com/crm/v5/Deals/" + dealId + "/actions/send_mail"
type :POST
parameters:data + ""
connection:"connectioname"
];
info sendmail;
Notes
- Replace "connectionname" with the name of the connection you specified when creating it (see the "Create a connection" section above).
- Replace "xxxxx" with the ID of the email template. (see the "Create an email template" section above).
- As the API URLs differ for each data center, you should identify your account's data center and use the corresponding domain in the fileUrl. For example, for India, use https://www.zohoapis.in/crm.
- Replace "fromemail" with the organization email address added to your CRM. Learn more
- The function will send emails to the contact email address associated with the deal.
- The code above is an example of sending emails containing attachments through a blueprint transition. You can use this code for any other module, such as quotes, sales orders, and purchase orders, by modifying the module name and other parameters.
Tips
- Configure and test the function in a sandbox to ensure that further development doesn't disrupt your production environment.
- You can use the super admin's email address to send the emails. In this scenario, please ensure the following:
- Replace "fromemail" with "zoho.adminuserid" in the code snippet.
- Delete the code at line #40, as you won't use the organization's email address.
- The super admin needs to create the connection used in the function.
Test the solution
- Click the Deals tab.
- Open a deal record containing the transition that sends an email with the attachment.
Note: The deal must have files attached to the Attachments tab in its Related List. - Complete the blueprint transition.
- Check whether the email was sent to the intended recipient as specified in the function.
Limitation
- If the total size of the attachments exceeds 10MB, the function will not send the email after completing the transition.
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 PointBlueprint
- 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