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:
Company Name: Father & Sons (Incorporated) Contact Name: O'Reilly
- Company Name: Father & Sons (Incorporated)
- 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:
- ZohoCRM > Setup > Developer Space > Connections
- Click on Get Started / Add New Connection
- Click on the "Zoho OAuth" icon
- Enter a Connection name (for this example I will call it "CRM API v2")
- Select the appropriate scope(s):
- ZohoCRM.coql.READ Required!
- ZohoCRM.modules.{module_name}.{operation_type} or ZohoCRM.modules.all
- Click Create and Connect
- You will be prompted to allow permissions, so click on "Accept/Allow"
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
Note that using this method, you don't have to escape parentheses characters ():
// 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 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); }
- // 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 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);
- }
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).
Regular expression reminder (do not use):
v_SearchName = v_SearchName.replaceAll("([&\'])","\\$1",false);
- v_SearchName = v_SearchName.replaceAll("([&\'])","\\$1",false);
Source(s)