Print

Zoho CRM: Get Unique Values of a Text Field

What?
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:
  1. Setup a connector to CRM with the scopes ZohoCRM.coql.READ and ZohoCRM.modules.ALL
  2. Build up a SQL query, or more specifically, a COQL - CRM Object Query Language
  3. Try using distinct on a single-line text field
  4. 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:
copyraw
// 
// 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;
  1.  // 
  2.  // build up COQL query 
  3.  v_CoqlQuery = "select distinct Currency from Quotes where Subject!=''"
  4.  // 
  5.  // build up parameters 
  6.  m_Params = Map()
  7.  m_Params.put("select_query",v_CoqlQuery)
  8.  // 
  9.  // invokeUrl 
  10.  r_Coql = invokeUrl 
  11.  [ 
  12.      url :"https://www.zohoapis.eu/crm/v2/coql" 
  13.      type :POST 
  14.      parameters: m_Params.toString() 
  15.      connection: "joels_coql_connection" 
  16.  ]
  17.  // 
  18.  // output 
  19.  info r_Coql; 
But this returns something like the following (I've reduced the 200 records to about 6 of them):
copyraw
{
  "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
  }
}
  1.  { 
  2.    "data": [ 
  3.      { 
  4.        "Currency": "EUR", 
  5.        "id": "123456789000000001" 
  6.      }, 
  7.      { 
  8.        "Currency": "EUR", 
  9.        "id": "123456789000000002" 
  10.      }, 
  11.      { 
  12.        "Currency": "EUR", 
  13.        "id": "123456789000000003" 
  14.      }, 
  15.      { 
  16.        "Currency": "GBP", 
  17.        "id": "123456789000000004" 
  18.      }, 
  19.      { 
  20.        "Currency": "USD", 
  21.        "id": "123456789000000005" 
  22.      }, 
  23.      { 
  24.        "Currency": "USD", 
  25.        "id": "123456789000000006" 
  26.      } 
  27.    ], 
  28.    "info": { 
  29.      "count": 200, 
  30.      "more_records": true 
  31.    } 
  32.  } 
Which isn't what I want because it has returned the same currency several times and thinks they're unique/distinct because the IDs of each quote record are different.

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):
copyraw
// 
// 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;
  1.  // 
  2.  // build up COQL query 
  3.  v_CoqlQuery = "select Currency from Quotes where Subject!='' group by Currency"
  4.  // 
  5.  // build up parameters 
  6.  m_Params = Map()
  7.  m_Params.put("select_query",v_CoqlQuery)
  8.  // 
  9.  // invokeUrl 
  10.  r_Coql = invokeUrl 
  11.  [ 
  12.      url :"https://www.zohoapis.eu/crm/v2/coql" 
  13.      type :POST 
  14.      parameters:m_Params.toString() 
  15.      connection:"joels_coql_connection" 
  16.  ]
  17.  // 
  18.  // output 
  19.  info r_Coql; 
Which returns something like the following:
copyraw
{
  "data": [
    {
      "Currency": "EUR",
      "id": "123456789000000001"
    },
    {
      "Currency": "GBP",
      "id": "123456789000000004"
    },
    {
      "Currency": "USD",
      "id": "123456789000000005"
    }
  ],
  "info": {
    "count": 3,
    "more_records": false
  }
}
  1.  { 
  2.    "data": [ 
  3.      { 
  4.        "Currency": "EUR", 
  5.        "id": "123456789000000001" 
  6.      }, 
  7.      { 
  8.        "Currency": "GBP", 
  9.        "id": "123456789000000004" 
  10.      }, 
  11.      { 
  12.        "Currency": "USD", 
  13.        "id": "123456789000000005" 
  14.      } 
  15.    ], 
  16.    "info": { 
  17.      "count": 3, 
  18.      "more_records": false 
  19.    } 
  20.  } 

Amazing! Let's go one further for a lookup field, in this case the account name field on the quotes record:
copyraw
// 
// 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;
  1.  // 
  2.  // build up COQL query 
  3.  v_CoqlQuery = "select Account_Name.Account_Name from Quotes where Subject!='' group by Account_Name.Account_Name"
  4.  // 
  5.  // build up parameters 
  6.  m_Params = Map()
  7.  m_Params.put("select_query",v_CoqlQuery)
  8.  // 
  9.  // invokeUrl 
  10.  r_Coql = invokeUrl 
  11.  [ 
  12.      url :"https://www.zohoapis.eu/crm/v2/coql" 
  13.      type :POST 
  14.      parameters:m_Params.toString() 
  15.      connection:"joels_coql_connection" 
  16.  ]
  17.  // 
  18.  // output 
  19.  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):
Category: Zoho :: Article: 776