This is an article to document how to use the searchRecords function in CRM and to sort the results response.
Why?
This is unclear and not always working as coded below since each system (CRM, Creator, Books) seems to interpret this sorting feature differently. This article is an effort to find clear and working solutions. It is working again as of 2022-02-10.
How?
Two methods listed here, the standard searchRecords() and then the getRecords() functions:
- Set the record ID to your own record ID.
- Set the list of pages to the number of pages you want to search. Keep in mind script execution limits set by Zoho.
- Set the search criteria string to your specific requirements (:equals:, :starts_with:, |=|). If 1 expression then you can omit the parenthesis. If multiple expressions then separate each with parenthesis joined by "and" or "or". Nulls are not returned.
- Set the "ModuleToSearch" to the API Name of the module you want to search.
Method #1: zoho.crm.searchRecords()
copyraw
// ****************** CODE USING SEARCHRECORDS **************** // // init v_RecordID = 1234567890123456789; // // loop through 5 pages of 200 records = 1000 records l_Pages = [1,2,3,4,5]; // // set search criteria v_SearchCriteria = "Lead:equals:" + v_RecordID + ""; // // set results parameters m_SearchParam = Map(); m_SearchParam.put("sort_by", "Modified_Time"); m_SearchParam.put("sort_order", "asc"); // // loop through each page for each v_Page in l_Pages { l_SearchResults = zoho.crm.searchRecords("ModuleToSearch", v_SearchCriteria, v_Page, 200, m_SearchParam); // // loop through search results for each r_Result in l_SearchResults { // check that there is an ID on this record to avoid looping through error messages if(!isNull(r_Result.get("id"))) { // do stuff to each record... info r_Result.get("id"); } } // // stop looping if less than 200 records on this page if(l_SearchResults.size() < 200) { break; } }
- // ****************** CODE USING SEARCHRECORDS ****************
- //
- // init
- v_RecordID = 1234567890123456789;
- //
- // loop through 5 pages of 200 records = 1000 records
- l_Pages = [1,2,3,4,5];
- //
- // set search criteria
- v_SearchCriteria = "Lead:equals:" + v_RecordID + "";
- //
- // set results parameters
- m_SearchParam = Map();
- m_SearchParam.put("sort_by", "Modified_Time");
- m_SearchParam.put("sort_order", "asc");
- //
- // loop through each page
- for each v_Page in l_Pages
- {
- l_SearchResults = zoho.crm.searchRecords("ModuleToSearch", v_SearchCriteria, v_Page, 200, m_SearchParam);
- //
- // loop through search results
- for each r_Result in l_SearchResults
- {
- // check that there is an ID on this record to avoid looping through error messages
- if(!isNull(r_Result.get("id")))
- {
- // do stuff to each record...
- info r_Result.get("id");
- }
- }
- //
- // stop looping if less than 200 records on this page
- if(l_SearchResults.size() < 200)
- {
- break;
- }
- }
Method #2: using COQL
copyraw
// ****************** CODE USING GETRECORDS **************** // init l_Pages = {1,2,3,4,5}; v_PerPage = 200; // // get other products with the same name (set in order of oldest first and keep only first one, delete the others) m_Params = {"select_query":"select id, Product_Name from Products where Product_Name = '" + v_SearchName + "' order by id limit " + v_PerPage}; r_CoqlSortedProducts = invokeUrl [ url :"https://www.zohoapis.eu/crm/v7/coql" type :POST parameters:m_Params.toString() connection:"zcrm" ]; l_SortedProducts = ifnull(r_CoqlSortedProducts.get("data"), List());
- // ****************** CODE USING GETRECORDS ****************
- // init
- l_Pages = {1,2,3,4,5};
- v_PerPage = 200;
- //
- // get other products with the same name (set in order of oldest first and keep only first one, delete the others)
- m_Params = {"select_query":"select id, Product_Name from Products where Product_Name = '" + v_SearchName + "' order by id limit " + v_PerPage};
- r_CoqlSortedProducts = invokeUrl
- [
- url :"https://www.zohoapis.eu/crm/v7/coql"
- type :POST
- parameters:m_Params.toString()
- connection:"zcrm"
- ];
- l_SortedProducts = ifnull(r_CoqlSortedProducts.get("data"), List());
Source(s):
- Joel Lipman - ZohoCRM: Process all records of a module
- Zoho Deluge - Search Records in Zoho CRM
- Zoho Deluge - Get Records from Zoho CRM
Category: Zoho :: Article: 804
Add comment