For Zoho services only


I'm currently part of a wider delivery team at Ascent Business Solutions, recognised as a leading Zoho Premium Solutions Partner in the United Kingdom.

Ascent Business Solutions support organisations with everything from targeted technical fixes through to full Zoho CRM implementations and long-term platform adoption. Working as a team rather than a one-person consultancy allows projects to move forward consistently, with access to the right skills at each stage.

The team I manage specialises in API integrations between Zoho and third-party finance and commerce platforms such as Xero, Shopify, WooCommerce, and eBay. Much of our work involves solving integration challenges that fall outside standard documentation, supporting new ideas, new sectors, and evolving business models.

Success is measured through practical outcomes and return on investment, ranging from scaling small operations into high-turnover businesses to delivering rapid gains through online payments, automation, and streamlined digital workflows.

If you are looking for structured Zoho expertise backed by an established consultancy, you can contact Ascent Business Solutions on 0121 392 8140 (UK), email info@ascentbusiness.co.uk, or visit https://www.ascentbusiness.co.uk.
Zoho Deluge: Search Records with Special Characters (COQL)

Zoho Deluge: Search Records with Special Characters (COQL)

What?
This is an article to remind me how to search for CRM records by a value that may contain an ampersand or parenthesis.

Why?
I wrote this article because some searches will work for me and sometimes it won't. Escaping the ampersand with a backslash or url encoding to %26 wasn't working for me. I spent several hours trying to write a script that could search for the existing records by company name. The issue is that if you use zoho.crm.searchRecords() this will work fine for company names without special characters such as the ampersand or parentheses. But what if amongst your records you may want to find:
copyraw
Company Name: Father & Sons (Incorporated)
Contact Name: O'Reilly
  1.  Company Name: Father & Sons (Incorporated) 
  2.  Contact Name: O'Reilly 

How?
Well I've tried various replace methods with regular expressions but the only method reliable enough I have found to work each time is using the CRM Object Query Language or Zoho's COQL. Similar to SQL but subject to similar issues of escaping special characters...

1. Setup a CRM Connection to Search Records via API
I won't go into detail on how to set up your own connection as I have elsewhere on my website, but to use in an invokeUrl here's a quick overview:
  1. ZohoCRM > Setup > Developer Space > Connections
  2. Click on Get Started / Add New Connection
  3. Click on the "Zoho OAuth" icon
  4. Enter a Connection name (for this example I will call it "CRM API v2")
  5. Select the appropriate scope(s):
    • ZohoCRM.coql.READ Required!
    • ZohoCRM.modules.{module_name}.{operation_type} or ZohoCRM.modules.all
  6. Click Create and Connect
  7. You will be prompted to allow permissions, so click on "Accept/Allow"
2. Determine the client datacenter
Look at the URL (website address) of the CRM you are wanting to do this on. The part after Zoho is the top level domain (TLD) and will be either .COM, .EU or as per your region. If the first part of your URL says:
  • https://zoho.com/ then use the endpoint https://www.zohoapis.com/crm/v2/coql
  • https://zoho.eu/ then use the endpoint https://www.zohoapis.eu/crm/v2/coql
  • https://zoho.com.cn/ then use the endpoint https://www.zohoapis.com.cn/crm/v2/coql
3. The Deluge Code - Uses the EU datacenter
Note that using this method, you don't have to escape parentheses characters ():
copyraw
// initialize
v_MatchedCount = 0;
v_MatchedAccountID = 0;
v_SearchName = "Father & Sons (Incorporated)";
//
// replace apostrophes with double apostrophe for sql
v_SearchName = v_SearchName.replaceAll("'","''",true);
//
// replace ampersand with unicode value
v_SearchName = v_SearchName.replaceAll("&","\u0026",true);
//
// build up COQL query
v_CoqlQuery = "select id from Accounts where Account_Name='" + v_SearchName + "'";
//
// build up parameters
m_Params = Map();
m_Params.put("select_query",v_CoqlQuery);
//
// invokeurl
r_Coql = invokeurl
[
    url :"https://www.zohoapis.eu/crm/v2/coql"
    type :POST
    parameters:m_Params.toString()
    connection:"crm_api_v2"
];
//
// retrieve results
if(!isNull(r_Coql.get("info")))
{
    v_MatchedCount = ifnull(r_Coql.get("info").get("count"),0);
}
if(!isNull(r_Coql.get("data")))
{
    v_MatchedAccountID = ifnull(r_Coql.get("data").get(0).get("id"),0);
}
  1.  // initialize 
  2.  v_MatchedCount = 0
  3.  v_MatchedAccountID = 0
  4.  v_SearchName = "Father & Sons (Incorporated)"
  5.  // 
  6.  // replace apostrophes with double apostrophe for sql 
  7.  v_SearchName = v_SearchName.replaceAll("'","''",true)
  8.  // 
  9.  // replace ampersand with unicode value 
  10.  v_SearchName = v_SearchName.replaceAll("&","\u0026",true)
  11.  // 
  12.  // build up COQL query 
  13.  v_CoqlQuery = "select id from Accounts where Account_Name='" + v_SearchName + "'"
  14.  // 
  15.  // build up parameters 
  16.  m_Params = Map()
  17.  m_Params.put("select_query",v_CoqlQuery)
  18.  // 
  19.  // invokeurl 
  20.  r_Coql = invokeurl 
  21.  [ 
  22.      url :"https://www.zohoapis.eu/crm/v2/coql" 
  23.      type :POST 
  24.      parameters:m_Params.toString() 
  25.      connection:"crm_api_v2" 
  26.  ]
  27.  // 
  28.  // retrieve results 
  29.  if(!isNull(r_Coql.get("info"))) 
  30.  { 
  31.      v_MatchedCount = ifnull(r_Coql.get("info").get("count"),0)
  32.  } 
  33.  if(!isNull(r_Coql.get("data"))) 
  34.  { 
  35.      v_MatchedAccountID = ifnull(r_Coql.get("data").get(0).get("id"),0)
  36.  } 
Note that for the above, if there are no matching records, r_Coql will simply return an empty string. Hence the check for isNull on the keys info and data.

And yes, the part that took me about 8 hours and the key point above is to replace the ampersand with its unicode equivalent. I consider myself familiar with Transact-SQL, Oracle PL/SQL and MySQL all of which have various ways of escaping the ampersand (except for MySQL which doesn't need to and in my opinion is the best SQL).

Additional Note(s):
  • select can specify up to 50 fields. Defaults to 200 but can return up to 10000 records (NB: use endpoint https://www.zohoapis.com/crm/v7/coql).
  • where can contain up to 25 criteria. Use parenthesis for precedence.
  • Pagination: LIMIT offset, limit:
    copyraw
    v_CoqlQuery = "select id, Deal_Name from Deals where ((Amount=0) and (Stage != 'Cancelled')) limit 400,2";
    // returns 2 deals after the first 400 records
    
    // OR 
    
    v_CoqlQuery = "select id, Deal_Name from Deals where ((Amount=0) and (Stage != 'Cancelled')) limit 2 offset 400";
    // returns 2 deals after the first 400 records
    
    // OR 
    
    v_PageOffset = v_Page - 1;
    v_PageOffset = v_PageOffset * v_PerPage;
    v_CoqlQuery = "select id, Deal_Name from Deals where ((Amount=0) and (Stage != 'Cancelled')) limit " + v_PerPage + " offset " + v_PageOffset;
    1.  v_CoqlQuery = "select id, Deal_Name from Deals where ((Amount=0) and (Stage != 'Cancelled')) limit 400,2"
    2.  // returns 2 deals after the first 400 records 
    3.   
    4.  // OR 
    5.   
    6.  v_CoqlQuery = "select id, Deal_Name from Deals where ((Amount=0) and (Stage != 'Cancelled')) limit 2 offset 400"
    7.  // returns 2 deals after the first 400 records 
    8.   
    9.  // OR 
    10.   
    11.  v_PageOffset = v_Page - 1
    12.  v_PageOffset = v_PageOffset * v_PerPage; 
    13.  v_CoqlQuery = "select id, Deal_Name from Deals where ((Amount=0) and (Stage != 'Cancelled')) limit " + v_PerPage + " offset " + v_PageOffset; 
  • Sort Order: ORDER BY column_name ASC/DESC:
    copyraw
    v_CoqlQuery = "select id, Deal_Name from Deals where ((Amount=0) and (Stage != 'Cancelled')) order by id desc, Stage asc";
    // returns 200 deals in most recently created order
    1.  v_CoqlQuery = "select id, Deal_Name from Deals where ((Amount=0) and (Stage != 'Cancelled')) order by id desc, Stage asc"
    2.  // returns 200 deals in most recently created order 
Regular expression reminder (do not use):
copyraw
v_SearchName = v_SearchName.replaceAll("([&\'])","\\$1",false);
  1.  v_SearchName = v_SearchName.replaceAll("([&\'])","\\$1",false)

Source(s)
Category: Zoho Deluge :: Article: 268

Joes Word Cloud

Accreditation

Badge - Zoho Creator Certified Developer Associate
Badge - Zoho Deluge Certified Developer
Badge - Certified Zoho CRM Developer

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 bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF

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: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience 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