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.
- week = zoho.currenttime.getWeekOfYear("Monday").toString(); // Get the current week of the year
- info week;
- clients = zoho.crm.getRecords("Accounts"); // Fetch all account records
- serviceAList = list(); // Initialize an empty list for 'Service A' records
- serviceBList = list(); // Initialize an empty list for 'Service B' records
- batchSize = 100; // Define the size of each batch
- totalProcessed = 0; // Track the total number of processed records
- for each client in clients
- {
- if(client.get("Status").contains("Active"))
- {
- // Check if the client has 'Service A'
- if(client.get("Services").contains("Service A"))
- {
- serviceAData = map(); // Create a map for storing 'Service A' data
- serviceAData.put("Week", week);
- serviceAData.put("Client", client.get("id"));
- serviceAData.put("Zone", client.get("Zone"));
- serviceAData.put("Neighborhood", client.get("Neighborhood"));
- serviceAData.put("Mobile", client.get("Mobile"));
- serviceAData.put("Service", ["Service A"]); // Store the service as a JSON array
- serviceAData.put("Last_Service", client.get("Last_Service"));
- serviceAList.add(serviceAData);
- }
- // Check if the client has 'Service B'
- if(client.get("Services").contains("Service B"))
- {
- serviceBData = map(); // Create a map for storing 'Service B' data
- serviceBData.put("Week", week);
- serviceBData.put("Client", client.get("id"));
- serviceBData.put("Zone", client.get("Zone"));
- serviceBData.put("Neighborhood", client.get("Neighborhood"));
- serviceBData.put("Mobile", client.get("Mobile"));
- serviceBData.put("Service", ["Service B"]); // Store the service as a JSON array
- serviceBData.put("Last_Service", client.get("Last_Service"));
- serviceBList.add(serviceBData);
- }
- }
-
- // Process batches of 100 records for 'Service A'
- if(serviceAList.size() >= batchSize)
- {
- responseServiceA = zoho.crm.bulkCreate("Tasks", serviceAList);
- info "Service A bulk creation - Batch " + ((totalProcessed / batchSize) + 1).toString();
- info responseServiceA;
- totalProcessed = totalProcessed + serviceAList.size();
- serviceAList.clear(); // Clear the list after processing
- }
- // Process batches of 100 records for 'Service B'
- if(serviceBList.size() >= batchSize)
- {
- responseServiceB = zoho.crm.bulkCreate("Tasks", serviceBList);
- info "Service B bulk creation - Batch " + ((totalProcessed / batchSize) + 1).toString();
- info responseServiceB;
- totalProcessed = totalProcessed + serviceBList.size();
- serviceBList.clear(); // Clear the list after processing
- }
- }
- // Process any remaining 'Service A' records
- if(!serviceAList.isEmpty())
- {
- responseServiceA = zoho.crm.bulkCreate("Tasks", serviceAList);
- info "Service A bulk creation - Final Batch";
- info responseServiceA;
- }
- // Process any remaining 'Service B' records
- if(!serviceBList.isEmpty())
- {
- responseServiceB = zoho.crm.bulkCreate("Tasks", serviceBList);
- info "Service B bulk creation - Final Batch";
- info responseServiceB;
- }
How the Code Works
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.
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.
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.
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.
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!