Hi Everyone,
I'm hoping this one will be really helpful for a lot of people. One thing that's missing from Zoho is a tighter integration between Workdrive and CRM. Whilst an update is in the works as far as I'm aware, file management can still be a needlessly complicated problem. And while there are apps out there that help with this, they often don't work with custom modules, or in the way you want.
Wouldn't it be great if you could just drag and drop your files into a workdrive folder and they all get renamed and appear on your CRM record without any effort from you? In this post, I will show you how to automatically create folders in workdrive AND LINK THEM TO THE CRM RECORD so that when a new file is added to the folder, it's automatically attached to the CRM record.
This is the basic process:
- CRM record is created
- Workdrive folder is created
- A Data Bind is created between the CRM Record and the Workdrive Folder
- When a new file is added to the Workdrive Folder, a function is triggered that attaches the file to the CRm Record.
When the file is attached to the CRM Record, we can take this opportunity to do whatever we want to aid in file management. In one of our organisations we accomplish the following in one of our business processes:
If the new file is a csv, the data is parsed and added to a subform on the recocrd
If the new file is a PDF, it is renamed with a specific format
If the new file contains a specific string of text, we know it's a thumbnail image, so it's uploaded to the record image
...
You get the idea of what we can do, so let's get into it.
Step 1: Create a CRM Function to receive the Data
First we need to create a function in CRM to receive a webhook, essentially a packed of data sent from Workdrive to CRM.
Head over to your functions in CRM and create a new Standalone function. Give it a name and description if you want.
We're going to create an argument called crmAPIRequest (spelled exactly like that, this is case sensitive) and se tthe argument type as string
Now save that and save the function, we don't need anything in there yet. In your function menu, click on the elipses (the 3 dots) and select REST API
Turn on "API Key" and the system will generate a url for you. Copy and save that URL as we will need it for Step 2. This finishes Step 1.
Step 2: Create an app in Workdrive
Firstly, we need to create an "App" in Workdrive. This will monitor your folder for specific events and send a webhook when that event is triggered, i.e new file created.
In Workdrive, head to the Admin Console --> Apps --> + New Custom App
Give your app a name that's descriptive and informative and paste your client ID. (There's insructions in the opo-up window about how to get your client ID)
When you have created your Custom App, go into it and head over to "Webhooks" and click "+ Create New Webhook"
Give your webhook a name and description if you please. The endpoint URL will be the url of your function that you created in step 1. This tells the app where to send the packed of data.
Next we select the event that will trigger the webhook. In this case, we want it to trigger when a file is uploaded.
Finally we want to specify the folder to monitor. This will be the parent folder that all your new folders will sit under. When you're done, click "Create Webhook"
Step 3: Setup the Data bind
A data bind links 2 records together with their unique ids. To do this we need a field on your CRM Record to store the Wokdrive Folder ID. Make this a single line field. If you don't know how to do this, then this guide is not for you.
So that stores the Workdrive folder ID, but what stores the CRM record ID? The answer is a Data Template. In you Workdrive Admin Console, head over to Data Templates and create a data template. Add a single line text field to your data template. This will store the CRM Record ID
Step 4: Create folders in Workdrive with Deluge function.
Now that we have set this up, we need to create folders and associate store the respective IDs. This will be done on record creation.
Use the following code snipped to create your folder
- // Create folder
- header = Map();
- header.put("Accept","application/vnd.api+json");
- data = Map();
- data_param1 = Map();
- att_param1 = Map();
- att_param1.put("name",<Folder Name>);
- att_param1.put("parent_id",<Parent Folder ID>);
- data_param1.put("attributes",att_param1);
- data_param1.put("type","files");
- data.put("data",data_param1);
- createFolder = invokeurl
- [
- url :"https://www.zohoapis.com.au/workdrive/api/v1/files"
- type :POST
- parameters:toString(data)
- headers:header
- connection: <Your Workdrive Connection>
- ];
- info createFolder;
- // m_record.put("Folder_Id",createFolder.get("data").get("id")); - Put the created folder ID into the map for your CRM record
- // Associate Data Template
- header = Map();
- header.put("Accept","application/vnd.api+json");
- data = Map();
- data_param1 = Map();
- att_param1 = Map();
- att_param1.put("resource_id",createFolder.get("data").get("id"));
- att_param1.put("data_template_id","s1a2u5f693092015d4edc876d9043efcf9bcd-73963000001705001"); //
- innerattr_param = List();
- innerattr_param_map1 = Map();
- innerattr_param_map1.put("custom_field_id",<Custom Field ID>);
- innerattr_param_map1.put("value",<CRM Record ID>));
- innerattr_param.add(innerattr_param_map1);
- att_param1.put("custom_data",innerattr_param);
- data_param1.put("attributes",att_param1);
- data_param1.put("type","custommetadata");
- data.put("data",data_param1);
- associateDataTemplate = invokeurl
- [
- url :"https://www.zohoapis.com.au/workdrive/api/v1/custommetadata"
- type :POST
- parameters:data.toString()
- headers:header
- connection: <Your Workdrive Connection>
- ];
- info associateDataTemplate;
Then update your CRM record with the ID of the workdrive folder you created.
Generally, we don't attach this folder to the CRM Record, all we want to see in the CRM record are the files within the folder, not the folder itself.
Step 5: Set-up CRM Function to receive Webhook and attach file to CRM Record
Now we need to go back to your CRM Function which you setup in Step 1 and we're going to add some code:
- crmAPIRequest = crmAPIRequest.get("body");
- getFolderMetadata = invokeurl
- [
- url :"https://www.zohoapis.com.au/workdrive/api/v1/files/" + crmAPIRequest.get("data").get(0).get("resource_info").get("parent_id") + "/custommetadata"
- type :GET
- headers:{"Accept":"application/vnd.api+json"}
- connection: <Your Workdrive Connection>
- ];
- info getFolderMetadata.get("data").get(0).get("attributes").get("custom_data").get(0).get("value");
- recordId = getFolderMetadata.get("data").get(0).get("attributes").get("custom_data").get(0).get("value"); // Gets the Data out of the Data Template - This is the CRM Record ID
- r_record = zoho.crm.getRecordById(<Module>,recordId); //Get your CRm Record
- fileID = crmAPIRequest.get("data").get(0).get("resource_info").get("resource_id");
- m_map = Map();
- // Attach new file to CRM Record
- //
- l_attachments = list();
- data = Map();
- data.put("$link_url","https://workdrive.zoho.com.au/file/" + fileID);
- data.put("File_Name",<Your File Name>);
- if(!data.isEmpty())
- {
- data.put("$resource_id",recordId);
- data.put("$type","teamdrive");
- l_attachments.add(data);
- payload = "attachments=" + zoho.encryption.urlEncode({"data":l_attachments});
- attachFile = invokeurl
- [
- url :"https://www.zohoapis.com.au/crm/v2/<Module>/" + recordId + "/Attachments"
- type :POST
- parameters:payload
- connection: <Your CRM Connection>
- content-type:"application/x-www-form-urlencoded"
- ];
- info attachFile;
- // Rename File in Work Drive
- data = Map();
- data_param1 = Map();
- att_param1 = Map();
- att_param1.put("name",<Your File Name>);
- data_param1.put("attributes",att_param1);
- data_param1.put("type","files");
- data.put("data",data_param1);
- renameFile = invokeurl
- [
- url :"https://www.zohoapis.com.au/workdrive/api/v1/files/" + fileID
- type :PATCH
- parameters:data.toString()
- headers:{"Accept":"application/vnd.api+json"}
- connection: <Your Workdrive Connection>
- ];
- info renameFile;
- }
- if(!m_map.isempty())
- {
- info zoho.crm.updateRecord(<Module>,recordId,m_proof);
- }
- return "";
So how does the above code work?
Firstly, we get the body of the Webhook, this has all the data we really want.
Next we get the file metadata, this will contain the data in teh Data Template, which of course is the CRM Record ID.
Now that we have the CRM Record ID from the file Metadata, we can fetch the CRM Record. Contained within the Webhook is also the File ID in Workdrive, so this gives us all the information we need to attach the file to your CRM Record and rename the file in Workdrive.
Within this function you can now do whatever you want with the file or anything else. Let's say you want to send an email when a specific file is uploaded, you can do that here. Or if you want to send a notification via Cliq to someone when a file is uploaded, do that here. You can code specific filename syntaxes so that all your files are perfectly name all the time, the limits are endless.
This is far more advanced than most deluge functions posted on here, so see how you go, play around and have fun. If you have any questions, I'll try to respond as soon as possible.