Requirement Overview
A Zoho CRM user wants to automate and simplify the process of sharing deal-related documents (such as proposals, invoices, or contracts) with customers directly using Custom Function instead of sending them manually by selecting each attachment.
Use Case
A company using Zoho CRM sells electronic products to customers, and during the sales process, the sales team uploads several documents to the Deal record:
- Proposal
- Pricing Doc
- Case Studies
- Purchase Order
- Contracts
These documents are stored under the Attachments section of the Deal in Zoho CRM.
Once a deal is marked as “Closed Won”, The company needs to send all the deal-related documents to the associated contact.
Challenges in sending documents manually: The sales representative has to click on the ‘Send Mail’ button every time a deal is marked as ‘Closed Won’ and attach each related document one by one and compose an email, which results in time consumption.
Why It is Important:
- Time Saving
- Zero Errors
- Scalability
- Mitigated Risk
- Impoved Customer Experience by sharing files instantly
Permission & Availability
-> Users with the Manage Extensibility permission can create connections and write custom functions.
-> Users with the Manage Automation permission can configure Workflow Rules.
-> Users with the Manage Sandbox permission can manage the sandbox and test this use-case.
Configuration
Since sales representative wants all the documents to be sent when deal is marked as "Closed Won". We can use the "Workflow Rule" automation feature to trigger whenever a deal stage is updated to "closed won" and execute the custom function to get all attachments of the deal and send them as an email to the associated contact.
Workflow Rule Configuration:
Navigate to Setup (⚙️) in Zoho CRM >> Automation >> Workflow Rules >> Create Rule
Select "Deal" module and provide a Name to rule along with a Description.
-> When: Select Record Action >> Edit >> Specific Stage Field gets modified to value "Closed Won" >> "Repeat" enabled
-> Condition: All Deals
-> Instant Actions: Select Function >> Write own function
Create a Connection:
Navigate to Setup (⚙️) in Zoho CRM >> Developer Hub >> Connections >> My Connection >> Create Connection
Select "Zoho OAuth" as service and provide Name to "Connection". Then, select the below scopes:
- ZohoCRM.modules.ALL
- ZohoCRM.settings.ALL
- ZohoCRM.modules.attachments.all
The Code
- void automation.toSendDealDocuments(Int dealRecordID,String associatedContactEmail)
- {
- //Get contact name associated to deal - to use it in email message
- dealData = zoho.crm.getRecordById("Deals",dealRecordID);
- // info dealData;
- dealContact = ifNull(dealData.get("Contact_Name"),"");
- dealContactName = ifNull(dealContact.get("name"),"");
- //Can also add via argument directly to get name - same as contact email
- // info dealContactName;
- // info associatedContactEmail;
- //Get all attachment from deal
- relatedrcords = zoho.crm.getRelatedRecords("Attachments","Deals",dealRecordID);
- info relatedrcords;
- if(relatedrcords.size() > 0)
- {
- attachementIdList = List();
- for each ele in relatedrcords
- {
- attachementId = ele.get("id");
- //getting all attachment id
- attachementIdList.add(attachementId);
- }
- info attachementIdList.size();
- fileList = List();
- //getting each attachment from deal using attachment id and adding into list
- for each index i in attachementIdList
- {
- downloadFile = invokeurl
- [
- url :"https://www.zohoapis.com/crm/v2/Deals/" + dealRecordID + "/Attachments/" + attachementIdList.get(i)
- type :GET
- connection:"send_email_with_attachment"
- ];
- // info downloadFile;
- fileList.add(downloadFile);
- }
- //Sending email to contact with all deal-related documents
- toAddress = associatedContactEmail;
- sendmail
- [
- from :zoho.adminuserid
- to :toAddress
- subject :"Deal Documents"
- message :"Hi " + dealContactName + ", Please find all attachments related to the deal."
- Attachments :file:fileList
- ]
- }
- else
- {
- info "No attachments found for this deal.";
- }
- }
Field Argument Mapping in Deluge Setup
Code Explanation
-> Script will fetch the current executed record using record id (via passed argument - dealRecordID)
-> From the fetched record data, it will get the field (Contact_Name) value and store the name in variable.
-> Then, it will fetch all the uploaded attachments from respective deal record and store into variable. Later, checking if there is any attachment or not. If yes, then creating a list (attachementIdList) and using loop, getting attachment IDs and store them in mentioned list.
-> Now, using a Loop again and fetching all attachment using attached IDs to store into a file. Which then, later passed on to Send Mail Task.
Working Demo - Screencast
End Result - Recieved Email from Zoho CRM:
To ensure a smooth implementation, we recommend configuring and testing the setup in the sandbox environment before deploying it to production.
TIPS: Avoid Common Errors
-> Ensure to use the correct API Names for both Module & Fields in the script.
-> To ensure you get the intended output, we would suggest you to use info() logs to each variable to check the output for seamless functionality under the Console section within Zoho CRM Function IDE.
-> Since we have used the Connections within a function script, ensure to have the required scopes added in connection to perform the intended API action. Also, ensure to use the connection link name (i.e., crm_connection ) while passing on to Deluge Invoke URL or Integration Task.
-> As a common practice, we have used US DC API end point. If you are using CRM account in a different DC (i.e., IN, EU, CA, AU, etc.), then we would recommend you to use the API end point URL according to your DC.
For example:
-> Since the Automation Feature (e.g., Workflow Rules) is involved in this use-case, if the intended functionality does not work, then users can check the associated function failure reason under "Setup >> Developer Hub >> Functions >> Failures". Additionally, Users can also see the complete Log of all function execution for any specific created function to track the executions (i.e. under My Functions >> 3 Dots >> Logs). This also help in a scenario where a function is executed via Workflow rule in CRM record(shows in timeline), however it didn't perform the intended actions/updates in record. In such scenario, users can check the output (info logs) & error of execution via function logs within Zoho CRM.
Notes: Refer to the following Guide - Article to learn the best practices for Optimizing the code and various ways to deploy Custom Function across Zoho CRM.