Auto-transfer attachment files

Automatically transfer attachment files from a closed-won deal to its associated account record to ensure easy access to relevant documents for future reference.

A record of a closed-won deal may include as attachments contracts, proposals, signed agreements, and other relevant files that were part of the deal process. Some businesses insist on transferring files associated with deals to their corresponding account records to ensure easy access to all necessary information in one place without having to sift through multiple records. This practice also helps maintain a well-organized and complete record of each deal within their account. Although there's no straightforward method or ready-made automation that does this in Zoho CRM, our low-code tools make it possible.

If your business follows this practice, you can create a custom function that downloads the file from an attachment field of a deal record and uploads it to an attachment field of its account record. Once you create it, link it with a workflow rule to automatically transfer files when the attachment field in deals is updated.

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 as detailed in the "Create Connection" section below. Learn more
  • Add a File Upload field in the Deals and the Accounts module. The field will hold the files uploaded in their respective records. Learn more
  • Create a workflow rule for the Deals module that will trigger the custom function whenever a deal is created or edited. Learn more
  • Write a custom Deluge function and link it up with the workflow rule. 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.

  1. Go to Setup > Developer Space > Connections.
  2. Click Create Connection.
  3. Select Zoho OAuth under Default Services.
  4. Specify a Connection Name.
    You must enter this name in the code snippet below.
  5. Select the following Scopes to request access.
    • ZohoCRM.Files.CREATE
    • ZohoCRM.Files.READ
  6. Click Create and Connect.
  7. Click Connect and then click Accept to confirm authorization of access for the scopes requested by the client.
Create Connection

Add custom fields

The next step is to add the custom file upload fields to the Deals and Accounts modules.

  1. Navigate to Setup > Customization > Modules and Fields.
  2. Click the Deals module to open the layout editor.
  3. Drag and drop the File Upload field from the New Fields tray to the desired section of the layout.
  4. Name the file upload field (e.g. "Attachment") and define its properties as required.
  5. Once you have finished, click Save and Close.
  6. Repeat the steps above for the Accounts module to add the file upload field.

Create a workflow rule

The final step is to create a workflow rule in your CRM account.

  1. Navigate to Setup > Workflow Rules > +Create Rule.
  2. Select Deals from the module dropdown list.
  3. Provide a name for the rule. For example: "Transfer attachment files". 
  4. Add a description (optional), then click Next.
  5. In the Execute this workflow rule based on section, do the following:
    1. Choose Record Action, then select Create or Edit from the dropdown.
    2. Check the box for Repeat this workflow whenever a deal is edited.
    3. Click Next.
  6. In the Which deals would you like to apply the rule to? section, specify the condition as "<Stage> <is> <Closed Won>" and click Next.
  7. Under Instant Actions, select Function and Write your own.
  8. Provide a name and description, if necessary, for the function.
  9. On the Deluge Script Editor, do the following:
    1. Copy and paste the code given below.
    2. Click Edit Arguments.
    3. Choose Deals - Deal Id, name it dealId, and click Save.
    4. Click Save & Execute Script.
  10. Click Save on the workflow rule page.

The code

Code Copied
//Get the deal record
recordMap = zoho.crm.getRecordById("Deals",dealId);
accountId = recordMap.get("Account_Name").get("id");

//Get the attachment field
recordAttachment = recordMap.get("Attachment");
info recordAttachment;

//Get the file ID
fileId = recordAttachment.getJSON("file_Id");

//Get the attachment ID
attachmentId = recordAttachment.getJSON("attachment_Id");

//Download the file
getFile = invokeurl
[
	url :"https://www.zohoapis.com/crm/v5/files?id=" + fileId
	type :GET
	connection:"connectionname"
];
getFile.setParamName("file");

//Upload the file to ZFS
uploadFile = invokeurl
[
	url :"https://www.zohoapis.com/crm/v5/files"
	type :POST
	files:getFile
	connection:"connectionname"
];
info uploadFile;

//Get uploaded file ID
uploadFileId = uploadFile.get("data").get(0).get("details").get("id");

//Add file into list/array
fileList = List();
fileList.add(uploadFileId);

//Attach file to the file upload field in account
updateMap = Map();
updateMap.put("Attachment",fileList);
updateResponse = zoho.crm.updateRecord("Accounts",accountId,updateMap);
info updateResponse;

//Delete the file from the deal
deleteFilelist = List();
deleteMap = Map();
deleteMap.put("attachment_id",attachmentId);
deleteMap.put("_delete",null);
deleteFilelist.add(deleteMap);
deleteFile = zoho.crm.updateRecord("Deals",dealId,{"Attachment":deleteFilelist});
info deleteFile;

Notes

  • Replace "connectionname" with the name of the connection you specified while creating the connection (see the "Create a connection" section above).
  • If you prefer to copy and paste the files instead of moving them to the account record, you can remove the related code (see the "Delete the file from the deal" ) from the snippet.
  • Use the accurate API name of the custom fields in the code snippet (e.g., "Attachment"). Learn more
  • The code above is an example of transferring attachment files from a closed-won deal record to its associated account. You can use this code for any other module, such as Invoices or custom modules, by modifying the module name and parameters in the code.

Tip

  • Configure and test the function in a sandbox to ensure that further development doesn't disrupt your production environment.

Test the solution

  1. In the Deals module, click on the deal record to which you want to attach files.
    Note: Make sure the deal is associated with an account.
  2. On the Deal Details page, upload the required files to the Attachment field, set the stage to Closed Won, and click Save.
    The workflow rule will be triggered.
  3. Check whether the files attached to the deal record were added to the attachment field of the corresponding account.

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

Features used in the solution

ConnectionCustom FieldWorkflow RuleDeluge ScriptSandbox
Story Image

Looking for a custom solution?

Contact us, we will help enhance your productivity at lightning speed.

SUBMIT REQUEST

Developers: Share your solution with our community!