It's common for businesses to categorize the contacts in an account by their roles or standing in the company. For example, you can classify them as primary contact, secondary contact, staff member, past employee, decision-maker, and other categories. This categorization helps ensure your salespeople are talking to the right people and sending targeted email campaigns. If you follow this setup in your CRM, you can view this information under an account's Contacts section.
However, wouldn't it be useful to be able to sort the list of contacts according to importance—for example, primary contact first, then secondary contact, staff member, past employee, and so on—instead of having to scroll through the entire list? Well, we have you covered. You can achieve this using a low-code custom function that helps you filter out past or inactive employees and sort them in a custom order of your choice.
Solution by : Hugh Marshall, Square Labs
Permissions and availability
- Users with the Manage Extensibility permission can write custom functions.
- Users with the Modules Customization permission can add related lists.
- Users with the Manage Sandbox permission can manage the sandbox.
Requirements
- Add a picklist field in the Contacts module. The picklist field will consist of different statuses of contact in an account. Learn more
- Write a custom function using Zoho's Deluge scripting language that fetches the account and its contact details, excludes inactive contacts, and displays the remaining ones in a customized sequence. Learn more
- Add a related list within the account details page and include the custom function to display the related contacts. Learn more
- Test the function in a sandbox environment before deploying it to your production environment. Learn more
Add a custom field
The first step is adding the custom picklist fields to the Contacts module.
- Navigate to Setup > Customization > Modules and Fields.
- Click the Contacts module to open the layout editor.
- Drag and drop the picklist field from the New Fields tray to the desired section of the layout.
- Name the pick list field (e.g. "Company Status"), enter the options, and define its properties as required.
You can enter multiple options such as Primary, Secondary, Staff Member, and Past Employee. - Once you have finished, select Done and then click Save and Close.
Write a custom function
The next step is to write the custom function that will be linked to the related list.
- Go to Setup > Developer Space > Functions > +New Function.
- Provide a name and display name for the function. For example: "Active Contacts".
- Select the category of the function as Related List.
- Copy and paste the code provided below.
- Click Edit Arguments.
- Enter the parameter name as accountId and select the type as string.
- Click Save.
The code
//Get account information
accountMap = zoho.crm.getRecordById("Accounts",accountId);
//Get the related contacts of the account
contactList = zoho.crm.getRelatedRecords("Contacts","Accounts",accountId);
//List the display order of the related contacts by role
primaryList = List();
secondaryList = List();
staffList = List();
otherList = List();
orderedContactList = List();
//Check if there is at least 1 related contact before proceeding
if(contactList.size() > 0)
{
//Check the contacts' role field, remove inactive or past employees, and add them to the list.
for each contact in contactList
{
if(contact.get("Company_Status") != "Past Employee")
{
if(contact.get("Company_Status") == "Primary")
{
primaryList.add({"id":contact.get("id"),"Full_Name":ifnull(contact.get("Full_Name")," "),"Email":ifnull(contact.get("Email")," "),"Mobile":ifnull(contact.get("Mobile")," "),"Company_Status":ifnull(contact.get("Company_Status")," ")});
}
else if(contact.get("Company_Status") == "Secondary")
{
secondaryList.add({"id":contact.get("id"),"Full_Name":ifnull(contact.get("Full_Name")," "),"Email":ifnull(contact.get("Email")," "),"Mobile":ifnull(contact.get("Mobile")," "),"Company_Status":ifnull(contact.get("Company_Status")," ")});
}
else if(contact.get("Company_Status") == "Staff Member")
{
staffList.add({"id":contact.get("id"),"Full_Name":ifnull(contact.get("Full_Name")," "),"Email":ifnull(contact.get("Email")," "),"Mobile":ifnull(contact.get("Mobile")," "),"Company_Status":ifnull(contact.get("Company_Status")," ")});
}
else
{
otherList.add({"id":contact.get("id"),"Full_Name":ifnull(contact.get("Full_Name")," "),"Email":ifnull(contact.get("Email")," "),"Mobile":ifnull(contact.get("Mobile")," "),"Company_Status":ifnull(contact.get("Company_Status")," ")});
}
}
}
//Display the contacts in the list according to your preference.
orderedContactList.addAll(primaryList);
orderedContactList.addAll(secondaryList);
orderedContactList.addAll(staffList);
orderedContactList.addAll(otherList);
//Check that this ordered list is not empty
if(orderedContactList.size() > 0)
{
//Prepare the XML and check if there is at least 1 active contact to display
rowVal = 0;
responseXML = "";
responseXML = responseXML + "<record>";
for each orderedContact in orderedContactList
{
responseXML = responseXML + "<row no='" + rowVal + "'>";
responseXML = responseXML + "<FL link ='true' url='<<INSERT ZOHO CRM CONTACTS URL HERE>>" + orderedContact.get("id") + "' val='Contact Name'>" + orderedContact.get("Full_Name") + "</FL>";
responseXML = responseXML + "<FL val='Email'>" + orderedContact.get("Email") + "</FL>";
responseXML = responseXML + "<FL val='Mobile'>" + orderedContact.get("Mobile") + "</FL>";
responseXML = responseXML + "<FL val='Employment Status'>" + orderedContact.get("Company_Status") + "</FL>";
responseXML = responseXML + "</row>";
rowVal = rowVal + 1;
}
//End the XML
responseXML = responseXML + "</record>";
}
else
{
//Input the message to be displayed if there are no active contacts for the account
responseXML = responseXML + "<error><message>No active contacts found</message></error>";
}
}
else
{
//Input the message to be displayed if there are no contacts associated with the account
responseXML = responseXML + "<error><message>No contacts found</message></error>";
}
info responseXML;
return responseXML;
Notes
- You won't be able to add new contacts or modify the displayed contact fields on the new related list.
- Make sure to use the accurate API names for their corresponding fields in the code snippet. Learn more
Add a related list
The final step is to add a related list to the account details page to display their active contacts.
- Open an account record to view its details.
- Go to the Related List panel on the left and click the Add Related List option.
- Select Functions in the pop-up window.
- Select Add Now, corresponding to the function you created in the previous steps.
- Specify a name for the related list. For example: "Active Contacts".
- Press # to fetch the merge field and map the argument as accountId = Accounts > Account Id.
- Click Save.
Tips
- Click the Reorder icon to move active contacts to a specific location on the account details page.
- To remove the system-defined Contacts section, move it to the unselected list.
Test the solution
- Go to the Accounts module and click on the desired contact.
- On the Account Details page, look for the Active Contacts related list section. It will display information about the active contacts associated with the account.
Did you find this useful? Try it out and let us know how it works. Share this with your team if they'd benefit from it! If you have questions, please don't hesitate to contact us.
More info
- ModuleAccounts
- Trigger PointRelated List
- EditionEnterprise and above
- ComplexityMedium
- Implementation Time30 minutes

Looking for a custom solution?
Contact us, we will help enhance your productivity at lightning speed.
SUBMIT REQUEST