Building Extensions #14: Creating widgets with the JS SDK bundle in Zoho Desk - Hooks API

Building Extensions #14: Creating widgets with the JS SDK bundle in Zoho Desk - Hooks API

This series aims to equip developers with all they need to build extensions for Zoho Desk in Zoho Sigma and publish them in Zoho Marketplace.

Hello developers!

In our previous post, we explained the use of the resources API and provided an example of how to use them in a Zoho Desk extension. In this post, we'll continue with the Hooks API.

What is the hooks API? 

The hooks API allows developers to control the flow of UI actions through their extensions on a conditional basis. You'll need to subscribe to the hooks and, based on your requirements, you can allow the event to happen or reject it by passing either boolean values or promises in the event handler.
  • If the boolean value TRUE is passed/if the extension resolves the promise, the event will be executed.
  • If the boolean value FALSE is passed/if the extension rejects the promise, the event will be terminated and the reason for terminating the event will be displayed as an error message to end users.
Extensions with subscriptions to hooks must respond within 30 seconds of the event being triggered. If not, the control provided by the extension will be lost and the event will be executed.

What events are supported by the hooks API ?

Zoho Desk currently supports hooks for the following events:
  • Reopening a ticket
  • Closing a ticket
  • Changing the status of a ticket
  • Changing the assignee of a ticket
  • Adding a ticket comment
  • Editing a ticket comment
  • Sending a ticket reply
  • Closing a ticket on sending ticket reply
  • Updating the agent status in a channel
Click this link to find out more about the events and locations that are supported for every event within Zoho Desk.

Now that we have an understanding of the hooks API, including when and where it can be used, let's look at an example of how it can be used in an extension.

Defining the hooks API 

Below is the structure of the hooks API to be defined in your extension code.

      App.instance.on('name of the hook to be subscribed', function(){    
          //return promise or boolean   
      })

You can subscribe to the required hooks and include your required custom logic. The custom logic should either return TRUE or the promise needs to be resolved to allow the subscribed event to occur; otherwise the event occurrence will be blocked with a relevant message popping up that you provide.

Hooks related to ticket status, ticket comments, and other parameters will return data that can be used within your custom logic.  

Possible scenarios  

Let's create an extension that tracks the activities associated with a ticket in Zoho Desk. The extension monitors the activities (e.g., a call, task, or event) of Zoho Desk tickets and controls the trigger of actions based on the hooks defined.

In this example, we're configuring an extension to subscribe to the Zoho Desk hook defined for the close ticket event. With this extension, when the agent tries to close the ticket, the extension checks whether the activities mapped to the ticket are completed.

If they are completed, the extension will allow the ticket to close in Zoho Desk; otherwise, the close ticket event will not be executed and a relevant error message will be displayed. In addition, we're configuring the hook to consider the activity as completed for both cancelled and no activities as well in the ticket.

Use case implementation

To implement our use case, we'll perform the following steps. The code snippet is added for reference.
  • Obtain the activities associated with the current ticket
  • Obtain the status of the activities
  • Subscribe to the ticket status hook and handle the event handler according to the requirements
  • Subscribe a hook to close the ticket
Sample code
            window.onload = function () {
                        ZOHODESK.extension.onload().then(App=>{
                        var activityStatus;
                        ZOHODESK.get(['ticket.id']).then(function (res) {
                        var result = res.data;
                        ticketID=result["ticket.id"];

//Get the activities associated to the ticket
                       var reqObj= {
             url: 'https://desk.zoho.com/api/v1/tickets/'+ ticketID+'/activities',
             headers: {
                           'Content-Type': 'application/json'},
                           type: 'GET',
                           postBody: {},
                           connectionLinkName: "deskproject"
             }
             ZOHODESK.request( reqObj ).then(function(response){
                        var resultparse = JSON.parse(response);
                        var resultparse2=JSON.parse(resultparse.response).statusMessage;

//From the response obtain the status of the activities
                if(resultparse2!=null){
                   var resultparse3=resultparse2.data;
                   for (let i = 0; i < resultparse3.length; i++) {
                         activityStatus=resultparse3[i].status;                   }
                        }
                  })
            })

//Register a Hook for changing the status of a ticket
            App.instance.on('ticket.status', function (data) {
            return new Promise((resolve, reject) => {
            if ((data["ticket.newStatus"] == "Closed")){
            if((activityStatus=="Completed")||(activityStatus=="Canceled")||(activityStatus==null))
            {
                  return true;
            } else {
                  reject({ msg: "Incomplete Activity" });
                  }
            }
                  })
})

//Register a Hook for ticket close event
            App.instance.on('ticket.close', function () {
            if((activityStatus=="Completed")||(activityStatus=="Canceled")||(activityStatus==null))
            {
                  return true;
            } else {
                  reject({ msg: "Incomplete Activity" });
                  }
            })
                  })
            }

Result:



Here, you can see that the extension doesn't allow you to close the ticket because there is an open activity tagged. This is how the hooks API helps us to configure whether an event should happen or not, based on the hook configured.
 
We hope you found this post to be useful. Stay tuned for more posts in this space! 

See Also

      Zoho Marketing Automation
              • Sticky Posts

              • Zoho Desk Cheat Sheet For The Year-End

                Check out these Zoho Desk best practices to end this year on a high and have a great one ahead! #1 Set Business (Holiday) Hours - If you have limited working hours, please make sure you restrict your business hours or set them as holidays for the coming days. Let your customers know when you will, and won't, be available. #2 Update the Annual Holiday List - Check the holidays for the new year and update the holiday schedule. Usually, holidays from the current year will be carried over for the next
              • Deprecation of older versions of ASAP Mobile SDK | Zoho Desk

                Hello, everyone.    Greetings from Zoho Desk ASAP!   In order to continue to deliver the best and most secure experience to our mobile SDK users. On account of the recent enhancements and updates to the mobile SDKs, we have planned to mark the older versions
              • Zoho Desk: Scheduled Maintenance on 8 August 2021

                Hello, everyone! Warm greetings from the Zoho Desk team. We have planned scheduled maintenance activity on 8 August 2021, around 11:00 am IST. It might engender downtime from 0.25 to 4 minutes for a few customers in the US data center. We do not expect
              • Issue: Responses sent multiple times from Zoho Desk

                Hi All  Im afraid we are facing an issue with Desk where responses from Desk are being sent out multiple times. We have all hands on board and we are working on fixing this. I will be posting a follow up shortly as soon as this is fixed with the RCA.  Appreciate your patience in the meantime.  Regards Aarty
              • Layouts just got a whole lot more customizable!

                Dear Customers, Layouts help you organize your fields in each module. They help you quickly get to the information you care about the most. Until now, you could modify layouts and create custom fields only at the organization level. Today, we've made layouts more customizable. Each department can now have its own layout. Agents can create and use custom fields within each department, without interfering with those in another department. They can also configure department-specific picklist values

              Zoho Pagesense Resources

                Zoho SalesIQ Resources



                      Zoho TeamInbox Resources

                        Zoho DataPrep Resources



                          Zoho CRM Plus Resources

                            Zoho Books Resources


                              Zoho Subscriptions Resources

                                Zoho Desk Resources

                                  Zoho Projects Resources


                                    Zoho Sprints Resources


                                      Qntrl Resources


                                        Zoho Creator Resources


                                          Zoho WorkDrive Resources





                                              Zoho Campaigns Resources

                                                Zoho CRM Resources

                                                      Design. Discuss. Deliver.

                                                      Create visually engaging stories with Zoho Show.

                                                      Get Started Now