Function: Create a WorkDrive Team Folder and attach it to the CRM record

05.26.23 09:54 AM By Andy

What we are going to do

We are going to create a workflow that will trigger a function that creates a Team folder in Zoho WorkDrive, and the folder will be attached to the attachments section in the CRM record. We are going to create a connection, then work on the function, and finally create the workflow.

Important notes

  • We are going to create a Team Folder. If you want to create a normal folder, check out the next tutorial.
  • You have to have full access to Zoho CRM and WorkDrive.
  • We are going to use the Deals module, but this works with any module, as long as the attachments section is enabled.

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. Get the parent folder ID from Zoho WorkDrive (on the URL).
    1. Replace "yourparentfolderid" with the id from the URL.
    1. Make changes to the createTeamFolder function's parameters if needed:
      1. Add a description or leave it empty ("").
      2. Select if it's a public folder ("true") or private folder ("false").
      3. Make sure the connection name matches the connection you created in the previous step.
      1. Test the function with any deal.
      2. Save the function.
      DealInfo = zoho.crm.getRecordById("Deals",deal_id);
      DealName = DealInfo.get("Deal_Name");
      ParentFolderID = "yourparentfolderid";
      // limiting the amount of characters because the Workdrive folder name has a limit
      if(DealName.len() >= 40)
      {
      DealName = DealName.subString(0,40);
      }
      FolderName = DealName + " - Main Folder";
      // folder name - parent folder id - description - public - connection
      Create_Team_Folder = zoho.workdrive.createTeamFolder(FolderName,ParentFolderID,"Description test",true,"crm_workdrive");
      info Create_Team_Folder;
      Team_Folder_ID = Create_Team_Folder.get("data").get("id");
      info Team_Folder_ID;
      // adding users to the team drive folder ----------------------------------------------
      // l = {"email1@yourdomain.com","email2@yourdomain.com"};
      // for each email in l
      // {
      // header = Map();
      // header.put("Accept","application/vnd.api+json");
      // data = Map();
      // data_param1 = Map();
      // att_param1 = Map();
      // att_param1.put("resource_id",Team_Folder_ID);
      // att_param1.put("shared_type","workspace");
      // att_param1.put("email_id",email);
      // att_param1.put("role_id","1");
      // data_param1.put("attributes",att_param1);
      // data_param1.put("type","members");
      // data.put("data",data_param1);
      // response = invokeurl
      // [
      // url :"https://www.zohoapis.com/workdrive/api/v1/members"
      // type :POST
      // parameters:data.toString()
      // headers:header
      // connection:"crm_workdrive"
      // ];
      // info response;
      // }
      // // link folder as ATTACHMENT IN CRM ------------------------------------------------
      dataList = List();
      data = Map();
      data.put("$link_url","https://workdrive.zoho.com/home/" + ParentFolderID + "/teams/" + ParentFolderID + "/ws/" + Team_Folder_ID + "/folders/files");
      data.put("File_Name",FolderName);
      data.put("$type","teamdrive");
      dataList.add(data);
      payload = "attachments=" + zoho.encryption.urlEncode({"data":dataList});
      response = invokeurl
      [
      url :"https://www.zohoapis.com/crm/v3/Deals/" + deal_id + "/Attachments"
      type :POST
      parameters:payload
      connection:"crm_workdrive"
      content-type:"application/x-www-form-urlencoded"
      ];

      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 > Stage is modified to the value of Won
        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