Rodger's Helpful Deluge Scripts #3 - Automatically attach files to CRM records when a file is added to Workdrive

Rodger's Helpful Deluge Scripts #3 - Automatically attach files to CRM records when a file is added to Workdrive

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:
  1. CRM record is created
  2. Workdrive folder is created
  3. A Data Bind is created between the CRM Record and the Workdrive Folder
  4. 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

  1. // Create folder
  2. header = Map();
  3. header.put("Accept","application/vnd.api+json");
  4. data = Map();
  5. data_param1 = Map();
  6. att_param1 = Map();
  7. att_param1.put("name",<Folder Name>);
  8. att_param1.put("parent_id",<Parent Folder ID>);
  9. data_param1.put("attributes",att_param1);
  10. data_param1.put("type","files");
  11. data.put("data",data_param1);
  12. createFolder = invokeurl
  13. [
  14. url :"https://www.zohoapis.com.au/workdrive/api/v1/files"
  15. type :POST
  16. parameters:toString(data)
  17. headers:header
  18. connection: <Your Workdrive Connection>
  19. ];
  20. info createFolder;
  21. // m_record.put("Folder_Id",createFolder.get("data").get("id")); - Put the created folder ID into the map for your CRM record
  22. // Associate Data Template
  23. header = Map();
  24. header.put("Accept","application/vnd.api+json");
  25. data = Map();
  26. data_param1 = Map();
  27. att_param1 = Map();
  28. att_param1.put("resource_id",createFolder.get("data").get("id"));
  29. att_param1.put("data_template_id","s1a2u5f693092015d4edc876d9043efcf9bcd-73963000001705001"); // 
  30. innerattr_param = List();
  31. innerattr_param_map1 = Map();
  32. innerattr_param_map1.put("custom_field_id",<Custom Field ID>);
  33. innerattr_param_map1.put("value",<CRM Record ID>));
  34. innerattr_param.add(innerattr_param_map1);
  35. att_param1.put("custom_data",innerattr_param);
  36. data_param1.put("attributes",att_param1);
  37. data_param1.put("type","custommetadata");
  38. data.put("data",data_param1);
  39. associateDataTemplate = invokeurl
  40. [
  41. url :"https://www.zohoapis.com.au/workdrive/api/v1/custommetadata"
  42. type :POST
  43. parameters:data.toString()
  44. headers:header
  45. connection: <Your Workdrive Connection>
  46. ];
  47. info associateDataTemplate;

Note: Use the following documentation to get the ID of your Data Template Field needed for line 33 above Zoho WorkDrive API Documentation

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:

  1. crmAPIRequest = crmAPIRequest.get("body");
  2. getFolderMetadata = invokeurl
  3. [
  4. url :"https://www.zohoapis.com.au/workdrive/api/v1/files/" + crmAPIRequest.get("data").get(0).get("resource_info").get("parent_id") + "/custommetadata"
  5. type :GET
  6. headers:{"Accept":"application/vnd.api+json"}
  7. connection: <Your Workdrive Connection>
  8. ];
  9. info getFolderMetadata.get("data").get(0).get("attributes").get("custom_data").get(0).get("value");
  10. 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
  11. r_record = zoho.crm.getRecordById(<Module>,recordId); //Get your CRm Record
  12. fileID = crmAPIRequest.get("data").get(0).get("resource_info").get("resource_id");
  13. m_map = Map();
  14. // Attach new file to CRM Record
  15. //
  16. l_attachments = list();
  17. data = Map();
  18. data.put("$link_url","https://workdrive.zoho.com.au/file/" + fileID);
  19. data.put("File_Name",<Your File Name>);
  20. if(!data.isEmpty())
  21. {
  22. data.put("$resource_id",recordId);
  23. data.put("$type","teamdrive");
  24. l_attachments.add(data);
  25. payload = "attachments=" + zoho.encryption.urlEncode({"data":l_attachments});
  26. attachFile = invokeurl
  27. [
  28. url :"https://www.zohoapis.com.au/crm/v2/<Module>/" + recordId + "/Attachments"
  29. type :POST
  30. parameters:payload
  31. connection: <Your CRM Connection>
  32. content-type:"application/x-www-form-urlencoded"
  33. ];
  34. info attachFile;
  35. // Rename File in Work Drive
  36. data = Map();
  37. data_param1 = Map();
  38. att_param1 = Map();
  39. att_param1.put("name",<Your File Name>);
  40. data_param1.put("attributes",att_param1);
  41. data_param1.put("type","files");
  42. data.put("data",data_param1);
  43. renameFile = invokeurl
  44. [
  45. url :"https://www.zohoapis.com.au/workdrive/api/v1/files/" + fileID
  46. type :PATCH
  47. parameters:data.toString()
  48. headers:{"Accept":"application/vnd.api+json"}
  49. connection: <Your Workdrive Connection>
  50. ];
  51. info renameFile;
  52. }
  53. if(!m_map.isempty())
  54. {
  55. info zoho.crm.updateRecord(<Module>,recordId,m_proof);
  56. }
  57. 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.

      Zoho Developer Community









                                Zoho Desk Resources

                                • Desk Community Learning Series


                                • Digest


                                • Functions


                                • Meetups


                                • Kbase


                                • Resources


                                • Glossary


                                • Desk Marketplace


                                • MVP Corner


                                • Word of the Day



                                    Zoho Marketing Automation
                                            • Sticky Posts

                                            • Introducing Built-In Telephony in Zoho CRM

                                              Dear All, Greetings! We are excited to introduce the bespoke "Built-In Telephony" in Zoho CRM. Built-in telephony is designed to allow organizations to build their own telephony framework, manage subscriptions and payments, generate call reports, and
                                            • Enhancements in Canvas

                                              Dear All, Greetings! Canvas lets you design the record details page to suit your brand or business preferences. We are glad to introduce the following enhancements to uplift your design experience. Reusable Components Style Presets Let's go! Reusable
                                            • Stay in the Know: Important TRAI mandate on URL whitelisting for Indian CRM customers

                                              Dear Customers, We hope you're well! If you are a CRM customer in India and have the Notification SMS feature enabled, this post is relevant to you. Notification SMS lets you send messages to your customers as an acknowledging response to transactions.
                                            • Announcing the deprecation of a few capabilities within Feeds

                                              Dear Customers, We hope you're well! We are here with a recent update we made to the Feeds feature in Zoho CRM. We have deprecate the following capabilities from Feeds: View inactive/ dormant records View records followed by you Auto-follow records With
                                            • Effortlessly advance through your CRM blueprints by including optional fields in transitions

                                              Editions: Professional edition and above DCs: All Release plan: This enhancement is being released in a phased manner to customers. It will soon be available to all users. [Updated on 5 Sep 2024] This enhancement has been released to all users in all


                                            Manage your brands on social media



                                                  Zoho TeamInbox Resources

                                                    Zoho DataPrep Resources



                                                      Zoho CRM Plus Resources

                                                        Zoho Books Resources


                                                          Zoho Subscriptions Resources

                                                            Zoho Projects Resources


                                                              Zoho Sprints Resources


                                                                Qntrl Resources


                                                                  Zoho Creator Resources



                                                                    Zoho WorkDrive Resources



                                                                      Zoho Campaigns Resources


                                                                        Zoho CRM Resources

                                                                        • CRM Community Learning Series

                                                                          CRM Community Learning Series


                                                                        • Tips

                                                                          Tips

                                                                        • Functions

                                                                          Functions

                                                                        • Meetups

                                                                          Meetups

                                                                        • Kbase

                                                                          Kbase

                                                                        • Resources

                                                                          Resources

                                                                        • Digest

                                                                          Digest

                                                                        • CRM Marketplace

                                                                          CRM Marketplace

                                                                        • MVP Corner

                                                                          MVP Corner





                                                                            Design. Discuss. Deliver.

                                                                            Create visually engaging stories with Zoho Show.

                                                                            Get Started Now