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;
- }