Auto assignment of tickets to an agent in Zoho Desk based on Account owner in Zoho CRM

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
  1. Click the Setup icon in the top bar.
  2. In the Setup Landing page, click Workflows under Automation.
  3. In the Workflows List page, click Create Rule in the upper-right area.
  1. In the New Workflow page, do the following:
  2. Select the Ticket module.
  3. Enter the Rule Name.
  4. Select the Active checkbox.
  5. Enter the Description for the workflow rule.
  6. Click Next.
Step 2: Execute On
  1. In the Execute On section, select Create.
  2. Click Next.

Part 3: Actions
  1. Under Actions, click the Add Custom Function icon and select an New custom function.
  1. Enter function Name.
  2. Click on Edit Arguments and enter argument name ticketId and select value Ticket Id.
  3. 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.
  1. orgId = 123456789; // organization ID. Replace it with your Org Id.
  2. ticketRecord = zoho.desk.getRecordById(orgId,"tickets",ticketId); //  ticketId to be mapped as an argument
  3. contactEmail = ticketRecord.get("email"); //extracts the contact person's email ID from the ticket record
  4. departmentId = ticketRecord.get("departmentId"); //extracts department id from the ticket record
  5. // search for a matching contact in Zoho CRM
  6. contactSearchResponse = invokeurl
  7. [
  8. url : "https://www.zohoapis.com/crm/v6/Contacts/search?email=" + contactEmail
  9. type : GET
  10. connection: "zcrm"
  11. ];
  12. // checks if a matching contact is found in Zoho CRM by checking the size of the response data
  13. if(contactSearchResponse.get("data").size() > 0)
  14. {
  15. data = contactSearchResponse.get("data").get(0);
  16. contactOwnerEmail = data.get("Owner").get("email"); // extracts the contact owner's email ID
  17. accountRecord = zoho.crm.getRecordById("Accounts", data.get("Account_Name").get("id"));
  18. contactOwnerId = data.get("Owner").get("id");
  19. accountOwnerId = accountRecord.get("Owner").get("id");
  20. //contact owner's ID and account owner's ID are compared to check if they are the same
  21. //If the contact owner and account owner are the same, search for a matching agent in Zoho Desk
  22. if ( contactOwnerId == accountOwnerId )
  23. {
  24. headerMap = Map();
  25. headerMap.put("orgId",orgId);
  26. agentSearchResponse = invokeurl
  27. [
  28. url : "https://desk.zoho.com/api/v1/agents/email/" + contactOwnerEmail
  29. type : GET
  30. headers: headerMap
  31. connection: "zdesk"
  32. ];
  33. agentId = agentSearchResponse.get("id");
  34. departmentList = agentSearchResponse.get("associatedDepartmentIds");
  35. // check if the matching agent has access to the department associated with the ticket
  36. // If the agent has access to the department, assign the ticket to the agent
  37. if(departmentList.contains(departmentId) == true)
  38. {
  39. agentMap = Map();
  40. agentMap.put("assigneeId",agentId);
  41. agent = agentMap.toString();
  42. ticketResponse = invokeurl
  43. [
  44. url : "https://desk.zoho.com/api/v1/tickets/" + ticketId
  45. type : PUT
  46. parameters: agent
  47. headers: headerMap
  48. connection: "zdesk"
  49. ];
  50. }
  51. }
  52. }
Best Regards,
Subhash Kumar