This article demonstrates how to return a list of unique values in a particular field in a CRM module.
Why?
My use-case is a data-migration where the records exist in a staging module in CRM but I want to transform/translate one of the column values into the target
How?
So this is my plan:
- Setup a connector to CRM with the scopes ZohoCRM.coql.READ and ZohoCRM.modules.ALL
- Build up a SQL query, or more specifically, a COQL - CRM Object Query Language
- Try using distinct on a single-line text field
- Adapt the query for unique values in a lookup field
I won't demonstrate how to set up a connector as there is plenty of documentation on the Zoho Official Documentation and even on this website so we'll skip to the COQL query. This is what I have:
// // build up COQL query v_CoqlQuery = "select distinct Currency from Quotes where Subject!=''"; // // 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: "joels_coql_connection" ]; // // output info r_Coql;
- //
- // build up COQL query
- v_CoqlQuery = "select distinct Currency from Quotes where Subject!=''";
- //
- // 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: "joels_coql_connection"
- ];
- //
- // output
- info r_Coql;
{ "data": [ { "Currency": "EUR", "id": "123456789000000001" }, { "Currency": "EUR", "id": "123456789000000002" }, { "Currency": "EUR", "id": "123456789000000003" }, { "Currency": "GBP", "id": "123456789000000004" }, { "Currency": "USD", "id": "123456789000000005" }, { "Currency": "USD", "id": "123456789000000006" } ], "info": { "count": 200, "more_records": true } }
- {
- "data": [
- {
- "Currency": "EUR",
- "id": "123456789000000001"
- },
- {
- "Currency": "EUR",
- "id": "123456789000000002"
- },
- {
- "Currency": "EUR",
- "id": "123456789000000003"
- },
- {
- "Currency": "GBP",
- "id": "123456789000000004"
- },
- {
- "Currency": "USD",
- "id": "123456789000000005"
- },
- {
- "Currency": "USD",
- "id": "123456789000000006"
- }
- ],
- "info": {
- "count": 200,
- "more_records": true
- }
- }
Having spoken to Zoho and raised with the Zoho Deluge team... Well, they realised they would get the same results, and then suggested looping through all records and creating a distinct list variable... But what if there are 20000+ records?
The advantage of working in a team of Zoho developers, is that "two heads are better than one" does apply more than we think. A colleague of mine suggested adding a group by to the query which works (tested with both the keyword 'distinct' and without it with no difference in the response):
// // build up COQL query v_CoqlQuery = "select Currency from Quotes where Subject!='' group by Currency"; // // 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:"joels_coql_connection" ]; // // output info r_Coql;
- //
- // build up COQL query
- v_CoqlQuery = "select Currency from Quotes where Subject!='' group by Currency";
- //
- // 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:"joels_coql_connection"
- ];
- //
- // output
- info r_Coql;
{ "data": [ { "Currency": "EUR", "id": "123456789000000001" }, { "Currency": "GBP", "id": "123456789000000004" }, { "Currency": "USD", "id": "123456789000000005" } ], "info": { "count": 3, "more_records": false } }
- {
- "data": [
- {
- "Currency": "EUR",
- "id": "123456789000000001"
- },
- {
- "Currency": "GBP",
- "id": "123456789000000004"
- },
- {
- "Currency": "USD",
- "id": "123456789000000005"
- }
- ],
- "info": {
- "count": 3,
- "more_records": false
- }
- }
Amazing! Let's go one further for a lookup field, in this case the account name field on the quotes record:
// // build up COQL query v_CoqlQuery = "select Account_Name.Account_Name from Quotes where Subject!='' group by Account_Name.Account_Name"; // // 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:"joels_coql_connection" ]; // // output info r_Coql;
- //
- // build up COQL query
- v_CoqlQuery = "select Account_Name.Account_Name from Quotes where Subject!='' group by Account_Name.Account_Name";
- //
- // 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:"joels_coql_connection"
- ];
- //
- // output
- info r_Coql;
Additional
If you wanted the unique values of a picklist/dropdown, then consider an invokeUrl looping through the fields settings in CRM.
Source(s):
- Zoho CRM Developer Docs API v2 - Get Records through a COQL Query
- Merriam-Webster Dictionary - Two heads are better than one