Using API Key - Serverless Functions

Using API Key - Serverless Functions

A severless function within your CRM can be invoked from any third-party application or within the CRM using a webhook. However, generally most of the webhooks do not support OAuth2, save for a few of them. In that case, you can execute the function using the API Key authentication method, which can be used by either you or others to invoke the function from anywhere.

The difference between API Key and OAuth2 lies in the mode of authentication. API Key has to be authenticated in the Request URL and not as a header.

See also

Sample Function

Scenario

Help desk and support-related softwares are an absolute essential in a business which involves interaction with customers and clients. Integrating your help desk softwares with your CRM saves up a lot of time and effort to manage all your business information from a single place.

Zendesk is one such software. Zoho Desk, developed by Zoho, is also one, but the difference is that Zoho Desk integration is made automatic in Zoho CRM, whereas you need to integrate Zendesk only through Widgets. Furthermore, when you need to transfer the ticket information from one help desk to another, you need to go to great lengths. Hence an integration of Zendesk and Zoho Desk, through Serverless Functions in Zoho CRM, is pretty convenient and useful.

The scenario which we are going to assume now is similar. Whenever a "Ticket" is created in "Zendesk", a ticket should be created in Zoho Desk as well, with the same information.

Required Applications
  • Zendesk
  • Zoho Desk

Part 1: Make a Connection

In order for the Function to make use of third-party applications and facilitate data transfer, you need to first set up a connection between Zoho CRM and the said third-party software. In this case, it would be Zendesk. Read more.

Create a connection to Zoho Desk


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

  1. string createTicket(string requestBody){
  2.   if(isNull(requestBody))
  3.   {
  4.     return "No Content";
  5.   }
  6.   if(!requestBody.contains("arguments"))
  7.   {
  8.     return "Invalid Format";
  9.   }
  10.   requestBody = requestBody.get("arguments");
  11.   requestBody = requestBody.get("requestBody");
  12.   requestBody = requestBody.get("details");
  13.   deskURL = "https://desk.zoho.com/api/v1/";
  14.   header = {"orgId":"664950682"};
  15.   departmentId = "264631000000006907"; //Get ticket and user details from requestBody
  16.   userDetails = requestBody.get("user");
  17.   ticketDetails = requestBody.get("ticket"); //Create map
  18.   param = Map(); //Insert Details
  19.   param.put("departmentId",departmentId);
  20.   if(!userDetails.contains("email"))
  21.   {
  22.     return "Email is Mandatory";
  23.   }
  24.   email = userDetails.get("email");
  25.   param.put("email",email);
  26.   contactName = "Guest";
  27.   if(userDetails.contains("full_name"))
  28.   {
  29.     contactName = userDetails.get("full_name");
  30.   }
  31.   else if(userDetails.contains("first_name") && userDetails.contains("last_name"))
  32.   {
  33.     contactName = userDetails.get("first_name") + " " + userDetails.contains("last_name");
  34.   }
  35.   else if(userDetails.contains("first_name"))
  36.   {
  37.     contactName = userDetails.get("first_name");
  38.   }
  39.   else if(userDetails.contains("last_name"))
  40.   {
  41.     contactName = userDetails.get("last_name");
  42.   }
  43.   //Get Contact ID from Contact name
  44.   //Get all Contacts
  45.   contactList = invokeurl
  46.   [
  47.     url :deskURL + "contacts"
  48.     type :GET
  49.     headers:header
  50.     connection:"zoho_desk9"
  51.   ];
  52.   isExistingCustomer = false;
  53.   contactId = "";
  54.   contactList = contactList.get("data");
  55.   for each contactInfo in contactList
  56.   {
  57.     contactEmail = contactInfo.get("email");
  58.     if(!isNull(contactEmail))
  59.     {
  60.       if(email.equals(contactEmail))
  61.       {
  62.         isExistingCustomer = true;
  63.         contactId = contactInfo.get("contactId");
  64.         break;
  65.       }
  66.     }
  67.   }
  68.   info isExistingCustomer;
  69.   if(!isExistingCustomer)
  70.   {
  71.     //Create a new Contact
  72.     contactParam = Map();
  73.     contactParam.put("firstName",userDetails.get("first_name"));
  74.     contactParam.put("lastName",userDetails.get("last_name"));
  75.     contactParam.put("mobile",userDetails.get("mobile"));
  76.     contactParam.put("email",userDetails.get("email"));
  77.     contactParam.put("accountId","264631000000081178");
  78.     contactList = invokeurl
  79.     [
  80.       url :deskURL + "contacts"
  81.       type :POST
  82.       parameters:contactParam.toString()
  83.       headers:header
  84.       connection:"zoho_desk9"
  85.     ];
  86.     contactId = contactList.get("id");
  87.   }
  88.   param.put("contactId",contactId);
  89.   if(!ticketDetails.contains("title"))
  90.   {
  91.     return "title is mandatory";
  92.   }
  93.   param.put("subject",ticketDetails.get("title"));
  94.   if(!ticketDetails.contains("status"))
  95.   {
  96.     return "status is mandatory";
  97.   }
  98.   param.put("status",ticketDetails.get("status"));
  99.   if(ticketDetails.contains("priority"))
  100.   {
  101.     param.put("priority",ticketDetails.get("priority"));
  102.   }
  103.   if(userDetails.contains("organization"))
  104.   {
  105.     organization = userDetails.get("organization");
  106.     if(organization.contains("name"))
  107.     {
  108.       param.put("accountName",organization.get("name"));
  109.     }
  110.   }
  111.   if(userDetails.contains("mobile"))
  112.   {
  113.     param.put("phone",userDetails.get("mobile"));
  114.   }
  115.   if(userDetails.contains("description"))
  116.   {
  117.     param.put("description",userDetails.get("description"));
  118.   }
  119.   info param;
  120.   response = invokeurl
  121.   [
  122.     url :deskURL + "tickets"
  123.     type :POST
  124.     parameters:param.toString()
  125.     headers:header
  126.     connection:"zoho_desk9"
  127.   ];
  128.   if(response.contains("errorMessage"))
  129.   {
  130.     return "Sorry Something went wrong. error ::: " + response.get("errorMessage");
  131.   }
  132.   //Send Notification to Suppot Team
  133.   sendmail
  134.   [
  135.     from :zoho.adminuserid
  136.     to :"deborah.g@zohocorp.com"
  137.     subject :"no-reply"
  138.     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>'"
  139.   ]
  140.   return response;


Part2.2: To make the function available as an API Key

  1. Click the Settings icon for the corresponding function which needs to be made an API.
  2. Click REST API.



  3. Enable API Key slider.
  4. Click Save.



Part 3: Create a Webhook in Zendesk

  1. Go to Admin > Extension > HTTP Target.
  2. 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

  1. Condition - when a ticket is created.
  2. Notify Target - HTTP Target name




JSON Body of arguments
  1. {"arguments":
  2. {
  3. "requestBody":{

  4. "details":{
  5. "ticket":
  6. {
  7. "title":"{{ticket.title}}",
  8. "description":"{{ticket.description}}",
  9. "source":"{{ticket.via}}",
  10. "priority":"{{ticket.priority}}",
  11. "due_date":"{{ticket.due_date}}",
  12. "URI":"{{ticket.url}}",
  13. "status":"{{ticket.status}}"
  14. },
  15. "assigne":
  16. {
  17. "email":"{{ticket.assignee.email}}",
  18. "name":"{{ticket.assignee.name}}",
  19. "first_name":"{{ticket.assignee.first_name}}",
  20. "last_name":"{{ticket.assignee.last_name}}"
  21. },
  22. "user":
  23. {
  24. "full_name":"{{current_user.name}}",
  25. "first_name":"{{current_user.first_name}}",
  26. "language":"{{current_user.language}}",
  27. "details":"{{current_user.details}}",
  28. "mobile":"{{current_user.phone}}",
  29. "email":"{{current_user.email}}",
  30. "organization":
  31. {
  32. "details":"{{current_user.organization.details}}",
  33. "name":"{{current_user.organization.name}}"
  34. }}
  35. }}
  36. }}

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



    • Related Articles

    • Authentication - Serverless Functions

      Authentication Method A serverless function within your CRM can be invoked from any third-party application or within the CRM. However, commonly not all of the applications support a single authentication method. Keeping that in mind, we have two ...
    • Overview - Serverless Functions

      Serverless architecture, also known as “Function-as-a-service”(Faas), provides a platform for developers to execute their own codes in response of various business events. In Zoho CRM, all these codes can be written through deluge scripts and can be ...
    • Using OAuth2 - Serverless Functions

      Introduction Functions can be made accessible through OAuth2 protocol. OAuth2 method allows you to share specific data with any application while keeping your usernames and passwords private, by having specific scopes which grant access to specfic ...
    • Types of Inputs - Serverless Functions

      The input for the function can be acquired in the following types Parameters Parameters are passed in URL of query string and the data passed in the post "form-data" section in an input. The "params" in the crmAPIRequest map contains these parameters ...
    • Request and Response Object - Serverless Functions

      Request object You can get the entire Request Object within the function using the "crmAPIRequest" argument. Say you've created a function and defined 2 arguments. Now you need to use the same function in 2 different webhooks, each of which might ...