One of this things I love about Zoho is that there's so many ways to accomplish something. Want to mass update some records? Sure, we have the Mass Update function to update a single field. Need to update more than 1 field? Sure, we have she Sheet view, but what if you need to incorporate logic decisions or calculations into your record update?
You could use sheet view, but that's limited to 100 records at a time, and what if there's multiple logic decisions to be made? Well, you use a deluge function.
This is the function I use when dealing with relatively small numbers of records. It can work well up to a couple of thousand records:
Firstly you're going to create a standalone function with the following arguments:
v_module (string data type)
v_records (integer data type)
Then we're going to use the following code:
- // Create a list of pages based on the number of records in the module. This will take the number of records, v_records,
- // divide it by 200 (200 records per page) and then round up to the nearest whole number. It will then use this number to repeat the text "1,"
- // and finally, it will remove the last "," and convert the string into a list
- l_pages = repeat("1,",(v_records/200).ceil()).removeLastOccurence(",").toList();
- l_bulkUpdate = List(); // Create a list to hold the records for the bulk update
- v_page = 1; // set the first page number
- for each page in l_pages
- {
- l_records = zoho.crm.getRecords(v_module,v_page,200);
- for each record in l_records
- {
- m_record = Map();
- m_record.put("id", record.get("id"));
- m_record.put("updated", true); // Create a temporary checkbox in your module to track whether the record has been updated
- //
- // insert your code here
- //
- l_bulkUpdate.add(m_record);
- if ( l_bulkUpdate.size() == 100 )
- {
- bulkUpdate = zoho.crm.bulkUpdate(v_module, l_bulkUpdate);
- info "Bulk Update: " + bulkUpdate;
- l_bulkUpdate.clear(); // Clears the bulk update list after sucessful bulk update
- }
- }
- v_page = v_page + 1; // Index the page number by 1
- }
- if ( l_bulkUpdate.size() > 0)
- {
- bulkUpdate = zoho.crm.bulkUpdate(v_module, l_bulkUpdate); // Final bulk update if the list size is greater than zero but less than 100
- info "Bulk Update: " + bulkUpdate;
- }
Now put your logic in where it says "// insert your code here" then you can save and execute.
Because we use the put the module name in a variable, you can use this code for any module and will be prompted for the module name and the record count when you click "Save and Execute"
This comes with a warning however - DO NOT TRY THIS IN PRODUCTION IF YOU'RE TESTING YOUR CODE. ALWAYS TEST THIS IN SANDBOX FIRST BEFORE USING IT IN PRODUCTION!!
Also, test out your code on a single record before testing out your bulk update. That way you won't have to undo a huge mess if something goes wrong.
This is by no means the ONLY way to bulk update records using a deluge function and will not work if you have tens or hundreds of thousands of records.