Today We came up with an alternate solution of how we can create multiple Unique Codes in Zoho CRM Modules. As we know that we can’t create more than one auto number field in Zoho CRM. So We can either drop our idea of having more than one Auto Number field. But today we are going to discuss a custom solution where we can generate multiple types of unique auto-incrementing values conditionally using any different field value. So let’s jump into understanding the use case and how we can achieve our requirement.

Understanding the requirements.

Let’s assume, we have created a custom module called “Clients_Database” and This Module will include the data of different types of clients. So let’s assure we have 4 types of client data that we will save in our Client’s database and each type has to generate a unique Client Identifier code when we select our Client type. In this example, we consider our 4 Client types given below

  1. Editor
  2. Contributor
  3. Subscriber
  4. Auditors

As per the above Client types, we will have their unique Prefixes as well, So that we can identify which Type this client belongs. So let’s consider the below Prefix for each Client type

  1. Editor -> EDT
  2. Contributor -> CTR
  3. Subscriber -> SUB
  4. Auditors -> AUD

Steps to Create the function for Unique Codes in Zoho CRM

  1. Go to the Setup area of your Zoho CRM account.
  2. Click on Workflow Rules under Automation Section
  3. Click on Create Rule Button
  4. Select the Module, Our example, we will select “Clients Database”
  5. Enter the Rule Name
  6. Select Execute Rule -> On a record action
  7. Select Condition -> Create -> Next
  8. Select All Records to affect with this rule -> Next
  9. Select Function in Instant Action
  10. Click on New Function button on the Popup Window
  11. Click on Write your own function
  12. Enter Function Name “Make sure function name doesn’t have any space”
  13. Enter Display Name -> Create
  14. Click on Edit Argument – > Enter id and set it to Client Database Record ID
Unique Codes in Zoho CRM
data = zoho.crm.getRecordById("Client_Database",id); //Client_Database is the Module's API Name
Ctype = data.getJSON("Client_Type"); //Client_Type is the API Name of Client Type field

//Prefixes for the Client Types
Editor = "EDT";
Contributor = "CTR";
Subscriber = "SUB";
Auditors = "AUD";

//Fetch Existing Client_Code in CRM using ZOHO CRM QOOL API
// Client_Code is the api name of the Client Code field

queryMap = Map();
//"select_query" is the query parameter to take your query and fetch the data in the crm.
queryMap.put("select_query","select id, Client_Code, Client_Type from Client_Database where ((Client_Type = '" + type + "') and (Client_Code is not null)) order by Client_Code DESC limit 1");
//API Call to fetch the data
response = invokeurl
[
	url :"https://www.zohoapis.com/crm/v2/coql"
	type :POST
	parameters:queryMap.toString()
	connection:"zohocrmcoql"//Connection Name
];
SearchRecs = response.get("data");
recSize = SearchRecs.size();
//info recSize;
if(recSize > 0)
{
	trList = List();
	info SearchRecs;
	for each  rec in SearchRecs
	{
		trList.add(ifnull(rec.getJSON("Client_Code"),""));
	}
	DesList = trList.sort(false);
	extracted = DesList.get(0).replaceAll("[^0-9.]","").toNumber(); // Extract the Numbers only from Existing Client Code

	if(Ctype == "Editor")
	{
		final_code = Editor + (extracted + 1);
	}
	else if(Ctype == "Contributor")
	{
		final_code = Contributor + (extracted + 1);
	}
	else if(Ctype == "Subscriber")
	{
		final_code = Subscriber + (extracted + 1);
	}
	else if(Ctype == "Auditors")
	{
		final_code = Auditors + (extracted + 1);
	}
}
else
{
	//If in CRM Data fetch, We could not find any records, then we sent these predefined Codes as per Matched Client Type
	if(Ctype == "Editor")
	{
		final_code = "EDT1001";
	}
	else if(Ctype == "Contributor")
	{
		final_code = "CTR10001";
	}
	else if(Ctype == "Subscriber")
	{
		final_code = "SUB1000001";
	}
	else if(Ctype == "Auditors")
	{
		final_code = "AUD101";
	}
}
upMap = Map();
upMap.put("Client_Code",final_code);
updateRec = zoho.crm.updateRecord("Client_Database",id,upMap);
info updateRec;

Copy the above code and paste it into your Deluge code editor and achieve your requirement of conditional unique code generation. I hope this function will help you to achieve your requirements.

Leave a Reply

Your email address will not be published. Required fields are marked *