Batch Processing of Records in Zoho CRM Using Deluge

Batch Processing of Records in Zoho CRM Using Deluge

Hello everyone,

I recently faced a challenge with bulk record creation in Zoho CRM, particularly when dealing with large numbers of records. Processing a significant volume of data all at once can lead to performance issues or even errors. To overcome this, I implemented a method that processes records in manageable batches, ensuring smooth and efficient execution.

Below is an example code that demonstrates how to handle records in Zoho CRM using batch processing. This code is designed to process "Service A" and "Service B" records for active clients, creating corresponding entries in a "Tasks" module in batches of 100 records. This approach prevents potential API limitations and ensures all records are processed effectively.

  1. week = zoho.currenttime.getWeekOfYear("Monday").toString();  // Get the current week of the year
  2. info week;

  3. clients = zoho.crm.getRecords("Accounts");  // Fetch all account records
  4. serviceAList = list();  // Initialize an empty list for 'Service A' records
  5. serviceBList = list();  // Initialize an empty list for 'Service B' records
  6. batchSize = 100;  // Define the size of each batch
  7. totalProcessed = 0;  // Track the total number of processed records

  8. for each client in clients
  9. {
  10.     if(client.get("Status").contains("Active"))
  11.     {
  12.         // Check if the client has 'Service A'
  13.         if(client.get("Services").contains("Service A"))
  14.         {
  15.             serviceAData = map();  // Create a map for storing 'Service A' data
  16.             serviceAData.put("Week", week);
  17.             serviceAData.put("Client", client.get("id"));
  18.             serviceAData.put("Zone", client.get("Zone"));
  19.             serviceAData.put("Neighborhood", client.get("Neighborhood"));
  20.             serviceAData.put("Mobile", client.get("Mobile"));
  21.             serviceAData.put("Service", ["Service A"]);  // Store the service as a JSON array
  22.             serviceAData.put("Last_Service", client.get("Last_Service"));
  23.             serviceAList.add(serviceAData);
  24.         }

  25.         // Check if the client has 'Service B'
  26.         if(client.get("Services").contains("Service B"))
  27.         {
  28.             serviceBData = map();  // Create a map for storing 'Service B' data
  29.             serviceBData.put("Week", week);
  30.             serviceBData.put("Client", client.get("id"));
  31.             serviceBData.put("Zone", client.get("Zone"));
  32.             serviceBData.put("Neighborhood", client.get("Neighborhood"));
  33.             serviceBData.put("Mobile", client.get("Mobile"));
  34.             serviceBData.put("Service", ["Service B"]);  // Store the service as a JSON array
  35.             serviceBData.put("Last_Service", client.get("Last_Service"));
  36.             serviceBList.add(serviceBData);
  37.         }
  38.     }
  39.     
  40.     // Process batches of 100 records for 'Service A'
  41.     if(serviceAList.size() >= batchSize)
  42.     {
  43.         responseServiceA = zoho.crm.bulkCreate("Tasks", serviceAList);
  44.         info "Service A bulk creation - Batch " + ((totalProcessed / batchSize) + 1).toString();
  45.         info responseServiceA;
  46.         totalProcessed = totalProcessed + serviceAList.size();
  47.         serviceAList.clear();  // Clear the list after processing
  48.     }

  49.     // Process batches of 100 records for 'Service B'
  50.     if(serviceBList.size() >= batchSize)
  51.     {
  52.         responseServiceB = zoho.crm.bulkCreate("Tasks", serviceBList);
  53.         info "Service B bulk creation - Batch " + ((totalProcessed / batchSize) + 1).toString();
  54.         info responseServiceB;
  55.         totalProcessed = totalProcessed + serviceBList.size();
  56.         serviceBList.clear();  // Clear the list after processing
  57.     }
  58. }

  59. // Process any remaining 'Service A' records
  60. if(!serviceAList.isEmpty())
  61. {
  62.     responseServiceA = zoho.crm.bulkCreate("Tasks", serviceAList);
  63.     info "Service A bulk creation - Final Batch";
  64.     info responseServiceA;
  65. }

  66. // Process any remaining 'Service B' records
  67. if(!serviceBList.isEmpty())
  68. {
  69.     responseServiceB = zoho.crm.bulkCreate("Tasks", serviceBList);
  70.     info "Service B bulk creation - Final Batch";
  71.     info responseServiceB;
  72. }

How the Code Works

  1. Initialization:

    • The script starts by retrieving the current week and all client records from the "Accounts" module. It also initializes empty lists for "Service A" and "Service B" records.
  2. Filtering and Data Preparation:

    • For each client, the script checks if the client is active and then further checks for the specific services ("Service A" or "Service B"). If a service is found, relevant data is stored in a map and added to the appropriate list.
  3. Batch Processing:

    • The code processes each list in batches of 100 records. Whenever a list reaches 100 records, the bulkCreate operation is triggered for the "Tasks" module, and the processed records are cleared from the list.
  4. Final Processing:

    • After iterating through all clients, any remaining records in the lists (those that didn't complete a batch) are processed in a final batch.
  5. Logging:

    • Throughout the process, informative messages are logged to track the batch processing, which helps in monitoring the script's execution.

Why This Approach?

  • Performance Efficiency: By processing records in batches, the script avoids hitting API limits or causing performance degradation in Zoho CRM.
  • Data Integrity: Ensures that no records are missed or duplicated during the bulk creation process.
  • Scalability: This method is scalable and can handle both small and large datasets efficiently.

This approach has worked well for me, and I hope it helps others facing similar challenges. Feel free to ask any questions or share your experiences with similar tasks!