Function: Create a Subfolders in WorkDrive from Zoho CRM

06.08.23 04:12 PM By Andy

What we are going to do

We are going to create a workflow that will trigger a function that creates a set of subfolders inside a Team folder in Zoho WorkDrive. You can use the function to create folders or subfolders in your personal folder as well. This is continuation of the previous tutorial called "Function: Create a WorkDrive Team Folder and Attach it to the CRM Record".

Important notes

  • We are going to create normal folders. If you want to create a Team Folder, check out this tutorial: https://www.blungo.com/blogs/post/function-create-a-workdrive-team-folder-and-attach-it-to-the-crm-record.
  • This tutorial is Part 2 of the WorkDrive folder creation series. The code will work as long as you already have a WorkDrive Team Folder attached to the Deal. You can modify the code as needed if you are not going to use a Team Folder. The code below includes some examples of different ways to get the folder ID. 
  • You have to have full access to Zoho CRM and WorkDrive.

Creating a connection

  1. In the CRM, go to the Setup.
  2. Under "developer space", click on "connections".
  3. Click on "create connection".
  4. The service is "Zoho OAuth".
  5. Name the connection. We are going to use "crm_workdrive".
  6. Add the scopes. Use the magnifier to easily find the scopes.
    1. List of scopes: ZohoCRM.modules.ALL, zohosearch.securesearch.READ, WorkDrive.team.ALL, WorkDrive.workspace.ALL, WorkDrive.files.ALL, WorkDrive.organization.READ, WorkDrive.members.READ, WorkDrive.members.CREATE, WorkDrive.members.UPDATE, WorkDrive.members.DELETE, WorkDrive.teamfolders.CREATE, WorkDrive.teamfolders.READ, WorkDrive.teamfolders.UPDATE, WorkDrive.teamfolders.DELETE, WorkDrive.teamfolders.sharing.CREATE, WorkDrive.teamfolders.sharing.READ, WorkDrive.teamfolders.sharing.UPDATE, WorkDrive.teamfolders.sharing.DELETE, WorkDrive.teamfolders.admin.READ, WorkDrive.groups.CREATE, WorkDrive.groups.READ, WorkDrive.groups.UPDATE, WorkDrive.groups.DELETE, WorkDrive.DataTemplates.CREATE, WorkDrive.DataTemplates.DELETE, WorkDrive.DataTemplates.UPDATE, WorkDrive.links.ALL, WorkDrive.users.READ
  7. Click on "create and connect".
  8. Click on "connect" and allow any permissions you are requested.

Setting up the function

  1. In the Zoho CRM settings, go to "Functions" under "Developer Space".
  2. Create a new function: Add a name and select "Automation" as the category.
  3. Copy the function located below and paste it in the text editor in Zoho CRM.
  4. Edit the arguments: add "deal_id" as a string.
  5. Generate your folder structure:
    1. Rename the folders,
    2. Add more folders,
    3. Create subfolders if needed.
    1. Test the function with a Deal that has a WorkDrive Team Folder attached.
    2. Save the function.
    folder_info = zoho.crm.getRelatedRecords("Attachments","Deals",deal_id);
    //this section finds the link in the attachments section, then it creates a list of elements in the link, and then picks the 8th element, which is the folder ID
    Parent_Folder_ID = folder_info.get(0).get("$link_url").toList("/").get(8);
    info Parent_Folder_ID;
    // Option 2 to find the folder ID
    // count = 0;
    // for each  item in folder_info
    // {
    // owner_id = folder_info.get(count).get("Owner").get("id");
    // if(owner_id == "4038274000010997002")
    // {
    // Parent_Folder_ID = folder_info.get(count).get("$link_url").toList("/").get(8);
    // info Parent_Folder_ID;
    // }
    // count = count + 1;
    // }
    // Option 3: manually add the team folder ID
    // Parent_Folder_ID = "replacewithyourteamfolder";
    // creating folder 1 ------------------------------------------------
    folder_1 = zoho.workdrive.createFolder("Folder 1 Name",Parent_Folder_ID,"crm_workdrive");
    folder_1_id = folder_1.get("data").get("id");
    //creating sub folders
    subfolder_1 = zoho.workdrive.createFolder("Subfolder 1 Name",folder_1_id,"crm_workdrive");
    subfolder_2 = zoho.workdrive.createFolder("Subfolder 2 Name",folder_1_id,"crm_workdrive");
    // creating folder 2 -----------------------------------------------
    folder_2 = zoho.workdrive.createFolder("Folder 2 Name",Parent_Folder_ID,"crm_workdrive");
    folder_2_id = folder_2.get("data").get("id");
    //creating subfolders from list
    subfolders = {"Subfolder 3 Name","Subfolder 4 Name","Subfolder 5 Name","Subfolder 6 Name"};
    for each  rec in subfolders
    {
    zoho.workdrive.createFolder(rec,folder_2_id,"crm_workdrive");
    }

    Creating a workflow

    1. In the setup page, click on workflow rules under automations
    2. Create a new rule, select the Deals module, and name it (we are selecting the Deals instead of the contact because we need to trigger this automation when a Deal stage changes)
    3. Configure your workflow trigger and conditions:
      1. When > on a record action > Edit > "Subfolders Needed" is modified to the value of "Yes"
      2. Condition: all Deals
    4. As the instant action, select "function"
    5. Click on "new function" and then select "functions"
    6. Find the function you just created, and click on "configure"
    7. Map the argument "deal_id" as the deal ID, using the # key
    8. Click on "save and associate" and save the workflow
    9. Test the workflow