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
- In the CRM, go to the Setup.
- Under "developer space", click on "connections".
- Click on "create connection".
- The service is "Zoho OAuth".
- Name the connection. We are going to use "crm_workdrive".
- Add the scopes. Use the magnifier to easily find the scopes.
- 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
- Click on "create and connect".
- Click on "connect" and allow any permissions you are requested.
Setting up the function
- In the Zoho CRM settings, go to "Functions" under "Developer Space".
- Create a new function: Add a name and select "Automation" as the category.
- Copy the function located below and paste it in the text editor in Zoho CRM.
- Edit the arguments: add "deal_id" as a string.
- Generate your folder structure:
- Rename the folders,
- Add more folders,
- Create subfolders if needed.
- Test the function with a Deal that has a WorkDrive Team Folder attached.
- 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
- In the setup page, click on workflow rules under automations
- 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)
- Configure your workflow trigger and conditions:
- When > on a record action > Edit > "Subfolders Needed" is modified to the value of "Yes"
- Condition: all Deals
- As the instant action, select "function"
- Click on "new function" and then select "functions"
- Find the function you just created, and click on "configure"
- Map the argument "deal_id" as the deal ID, using the # key
- Click on "save and associate" and save the workflow
- Test the workflow