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.

    Access your files securely from anywhere


            Zoho Developer Community





                                      • Desk Community Learning Series


                                      • Digest


                                      • Functions


                                      • Meetups


                                      • Kbase


                                      • Resources


                                      • Glossary


                                      • Desk Marketplace


                                      • MVP Corner


                                      • Word of the Day


                                      • Ask the Experts



                                          Zoho Marketing Automation


                                                  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 CRM Resources

                                                                            • CRM Community Learning Series

                                                                              CRM Community Learning Series


                                                                            • Kaizen

                                                                              Kaizen

                                                                            • 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


                                                                                  Zoho Show Resources


                                                                                    Zoho Writer Writer

                                                                                    Get Started. Write Away!

                                                                                    Writer is a powerful online word processor, designed for collaborative work.

                                                                                      Zoho CRM コンテンツ








                                                                                        Nederlandse Hulpbronnen


                                                                                            ご検討中の方





                                                                                                  • Recent Topics

                                                                                                  • Trying to copy Active Leads to a Custom Module called Dead Leads

                                                                                                    Hi, I am trying to code when the "Active Lead Status" field is changed to "Dead", a copy of the record should be created in the "Dead Leads" module while keeping the original lead in "Active Leads." I am using the following code but I keep getting the
                                                                                                  • Blocking an email address

                                                                                                    Hi How do I block an email address in my Zoho mail? Thanks Brian 
                                                                                                  • Zoho Desk add OIDC federation for 3rd party IDP

                                                                                                    Please consider adding the ability to use OIDC/OAuth compliant Identity Providers to Zoho Desk. You currently have Zoho, Microsoft, LinkedIn, Google and Facebook. But if you added the ability to use ANY OIDC compliant provider, you would add compatibility
                                                                                                  • Cliq funtion not triggerring from Books.

                                                                                                    Gentlemen, I have been requesting the Zoho Books team for 15 days through various emails- then even escalated. The response that I get from Zoho is " Our back-end team is working on it......." It is a template-type response. I authenticated the link(Cliq
                                                                                                  • Tracking Agent Diligence in Updating Records

                                                                                                    Our organization mainly focus on B2B. While we've implemented automations like pushing info from web forms into CRM, we often get info from events and individual networks. The complete/updated info relies in the agents who directly interacts with them,
                                                                                                  • how to create a company without an assigned owner

                                                                                                    In my company we are reassigning accounts and we need to leave some companies without an owner to review their potential and then assign them, but the options that appear in owner require that they be assigned to a seller, how to leave them without an
                                                                                                  • Stop selling out of stock Items.

                                                                                                    Hi I have been using Zohobooks for a around 8 month now. I am not involved in selling process but my staff cant stop selling product which they do not hold in stock, this is a big headache for me as physical count never matches what is shown on the books. 
                                                                                                  • Matrix dropdown

                                                                                                    Is there a way to create a matrix question with dropdown answers but 1 row correlates to 1 column? So really I want to combine 4 dropdown questions into 1 matrix but each question has a single, and different, dropdown for answers.
                                                                                                  • Managing Rental Inventory in Zoho

                                                                                                    Can Zoho Inventory manage rental inventory efficiently, or is there another Zoho app that would be better suited for this kind of operation? Looking for advice on the best approach. Thanks in advance.
                                                                                                  • Code API récupération enquêtes et documents

                                                                                                    Bonjour peut-on générer des codes API avec Zoho Survey ou Forms afin de récupérer des questionnaires existants sur nos autres outils ? Comment procéder ? Faut-il disposer d'un abonnement particulier ?
                                                                                                  • Field Updated based off Call Status

                                                                                                    I'm trying to create a Field Update where: When Call Status is Completed Lead/Contact/Account Description field with the information in the "Outcome Of Outgoing Call" Description field. Once our sales team finishes their calls and they add a description
                                                                                                  • Exporting uploaded files in bulk on Zoho Survey?

                                                                                                    Hi, I have a survey that includes an image upload field, I'd like to be able to export the individual responses in bulk, but it appears only the name of the image uploaded, and not the image itself gets exported. Any way to have the images be exported
                                                                                                  • Is there API Doc for Zoho Survey?

                                                                                                    Hi everyone, Is there API doc for Zoho Survey? Currently evaluating a solution - use case to automate survey administration especially for internal use. But after a brief search, I couldn't find API doc for this. So I thought I should ask here. Than
                                                                                                  • Create Quote does not show the "Product Description" entered as part of the Product setup.

                                                                                                    The product description created as part of the product setup page, does not show in the Create Quote module; The module allows for an additional description to be added but without access to the original stored description. By selecting the product from the "Product Name Lookup" pop-up, the "Product Description" part should be populated from the product record where the user is allowed to further modify it.
                                                                                                  • How to convert Item to composite Item

                                                                                                    Dear Team, We need to convert Item to composite Item as Zoho is importing partial information from Shopify.
                                                                                                  • Function #4: Schedule Customer Statements

                                                                                                    Regularly sending statements to customers is an imperative part of many business processes as it helps foster strong customer relationships and provides timely guidance on payments. While you can generate the statement of accounts and have it sent over
                                                                                                  • Why wont Zoho Support Grammarly!! --- PLEASE VOTE FOR THIS to show Zoho we need this

                                                                                                    The spell check and grammar in ZOHO are so buggy and a waste of time. Please support Grammarly! I'm sure I'm not the only one — there are other CRMs that support this. If you're not planning to add this feature, Please let others know before accepting
                                                                                                  • Phone Number Format Change - Bulk Add Country Code and Remove Dashes

                                                                                                    In the past I would create a new customer in my Zoho Books, and enter their phone number in the format customary for the USA (that's where we are domiciled): Areacode-Prefix-Number 234-456-6789 This scheme is important as many other software packages
                                                                                                  • User Tips: Auto-Create Opportunity/Deal upon Quote Save (PART 1)

                                                                                                    Problem: We use quotes which convert to sales orders but Users / Sales Reps do not create opportunities / deals and go straight to creating a quote. This leads to poor reporting. Implementing this solution improves reporting and makes it easier for users.
                                                                                                  • Client Can't View Ticket

                                                                                                    Hi, I created a ticket for my Client since he had an issue. I added him as a Contact as well. My client got a notification in his email that the ticket has been created. When he went to view the ticket on his desktop, the screen was blank. He then viewed
                                                                                                  • Automating Custom Web Link Messages to Customers via Instant Messaging in Zoho Desk

                                                                                                    Hello, I am looking for assistance with Zoho Desk's Instant Messaging feature. Is there a way to automate the sending of a custom web link to customers every day at 6:30 PM? Any guidance or suggestions on how to achieve this would be greatly appreciated.
                                                                                                  • Categorise Attachments

                                                                                                    We take ID, proof of address, right to work documentation and more.  I can upload a single file in to field, but we often receive multiple files for each category e.g. someone may send a separate file for the front and back of their national ID card.  My team don't have time to manipulate the files in order to upload them as a single file. The options, as far as I can tell, would be to create additional fields on attachments in order to categorise what the file is, or to be able to upload single
                                                                                                  • How to autorespond for inactive users?

                                                                                                    Hi, we have a few inactive users that we would like to set up an autorespond to the sender telling that the email addresses are no longer active. How can we do that?
                                                                                                  • Mass Action Button Script

                                                                                                    Hi all, I've been trying to search a lot of places for a solution on how to do a button placed at the "Mass Action Menu" and thus only updating multiple CHOSEN records at once. After finding no success whatsoever, I decided to just run and test whatever
                                                                                                  • Error 553

                                                                                                    Hello how are you? I'm having trouble sending email. This message appears whenever I try to send an email. I'm receiving emails normally, but I just can't send them
                                                                                                  • Implement Regex in Layout and Validation rules

                                                                                                    Hello all, We are excited to announce that users can now implement Regular Expressions (Regex) in our layout and validation rules. This new functionality allows for more flexible rules to be created when designing and validating forms. What is Regex?
                                                                                                  • Zoho Desk Validation Rule Using Custom Function

                                                                                                    Hi all, I tried to find the way to validate fields using custom function just like in Zoho CRM but to no avail. Is there a way to do this?
                                                                                                  • Confirmation prompt before a custom button action is triggered

                                                                                                    Have you ever created a custom button and just hoped that you/your users are prompted first to confirm the action? Well, Zoho knows this concept. For example, in blueprint, whenever we want to advance to the next state by clicking the transition, it is
                                                                                                  • How do we add Google Analytics code to Zoho Bookings scheduling pages or the thank you page?

                                                                                                    We need to track user activity on individual Bookings' pages and/or the thank you pages? How do we do this?
                                                                                                  • Zoho Sheets working offline

                                                                                                    Hi,  I am looking for the ability to work offline in Zoho Sheets, but currently I cannot find the process to complete this.  Does someone have any ideas or steps I might have missed?  Also does Zoho Sheets have the "Format as Tables" function as is currently in MS Excel 2016??  Many thanks. 
                                                                                                  • Repeating Sections in Writer

                                                                                                    I am wondering if it's possible to create repeating sections or text boxes of a document based on a merged subform fields coming from Zoho Forms. We are currently using excel to dynamically create a legal PDF document based on input fields in another
                                                                                                  • Add Knowledge Base KB Articles to multiple categories

                                                                                                    Greetings,  Love you help center system.  One item that would be incredibly helpful to many of us would be able to add a single Knowledge Base KB article to multiple categories in our system.  It seems it could be quite easy to use a checkbox form, instead
                                                                                                  • Lost the ability to sort by ticket owner

                                                                                                    Hi all, in the last week or so, we have lost the ability to sort tickets by Ticket Owner. Unlike the other columns which we can hover over and click on to sort, Ticket Owner is no longer clickable. Is it just us, or are other customers seeing this too?
                                                                                                  • Default Reminder for tasks set to Pop-up instead of email

                                                                                                    Try to save a click every time we create a task we want it to default to pop-up instead of email. Also default is to have reminder unchecked anyway to change so reminder is checked and method is pop-up default instead of email? Attached picture. Thanks,
                                                                                                  • Why hasn't Zoho CRM For Everyone been rolled out?

                                                                                                    I don't understand the point of rolling out new features so slowly after a big fanfare launch 8 months ago. I've signed up for 'early access' and also contacted my point of contact, but nothing. Not even an auto reply. Would you say that this is good
                                                                                                  • Canva Integration

                                                                                                    Hello! As many marketing departments are streamlining their teams, many have begun utilizing Canva for all design mockups and approvals prior to its integration into Marketing automation software. While Zoho Social has this integration already accomplished,
                                                                                                  • Time Zone Sending in Marketing Automation

                                                                                                    Good day...can anyone share if they have been able to get sending via Time Zones to actually work? Our data is 99% perfect, with all the time zone criteria met, yet the results are completely off target when we try to use it. We've had varied results
                                                                                                  • Combine Small Pie Slices in Pie Chart

                                                                                                    Hello, I am trying to make a pie chart showing our sales in each of our product categories. The problem is that we have over 80 product categories, and not that much room from which to look at it. I am wondering if there is a way to combine the smallest
                                                                                                  • Which are the IP addresses to use for 'split delivery' with Office 365? (Zoho mail inbound gateway)

                                                                                                    Hi, I'm trying to set up 'split delivery' (email routing) with Office 365. I'm following the instructions to set up Office 365 as the primary server (https://www.zoho.com/mail/help/adminconsole/coexistence-with-office365.html) One of the prerequisites
                                                                                                  • Zoho Desk Onboarding Assistance - How to do bulk taging

                                                                                                    Hi How to apply a particular tag to multiple tickets in one go.
                                                                                                  • Next Page