Auto assignment of tickets to an agent in Zoho Desk based on Account owner in Zoho CRM
Hello everyone!
I have written a custom function in Zoho Desk to automatically assign a ticket to an agent in Zoho Desk based on the account owner in Zoho CRM when a new ticket is created in Zoho Desk. I hope this helps who need it.
You must create a workflow to automate this. To create a workflow, follow below steps.
Step 1: Basic Information
- Click the Setup icon in the top bar.
- In the Setup Landing page, click Workflows under Automation.
- In the Workflows List page, click Create Rule in the upper-right area.
- In the New Workflow page, do the following:
- Select the Ticket module.
- Enter the Rule Name.
- Select the Active checkbox.
- Enter the Description for the workflow rule.
- Click Next.
Step 2: Execute On
- In the Execute On section, select Create.
- Click Next.
Part 3: Actions
- Under Actions, click the Add Custom Function icon and select an New custom function.
- Enter function Name.
- Click on Edit Arguments and enter argument name ticketId and select value Ticket Id.
- Click Done.
Pre-requisites:
1. Create connection in Zoho Desk with following scopes for Zoho CRM service
ZohoCRM.modules.contacts.READ, ZohoSearch.securesearch.READ
2. Create connection in Zoho Desk with following scopes for Zoho Desk service
Desk.settings.READ, Desk.basic.READ
Paste below code in editor. Replace orgId with your org ID.
- orgId = 123456789; // organization ID. Replace it with your Org Id.
- ticketRecord = zoho.desk.getRecordById(orgId,"tickets",ticketId); // ticketId to be mapped as an argument
- contactEmail = ticketRecord.get("email"); //extracts the contact person's email ID from the ticket record
- departmentId = ticketRecord.get("departmentId"); //extracts department id from the ticket record
- // search for a matching contact in Zoho CRM
- contactSearchResponse = invokeurl
- [
- url : "https://www.zohoapis.com/crm/v6/Contacts/search?email=" + contactEmail
- type : GET
- connection: "zcrm"
- ];
- // checks if a matching contact is found in Zoho CRM by checking the size of the response data
- if(contactSearchResponse.get("data").size() > 0)
- {
- data = contactSearchResponse.get("data").get(0);
- contactOwnerEmail = data.get("Owner").get("email"); // extracts the contact owner's email ID
- accountRecord = zoho.crm.getRecordById("Accounts", data.get("Account_Name").get("id"));
- contactOwnerId = data.get("Owner").get("id");
- accountOwnerId = accountRecord.get("Owner").get("id");
- //contact owner's ID and account owner's ID are compared to check if they are the same
- //If the contact owner and account owner are the same, search for a matching agent in Zoho Desk
- if ( contactOwnerId == accountOwnerId )
- {
- headerMap = Map();
- headerMap.put("orgId",orgId);
- agentSearchResponse = invokeurl
- [
- url : "https://desk.zoho.com/api/v1/agents/email/" + contactOwnerEmail
- type : GET
- headers: headerMap
- connection: "zdesk"
- ];
- agentId = agentSearchResponse.get("id");
- departmentList = agentSearchResponse.get("associatedDepartmentIds");
- // check if the matching agent has access to the department associated with the ticket
- // If the agent has access to the department, assign the ticket to the agent
- if(departmentList.contains(departmentId) == true)
- {
- agentMap = Map();
- agentMap.put("assigneeId",agentId);
- agent = agentMap.toString();
- ticketResponse = invokeurl
- [
- url : "https://desk.zoho.com/api/v1/tickets/" + ticketId
- type : PUT
- parameters: agent
- headers: headerMap
- connection: "zdesk"
- ];
- }
- }
- }
Best Regards,
Subhash Kumar