Part 2: Create the function
Create a function in Zoho CRM(inclusive of the code from the Connection).
Part 2.1: Write the function
The Function code is
- string createTicket(string requestBody){
- if(isNull(requestBody))
- {
- return "No Content";
- }
- if(!requestBody.contains("arguments"))
- {
- return "Invalid Format";
- }
- requestBody = requestBody.get("arguments");
- requestBody = requestBody.get("requestBody");
- requestBody = requestBody.get("details");
- deskURL = "https://desk.zoho.com/api/v1/";
- header = {"orgId":"664950682"};
- departmentId = "264631000000006907"; //Get ticket and user details from requestBody
- userDetails = requestBody.get("user");
- ticketDetails = requestBody.get("ticket"); //Create map
- param = Map(); //Insert Details
- param.put("departmentId",departmentId);
- if(!userDetails.contains("email"))
- {
- return "Email is Mandatory";
- }
- email = userDetails.get("email");
- param.put("email",email);
- contactName = "Guest";
- if(userDetails.contains("full_name"))
- {
- contactName = userDetails.get("full_name");
- }
- else if(userDetails.contains("first_name") && userDetails.contains("last_name"))
- {
- contactName = userDetails.get("first_name") + " " + userDetails.contains("last_name");
- }
- else if(userDetails.contains("first_name"))
- {
- contactName = userDetails.get("first_name");
- }
- else if(userDetails.contains("last_name"))
- {
- contactName = userDetails.get("last_name");
- }
- //Get Contact ID from Contact name
- //Get all Contacts
- contactList = invokeurl
- [
- url :deskURL + "contacts"
- type :GET
- headers:header
- connection:"zoho_desk9"
- ];
- isExistingCustomer = false;
- contactId = "";
- contactList = contactList.get("data");
- for each contactInfo in contactList
- {
- contactEmail = contactInfo.get("email");
- if(!isNull(contactEmail))
- {
- if(email.equals(contactEmail))
- {
- isExistingCustomer = true;
- contactId = contactInfo.get("contactId");
- break;
- }
- }
- }
- info isExistingCustomer;
- if(!isExistingCustomer)
- {
- //Create a new Contact
- contactParam = Map();
- contactParam.put("firstName",userDetails.get("first_name"));
- contactParam.put("lastName",userDetails.get("last_name"));
- contactParam.put("mobile",userDetails.get("mobile"));
- contactParam.put("email",userDetails.get("email"));
- contactParam.put("accountId","264631000000081178");
- contactList = invokeurl
- [
- url :deskURL + "contacts"
- type :POST
- parameters:contactParam.toString()
- headers:header
- connection:"zoho_desk9"
- ];
- contactId = contactList.get("id");
- }
- param.put("contactId",contactId);
- if(!ticketDetails.contains("title"))
- {
- return "title is mandatory";
- }
- param.put("subject",ticketDetails.get("title"));
- if(!ticketDetails.contains("status"))
- {
- return "status is mandatory";
- }
- param.put("status",ticketDetails.get("status"));
- if(ticketDetails.contains("priority"))
- {
- param.put("priority",ticketDetails.get("priority"));
- }
- if(userDetails.contains("organization"))
- {
- organization = userDetails.get("organization");
- if(organization.contains("name"))
- {
- param.put("accountName",organization.get("name"));
- }
- }
- if(userDetails.contains("mobile"))
- {
- param.put("phone",userDetails.get("mobile"));
- }
- if(userDetails.contains("description"))
- {
- param.put("description",userDetails.get("description"));
- }
- info param;
- response = invokeurl
- [
- url :deskURL + "tickets"
- type :POST
- parameters:param.toString()
- headers:header
- connection:"zoho_desk9"
- ];
- if(response.contains("errorMessage"))
- {
- return "Sorry Something went wrong. error ::: " + response.get("errorMessage");
- }
- //Send Notification to Suppot Team
- sendmail
- [
- from :zoho.adminuserid
- to :"deborah.g@zohocorp.com"
- subject :"no-reply"
- message :"'Dear Team,<expression></expression><div><br></div><div>A New Ticket is created inside Zoho DESK through Zendesk.</div><div><br></div><div>Ticket Details - </div><div><br></div><div>User Name - ' + contactName + '</div><div><br></div><div><br></div><div>Request Body = " + requestBody + "</div><div><br></div>'"
- ]
- return response;
- }
Part2.2: To make the function available as an API Key
- Click the Settings icon for the corresponding function which needs to be made an API.
- Click REST API.
- Enable API Key slider.
- Click Save.
Part 3: Create a Webhook in Zendesk
- Go to Admin > Extension > HTTP Target.
Provide the following information:
- Function URL
- Method - GET or POST
- Select Content-type as JSON. To know more, click here.
Note:
- The Function URL is the API Key URL which you get from Step 2.
Part 4: Create a Trigger in Zendesk
- Condition - when a ticket is created.
- Notify Target - HTTP Target name
JSON Body of arguments
- {"arguments":
- {
- "requestBody":{
- "details":{
- "ticket":
- {
- "title":"{{ticket.title}}",
- "description":"{{ticket.description}}",
- "source":"{{ticket.via}}",
- "priority":"{{ticket.priority}}",
- "due_date":"{{ticket.due_date}}",
- "URI":"{{ticket.url}}",
- "status":"{{ticket.status}}"
- },
- "assigne":
- {
- "email":"{{ticket.assignee.email}}",
- "name":"{{ticket.assignee.name}}",
- "first_name":"{{ticket.assignee.first_name}}",
- "last_name":"{{ticket.assignee.last_name}}"
- },
- "user":
- {
- "full_name":"{{current_user.name}}",
- "first_name":"{{current_user.first_name}}",
- "language":"{{current_user.language}}",
- "details":"{{current_user.details}}",
- "mobile":"{{current_user.phone}}",
- "email":"{{current_user.email}}",
- "organization":
- {
- "details":"{{current_user.organization.details}}",
- "name":"{{current_user.organization.name}}"
- }}
- }}
- }}
Part 5: Configure an argument which contains the entire request body
When a webhook sends data to a function, there is no way of knowing the number of arguments present in it. In order to handle this issue, you can have the function contain the entire request body within a single argument.
Invoke the Desk API inside the Function and save the function.
The Outcome
Creating a ticket in Zendesk:
Ticket is created in Zoho Desk