For Zoho Services only:


I'm actually part of a bigger team at Ascent Business Solutions where we have support technicians and project consultants. Support is for smaller technical fixes but this can include developments, reports or integrations; depending on the size of the task. Projects are for more time-consuming developments such as revamps of the Zoho Suite of apps or on-site training. The advantage of a team is that if I am out-of-office for a day or so, there is always someone at Ascent Business Solutions who can deal with any queries/issues you may have.

Our support rates can be found and purchased at http://ascentbusiness.co.uk/zoho-support-2. A support bundle doesn't have an expiry date. So whether we can do what you want within the bundle and a year later need further support, if there are minutes left on the bundle then there is no additional charge.

Our project rates for bigger developments can be found at http://ascentbusiness.co.uk/crm-solutions/zoho-crm-packages-prices and will involve a dedicated project consultant along with developers who will hold your hand through the development process.

If you want help building a solution for one of the Zoho Apps in the Zoho Suite, contact us on 0121 392 8140 (UK) or by email at info@ascentbusiness.co.uk. You can also visit our website at http://ascentbusiness.co.uk.

I regularly build and specialize in 2-way API integrations for Xero, Shopify and eBay.

Zoho CRM: searchRecords with sorted results

What?
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:

  1. Set the record ID to your own record ID.
  2. Set the list of pages to the number of pages you want to search. Keep in mind script execution limits set by Zoho.
  3. 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.
  4. Set the "ModuleToSearch" to the API Name of the module you want to search.

Method #1: zoho.crm.searchRecords()
Contrary to the documentation saying there are only the approved and converted values supported in additional parameters, I find the below works in CRM just fine:
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;
	}
}
  1.  // ****************** CODE USING SEARCHRECORDS **************** 
  2.  // 
  3.  // init 
  4.  v_RecordID = 1234567890123456789
  5.  // 
  6.  // loop through 5 pages of 200 records = 1000 records 
  7.  l_Pages = [1,2,3,4,5]
  8.  // 
  9.  // set search criteria 
  10.  v_SearchCriteria = "Lead:equals:" + v_RecordID + ""
  11.  // 
  12.  // set results parameters 
  13.  m_SearchParam = Map()
  14.  m_SearchParam.put("sort_by", "Modified_Time")
  15.  m_SearchParam.put("sort_order", "asc")
  16.  // 
  17.  // loop through each page 
  18.  for each  v_Page in l_Pages 
  19.  { 
  20.      l_SearchResults = zoho.crm.searchRecords("ModuleToSearch", v_SearchCriteria, v_Page, 200, m_SearchParam)
  21.      // 
  22.      // loop through search results 
  23.      for each r_Result in l_SearchResults 
  24.      { 
  25.          // check that there is an ID on this record to avoid looping through error messages 
  26.          if(!isNull(r_Result.get("id"))) 
  27.          { 
  28.              // do stuff to each record... 
  29.              info r_Result.get("id")
  30.          } 
  31.      } 
  32.      // 
  33.      // stop looping if less than 200 records on this page 
  34.      if(l_SearchResults.size() < 200) 
  35.      { 
  36.          break
  37.      } 
  38.  } 

Method #2: zoho.crm.getRecords()
copyraw
// ****************** CODE USING GETRECORDS ****************
// init
l_Pages = {1,2,3,4,5};
v_PerPage = 200;
m_SortCriteria = Map();
m_SortCriteria.put("sort_by","Created_Time");
m_SortCriteria.put("sort_order","asc");
// add in additional search criteria
m_SortCriteria.put("Processed_by_Function","false");
// loop through all pages
for each v_Page in l_Pages
{
	r_SearchResults = zoho.crm.getRecords("Accounts",v_Page, v_PerPage, m_SortCriteria);
	// loop through all results per page
	for each r_Result in r_SearchResults
    {
		// check there is a record id (as error also returns result)
		if(!isnull(r_Result.get("id")))
		{
			v_AccountID = r_Result.get("id");
			m_UpdateAccount = Map();
			m_UpdateAccount.put("Processed_by_Function", true);
			r_UpdateAccount = zoho.crm.updateRecord("Accounts", v_AccountID, m_UpdateAccount);
			// optional: output response of update request
			info v_AccountID + "::" + r_UpdateAccount.toString();
		}
    }
}
  1.  // ****************** CODE USING GETRECORDS **************** 
  2.  // init 
  3.  l_Pages = {1,2,3,4,5}
  4.  v_PerPage = 200
  5.  m_SortCriteria = Map()
  6.  m_SortCriteria.put("sort_by","Created_Time")
  7.  m_SortCriteria.put("sort_order","asc")
  8.  // add in additional search criteria 
  9.  m_SortCriteria.put("Processed_by_Function","false")
  10.  // loop through all pages 
  11.  for each v_Page in l_Pages 
  12.  { 
  13.      r_SearchResults = zoho.crm.getRecords("Accounts",v_Page, v_PerPage, m_SortCriteria)
  14.      // loop through all results per page 
  15.      for each r_Result in r_SearchResults 
  16.      { 
  17.          // check there is a record id (as error also returns result) 
  18.          if(!isnull(r_Result.get("id"))) 
  19.          { 
  20.              v_AccountID = r_Result.get("id")
  21.              m_UpdateAccount = Map()
  22.              m_UpdateAccount.put("Processed_by_Function", true)
  23.              r_UpdateAccount = zoho.crm.updateRecord("Accounts", v_AccountID, m_UpdateAccount)
  24.              // optional: output response of update request 
  25.              info v_AccountID + "::" + r_UpdateAccount.toString()
  26.          } 
  27.      } 
  28.  } 

Source(s):
Category: Zoho :: Article: 804

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 bc1qjtp4l4ra452wzvuk9a45yfj82zkahsyy2z379y
© 2022 Joel Lipman .com. All Rights Reserved.