Automate Bulk Record Imports in Zoho CRM Using Bulk Write API and Deluge

Automate Bulk Record Imports in Zoho CRM Using Bulk Write API and Deluge

Summary:

This article demonstrates how to use Zoho CRM’s Bulk Write API (v8) with Deluge scripts and Zoho WorkDrive to automate importing large volumes of records. The process is split across two custom functions, one for initiating the bulk import and another for tracking its status.

Custom Function: Automate Bulk Record Imports from WorkDrive  

Requirement Overview: 


Businesses often deal with high-volume data entry operations such as customer databases.  Uploading such data manually using the CRM UI is time consuming, error-prone, and inefficient.

This solution automates that workflow using:
  1. A .zip file with .CSV data uploaded in WorkDrive
  2. Two custom functions in Zoho CRM to:
                          a)  Upload the file and trigger the Bulk Write API
                          b)  Track the job status and retrieve the result

Deploy Custom Function in Zoho CRM  

This use case requires two custom functions:
  1. Function 1:
    • Fetches the .zip file from WorkDrive
    • Uploads it to CRM
    • Calls the Bulk Write API
    • Stores the job ID in a CRM variable
  1. Function 2:
    • Fetches the job ID
    • Tracks the job status
    • Downloads the result file upon success
You can configure Function 2 to run after a delay using CRM's Schedules module.


Use Case
  

Scenario:


A real estate agency receives new property data daily in CSV format. The goal is to automate the bulk creation of “Floor Plan” records in CRM using this data. The CSV is zipped and stored in WorkDrive. This setup ensures data consistency and eliminates manual imports.

Configuration Steps:
 

1. Prepare a .csv file with the required module fields.  
  • Example columns: Name, Email, etc.
  • Zip the file and upload it to a specific WorkDrive folder.
2. Create a CRM variable named Job_Id  
  • API Name: Job_Id
  • Type: Text
3. Set up API Connections 
  • One for WorkDrive
  • One for Zoho CRM (named crm in this example)


The Code  

Function 1: Initiate Bulk Write Job  

(Download WorkDrive file → Upload to CRM → Start Bulk Write job → Store Job ID)

  1. // Fetch file from WorkDrive
  2. headerMap = Map();
  3. headerMap.put("Accept","application/vnd.api+json");

  4. fetchFiles = invokeurl
  5. [
  6. url :"https://www.zohoapis.com/workdrive/api/v1/files/<FOLDER_ID>/files"
  7. type :GET
  8. headers:headerMap
  9. connection:"workdrive"
  10. ];
  11. documentId = fetchFiles.getJSON("data").get(0).get("id");

  12. // Download file
  13. downloadFile = invokeurl
  14. [
  15. url :"https://download.zoho.com/v1/workdrive/download/" + documentId
  16. type :GET
  17. headers:headerMap
  18. connection:"workdrive"
  19. ];

  20. // Upload to CRM
  21. headerMap = Map();
  22. headerMap.put("feature", "bulk-write");
  23. headerMap.put("X-CRM-ORG", "<CRM ORG_ID>");

  24. uploadFile = invokeurl
  25. [
  26. url: "https://content.zohoapis.com/crm/v8/upload"
  27. type: POST
  28. headers: headerMap
  29. files: downloadFile
  30. connection: "crm"
  31. ];

  32. fileId = uploadFile.get("details").get("file_id");

  33. // Construct Bulk Write payload
  34. fieldMappings = list();
  35. fieldMappings.add({"api_name":"Name","index":0});
  36. fieldMappings.add({"api_name":"Email","index":1});

  37. resource = Map();
  38. resource.put("type","data");
  39. resource.put("module",{"api_name":"Floor_plans"});
  40. resource.put("field_mappings",fieldMappings);
  41. resource.put("file_id",fileId);

  42. data = list();
  43. data.add(resource);

  44. payload = Map();
  45. payload.put("operation","insert");
  46. payload.put("ignore_empty","true");
  47. payload.put("callback",{"url": "https://webhook.site/<your-id>","method":"post"});
  48. payload.put("resource",data);

  49. // Trigger Bulk Write API
  50. headerMap = Map();
  51. headerMap.put("Content-Type","application/json");

  52. bulkWrite = invokeurl
  53. [
  54. url :"https://www.zohoapis.com/crm/bulk/v8/write"
  55. type :POST
  56. parameters:payload.toString()
  57. headers:headerMap
  58. connection:"crm"
  59. ];

  60. jobId = createBulkWrite.getJSON("details").get("id");

  61. jobIdVariable = "<Add the ID of the Variable>";

  62. variableMap = Map();
  63. variableMap.put("id",jobIdVariable);
  64. variableMap.put("value",jobId);

  65. variablesList = List();
  66. variablesList.add(variableMap);

  67. param = Map();
  68. param.put("variables",variablesList);

  69. // Update the variable with the Job ID value
  70. updateVariable = invokeurl
  71. [
  72. url :"https://www.zohoapis.com/crm/v8/settings/variables"
  73. type :PUT
  74. parameters:param.toString()
  75. connection:"crm"
  76. ];
  77. info updateVariable;




Function 2: Track Job Status & Download Result  

(Run using a Schedule – after 15-30 mins)

  1. // Get the Job ID variable info
  2. getVariableInfo = invokeurl
  3. [
  4. url: "https://www.zohoapis.com/crm/v8/settings/variables/3361723000088498029"
  5. type: GET
  6. connection:"stagehistory"
  7. ];
  8. info getVariableInfo;

  9. variableValue = getVariableInfo.getJSON("variables").get(0).get("value");
  10. info variableValue ;

  11. // Call the Job details API to know the status
  12. jobDetails = invokeurl
  13. [
  14. url: "https://www.zohoapis.com/crm/bulk/v8/write/"+jobId
  15. type: GET
  16.     connection:"stagehistory"
  17. ];
  18. info jobDetails;

  19. jobStatus = jobDetails.getJSON("status");

  20. // Check the status of the job
  21. if(jobStatus == "success") {
  22. // Business logic to perform a task if the job status is success
  23. }
Custom Solution Created by Francis (Vishnu) | Zoho Partner Support

Tips : Avoid Common Errors  

  1. Ensure to use the correct API Names for both Module & Fields in the script.

  2. To ensure you get the intended output, we would suggest you to use info() logs to each variable to check the output for seamless functionality under the Console section within Zoho CRM Function IDE.

  3. Since we have used the Connections within a function script, ensure to have the required scopes added in connection to perform the intended API action. Also, ensure to use the connection link name (i.e., crm_connection ) while passing on to Deluge Invoke URL or Integration Task.

  4. As a common practice, we have used US DC API end point. If you are using CRM account in a different DC (i.e., IN, EU, CA, AU, etc.), then we would recommend you to use the API end point URL according to your DC.
Refer to the following Guide - Article to learn the Best Practices for Optimising the Code and Various ways to Deploy Custom Function across Zoho CRM.



If you need any further clarifications, please don’t hesitate to contact partner-support@zohocorp.com.

Additionally, we kindly ask all "Europe and UK partners" to reach out to partner-support@eu.zohocorp.com.