Skip to main content

Scala SDK Samples - Notification Operations

Enable Notifications
              
              
package com.zoho.crm.sample.notification

import java.lang.reflect.Field
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util

import com.zoho.crm.api.ParameterMap
import com.zoho.crm.api.notification.{APIException, ActionHandler, ActionResponse, ActionWrapper, BodyWrapper, Info, Notification, NotificationOperations, ResponseHandler, ResponseWrapper, SuccessResponse}
import com.zoho.crm.api.notification.NotificationOperations.DisableNotificationsParam
import com.zoho.crm.api.notification.NotificationOperations.GetNotificationDetailsParam
import com.zoho.crm.api.util.APIResponse
import com.zoho.crm.api.util.Model

import scala.collection.mutable.ArrayBuffer

object Notifications {
  /**
   *  Enable Notifications 
   * This method is used to Enable Notifications and print the response.
   *
   * @throws Exception
   */
  @throws[Exception]
  def enableNotifications(): Unit = { //Get instance of NotificationOperations Class
    val notificationOperations = new NotificationOperations
    //Get instance of BodyWrapper Class that will contain the request body
    val bodyWrapper = new BodyWrapper
    //List of Notification instances
    val notifications = new ArrayBuffer[Notification]
    //Get instance of Notification Class
    val notification = new Notification
    //Set channel Id of the Notification
    notification.setChannelId(Option(106800211l))
    val events = new ArrayBuffer[String]
    events.addOne("Deals.all")
    //To subscribe based on particular operations on given modules.
    notification.setEvents(events)
    //To set the expiry time for instant notifications.
    notification.setChannelExpiry(Option(OffsetDateTime.of(2020, 5, 2, 1, 0, 0, 0, ZoneOffset.of("+05:30"))))
    //To ensure that the notification is sent from Zoho CRM, by sending back the given value in notification URL body.
    //By using this value, user can validate the notifications.
    notification.setToken(Option("TOKEN_FOR_VERIFICATION_OF_1000000068002"))
    //URL to be notified (POST request)
    notification.setNotifyUrl(Option("https://www.zohoapis.com"))
    //Add Notification instance to the list
    notifications.addOne(notification)
    val notification2 = new Notification
    notification2.setChannelId(Option(106800211l))
    val events2 = new ArrayBuffer[String]
    events2.addOne("Accounts.all")
    notification2.setEvents(events2)
    notification2.setChannelExpiry(Option(OffsetDateTime.of(2020, 5, 2, 10, 0, 0, 1, ZoneOffset.of("+05:30"))))
    notification2.setToken(Option("TOKEN_FOR_VERIFICATION_OF_1000000068002"))
    notification2.setNotifyUrl(Option("https://www.zohoapis.com"))
    notifications.addOne(notification2)
    //Set the list to notifications in BodyWrapper instance
    bodyWrapper.setWatch(notifications)
    //Call enableNotifications method that takes BodyWrapper instance as parameter
    val responseOption= notificationOperations.enableNotifications(bodyWrapper)
    if (responseOption.isDefined ) { //Check response
      val response=responseOption.get
      println("Status Code: " + response.getStatusCode)
      //Check if expected response is received
      if (response.isExpected) { //Get object from response
        val actionHandler = response.getObject
        if (actionHandler.isInstanceOf[ActionWrapper]) { //Get the received ActionWrapper instance
          val actionWrapper = actionHandler.asInstanceOf[ActionWrapper]
          //Get the list of obtained ActionResponse instances
          val actionResponses = actionWrapper.getWatch

          for (actionResponse <- actionResponses) { //Check if the request is successful
            if (actionResponse.isInstanceOf[SuccessResponse]) { //Get the received SuccessResponse instance
              val successResponse = actionResponse.asInstanceOf[SuccessResponse]
              //Get the Status
              println("Status: " + successResponse.getStatus.getValue)
              //Get the Code
              println("Code: " + successResponse.getCode.getValue)
              println("Details: ")
              //Get the details map

              successResponse.getDetails.foreach(entry=>{

                if (entry._2.isInstanceOf[ArrayBuffer[Any]]) {
                  val dataList = entry._2.asInstanceOf[ArrayBuffer[Any]]
                  if (dataList.size > 0 && dataList(0).isInstanceOf[Notification]) {
                    @SuppressWarnings(Array("unchecked")) val eventList = dataList.asInstanceOf[ArrayBuffer[Notification]]

                    for (event <- eventList) {
                      println("Notification ChannelExpiry: " + event.getChannelExpiry)
                      println("Notification ResourceUri: " + event.getResourceUri)
                      println("Notification ResourceId: " + event.getResourceId)
                      println("Notification ResourceName: " + event.getResourceName)
                      println("Notification ChannelId: " + event.getChannelId)
                    }
                  }
                }
                else println(entry._1 + ": " + entry._2)
              })
              //Get the Message
              println("Message: " + successResponse.getMessage.getValue)
            }
            else { //Check if the request returned an exception
              if (actionResponse.isInstanceOf[APIException]) { //Get the received APIException instance
                val exception = actionResponse.asInstanceOf[APIException]
                println("Status: " + exception.getStatus.getValue)
                println("Code: " + exception.getCode.getValue)
                println("Details: ")

                exception.getDetails.foreach(entry=>{
                  println(entry._1 + ": " + entry._2)
                })
                println("Message: " + exception.getMessage.getValue)
              }
            }
          }
        }
        else if (actionHandler.isInstanceOf[APIException]) {
          val exception = actionHandler.asInstanceOf[APIException]
          println("Status: " + exception.getStatus.getValue)
          println("Code: " + exception.getCode.getValue)
          println("Details: ")

          exception.getDetails.foreach(entry=>{
            println(entry._1 + ": " + entry._2)
          })
          println("Message: " + exception.getMessage.getValue)
        }
      }
      else { //If response is not as expected
        //Get model object from response
        val responseObject = response.getModel
        //Get the response object's class
        val clas = responseObject.getClass
        //Get all declared fields of the response class
        val fields = clas.getDeclaredFields
        for (field <- fields) { //Get each value
          println(field.getName + ":" + field.get(responseObject))
        }
      }
    }
  }
}
 
Get Notification Details
Update Notifications
              
              
package com.zoho.crm.sample.notification

import java.lang.reflect.Field
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util

import com.zoho.crm.api.ParameterMap
import com.zoho.crm.api.notification.{APIException, ActionHandler, ActionResponse, ActionWrapper, BodyWrapper, Info, Notification, NotificationOperations, ResponseHandler, ResponseWrapper, SuccessResponse}
import com.zoho.crm.api.notification.NotificationOperations.DisableNotificationsParam
import com.zoho.crm.api.notification.NotificationOperations.GetNotificationDetailsParam
import com.zoho.crm.api.util.APIResponse
import com.zoho.crm.api.util.Model

import scala.collection.mutable.ArrayBuffer

object Notifications {
  /**
   *  Update Notifications 
   * This method is used to update Notifications and print the response.
   *
   * @throws Exception
   */
  @throws[Exception]
  def updateNotifications(): Unit = {
    val notificationOperations = new NotificationOperations
    val bodyWrapper = new BodyWrapper
    val notificationList = new ArrayBuffer[Notification]
    val notification = new Notification
    //Set ChannelId to the Notification instance
    notification.setChannelId(Option(100000006800211l))
    val events = new ArrayBuffer[String]
    events.addOne("Accounts.all")
    notification.setEvents(events)
    //Set name to the Notification instance
    //    notification.setChannelExpiry(OffsetDateTime.now)
    notification.setToken(Option("TOKEN_FOR_VERIFICATION_OF_168002"))
    notification.setNotifyUrl(Option("https://www.zohoapis.com"))
    notificationList.addOne(notification)
    //Set the list to notification in BodyWrapper instance
    bodyWrapper.setWatch(notificationList)
    //Call updateNotifications method that takes BodyWrapper instance as parameter
    val responseOption= notificationOperations.updateNotifications(bodyWrapper)
    if (responseOption.isDefined ) { //Check response
      val response=responseOption.get
      println("Status Code: " + response.getStatusCode)
      if (response.isExpected) {
        val actionHandler = response.getObject
        if (actionHandler.isInstanceOf[ActionWrapper]) {
          val actionWrapper = actionHandler.asInstanceOf[ActionWrapper]
          val actionResponses = actionWrapper.getWatch

          for (actionResponse <- actionResponses) {
            if (actionResponse.isInstanceOf[SuccessResponse]) {
              val successResponse = actionResponse.asInstanceOf[SuccessResponse]
              println("Status: " + successResponse.getStatus.getValue)
              println("Code: " + successResponse.getCode.getValue)
              println("Details: ")
              successResponse.getDetails.foreach(entry=>{

                if (entry._2.isInstanceOf[ArrayBuffer[Any]]) {
                  val dataList = entry._2.asInstanceOf[ArrayBuffer[Any]]
                  if (dataList.size > 0 && dataList(0).isInstanceOf[Notification]) {
                    @SuppressWarnings(Array("unchecked")) val eventList = dataList.asInstanceOf[ArrayBuffer[Notification]]

                    for (event <- eventList) {
                      println("Notification ChannelExpiry: " + event.getChannelExpiry)
                      println("Notification ResourceUri: " + event.getResourceUri)
                      println("Notification ResourceId: " + event.getResourceId)
                      println("Notification ResourceName: " + event.getResourceName)
                      println("Notification ChannelId: " + event.getChannelId)
                    }
                  }
                }
                else println(entry._1 + ": " + entry._2)
              })
              println("Message: " + successResponse.getMessage.getValue)
            }
            else if (actionResponse.isInstanceOf[APIException]) {
              val exception = actionResponse.asInstanceOf[APIException]
              println("Status: " + exception.getStatus.getValue)
              println("Code: " + exception.getCode.getValue)
              println("Details: ")

              exception.getDetails.foreach(entry=>{
                println(entry._1 + ": " + entry._2)
              })
              println("Message: " + exception.getMessage.getValue)
            }
          }
        }
        else if (actionHandler.isInstanceOf[APIException]) {
          val exception = actionHandler.asInstanceOf[APIException]
          println("Status: " + exception.getStatus.getValue)
          println("Code: " + exception.getCode.getValue)
          println("Details: ")

          exception.getDetails.foreach(entry=>{
            println(entry._1 + ": " + entry._2)
          })
          println("Message: " + exception.getMessage.getValue)
        }
      }
      else {
        val responseObject = response.getModel
        val clas = responseObject.getClass
        val fields = clas.getDeclaredFields
        for (field <- fields) {
          println(field.getName + ":" + field.get(responseObject))
        }
      }
    }
  }
}
 
Update Specific Information of a Notification
              
              
package com.zoho.crm.sample.notification

import java.lang.reflect.Field
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util

import com.zoho.crm.api.ParameterMap
import com.zoho.crm.api.notification.{APIException, ActionHandler, ActionResponse, ActionWrapper, BodyWrapper, Info, Notification, NotificationOperations, ResponseHandler, ResponseWrapper, SuccessResponse}
import com.zoho.crm.api.notification.NotificationOperations.DisableNotificationsParam
import com.zoho.crm.api.notification.NotificationOperations.GetNotificationDetailsParam
import com.zoho.crm.api.util.APIResponse
import com.zoho.crm.api.util.Model

import scala.collection.mutable.ArrayBuffer

object Notifications {
  /**
   *  Update Specific Information of a Notification 
   * This method is used to update single Notification and print the response.
   *
   * @throws Exception
   */
  @throws[Exception]
  def updateNotification(): Unit = {
    val notificationOperations = new NotificationOperations
    val bodyWrapper = new BodyWrapper
    val notificationList = new ArrayBuffer[Notification]
    val notification = new Notification
    notification.setChannelId(Option(10800211l))
    val events = new ArrayBuffer[String]
    events.addOne("Deals.all")
    notification.setEvents(events)
    //    notification.setChannelExpiry(OffsetDateTime.now)
    notification.setToken(Option("TOKEN_FOR_VERIFICATION_OF_1068002"))
    notification.setNotifyUrl(Option("https://www.zohoapis.com"))
    notificationList.addOne(notification)
    bodyWrapper.setWatch(notificationList)
    //Call updateNotification method that takes BodyWrapper instance as parameters
    val responseOption= notificationOperations.updateNotification(bodyWrapper)
    if (responseOption.isDefined ) { //Check response
      val response=responseOption.get
      println("Status Code: " + response.getStatusCode)
      if (response.isExpected) {
        val actionHandler = response.getObject
        if (actionHandler.isInstanceOf[ActionWrapper]) {
          val actionWrapper = actionHandler.asInstanceOf[ActionWrapper]
          val actionResponses = actionWrapper.getWatch

          for (actionResponse <- actionResponses) {
            if (actionResponse.isInstanceOf[SuccessResponse]) {
              val successResponse = actionResponse.asInstanceOf[SuccessResponse]
              println("Status: " + successResponse.getStatus.getValue)
              println("Code: " + successResponse.getCode.getValue)
              println("Details: ")

              successResponse.getDetails.foreach(entry=>{
                if (entry._2.isInstanceOf[ArrayBuffer[Any]]) {
                  val dataList = entry._2.asInstanceOf[ArrayBuffer[Any]]
                  if (dataList.size > 0 && dataList(0).isInstanceOf[Notification]) {
                    @SuppressWarnings(Array("unchecked")) val eventList = dataList.asInstanceOf[ArrayBuffer[Notification]]

                    for (event <- eventList) {
                      println("Notification ChannelExpiry: " + event.getChannelExpiry)
                      println("Notification ResourceUri: " + event.getResourceUri)
                      println("Notification ResourceId: " + event.getResourceId)
                      println("Notification ResourceName: " + event.getResourceName)
                      println("Notification ChannelId: " + event.getChannelId)
                    }
                  }
                }
                else println(entry._1 + ": " + entry._2)
              })
              println("Message: " + successResponse.getMessage.getValue)
            }
            else if (actionResponse.isInstanceOf[APIException]) {
              val exception = actionResponse.asInstanceOf[APIException]
              println("Status: " + exception.getStatus.getValue)
              println("Code: " + exception.getCode.getValue)
              println("Details: ")

              exception.getDetails.foreach(entry=>{
                println(entry._1 + ": " + entry._2)
              })
              println("Message: " + exception.getMessage.getValue)
            }
          }
        }
        else if (actionHandler.isInstanceOf[APIException]) {
          val exception = actionHandler.asInstanceOf[APIException]
          println("Status: " + exception.getStatus.getValue)
          println("Code: " + exception.getCode.getValue)
          println("Details: ")

          exception.getDetails.foreach(entry=>{
            println(entry._1 + ": " + entry._2)
          })
          println("Message: " + exception.getMessage.getValue)
        }
      }
      else {
        val responseObject = response.getModel
        val clas = responseObject.getClass
        val fields = clas.getDeclaredFields
        for (field <- fields) {
          println(field.getName + ":" + field.get(responseObject))
        }
      }
    }
  }
}
 
Disable Notifications
              
              
package com.zoho.crm.sample.notification

import java.lang.reflect.Field
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util

import com.zoho.crm.api.ParameterMap
import com.zoho.crm.api.notification.{APIException, ActionHandler, ActionResponse, ActionWrapper, BodyWrapper, Info, Notification, NotificationOperations, ResponseHandler, ResponseWrapper, SuccessResponse}
import com.zoho.crm.api.notification.NotificationOperations.DisableNotificationsParam
import com.zoho.crm.api.notification.NotificationOperations.GetNotificationDetailsParam
import com.zoho.crm.api.util.APIResponse
import com.zoho.crm.api.util.Model

import scala.collection.mutable.ArrayBuffer


object Notifications {
  /**
   *  Disable Notifications 
   * To stop all the instant notifications enabled by the user for a channel.
   *
   * @param channelIds - Specify the unique IDs of the notification channels to be disabled.
   * @throws Exception
   */
  @throws[Exception]
  def disableNotifications(channelIds: ArrayBuffer[Long]): Unit = { //example
    //ArrayList<Long> channelIds = new ArrayList<Long>(Arrays.asList(347706105208001l))
    val notificationOperations = new NotificationOperations
    //Get instance of ParameterMap Class
    val paramInstance = new ParameterMap

    for (id <- channelIds) {
      paramInstance.add(new DisableNotificationsParam().channelIds, id)
    }
    //Call disableNotifications method that takes paramInstance as parameter
    val responseOption= notificationOperations.disableNotifications(Option(paramInstance))
    if (responseOption.isDefined ) { //Check response
      val response=responseOption.get
      println("Status Code: " + response.getStatusCode)
      if (response.isExpected) {
        val actionHandler = response.getObject
        if (actionHandler.isInstanceOf[ActionWrapper]) {
          val actionWrapper = actionHandler.asInstanceOf[ActionWrapper]
          val actionResponses = actionWrapper.getWatch

          for (actionResponse <- actionResponses) {
            if (actionResponse.isInstanceOf[SuccessResponse]) {
              val successResponse = actionResponse.asInstanceOf[SuccessResponse]
              println("Status: " + successResponse.getStatus.getValue)
              println("Code: " + successResponse.getCode.getValue)
              println("Details: ")

              successResponse.getDetails.foreach(entry=>{
                println(entry._1 + ": " + entry._2)
              })
              println("Message: " + successResponse.getMessage.getValue)
            }
            else if (actionResponse.isInstanceOf[APIException]) {
              val exception = actionResponse.asInstanceOf[APIException]
              println("Status: " + exception.getStatus.getValue)
              println("Code: " + exception.getCode.getValue)
              println("Details: ")

              exception.getDetails.foreach(entry=>{
                println(entry._1 + ": " + entry._2)
              })
              println("Message: " + exception.getMessage.getValue)
            }
          }
        }
        else if (actionHandler.isInstanceOf[APIException]) {
          val exception = actionHandler.asInstanceOf[APIException]
          println("Status: " + exception.getStatus.getValue)
          println("Code: " + exception.getCode.getValue)
          println("Details: ")

          exception.getDetails.foreach(entry=>{
            println(entry._1 + ": " + entry._2)
          })
          println("Message: " + exception.getMessage.getValue)
        }
      }
      else {
        val responseObject = response.getModel
        val clas = responseObject.getClass
        val fields = clas.getDeclaredFields
        for (field <- fields) {
          println(field.getName + ":" + field.get(responseObject))
        }
      }
    }
  }
}
 
Disable Specific Notifications
              
              
package com.zoho.crm.sample.notification

import java.lang.reflect.Field
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util

import com.zoho.crm.api.ParameterMap
import com.zoho.crm.api.notification.{APIException, ActionHandler, ActionResponse, ActionWrapper, BodyWrapper, Info, Notification, NotificationOperations, ResponseHandler, ResponseWrapper, SuccessResponse}
import com.zoho.crm.api.notification.NotificationOperations.DisableNotificationsParam
import com.zoho.crm.api.notification.NotificationOperations.GetNotificationDetailsParam
import com.zoho.crm.api.util.APIResponse
import com.zoho.crm.api.util.Model

import scala.collection.mutable.ArrayBuffer

object Notifications {
  /**
   *  Disable Specific Notifications 
   * This method is used to disable notifications for the specified events in a channel.
   *
   * @throws Exception
   */
  @throws[Exception]
  def disableNotification(): Unit = {
    val notificationOperations = new NotificationOperations
    val bodyWrapper = new BodyWrapper
    val notificationList = new ArrayBuffer[Notification]
    val notification = new Notification
    notification.setChannelId(Option(106800211l))
    val events = new ArrayBuffer[String]
    events.addOne("Deals.edit")
    notification.setEvents(events)
    notification.setDeleteevents(Option(true))
    notificationList.addOne(notification)
    bodyWrapper.setWatch(notificationList)
    //Call disableNotification which takes BodyWrapper instance as parameter
    val responseOption= notificationOperations.disableNotification(bodyWrapper)
    if (responseOption.isDefined ) { //Check response
      val response=responseOption.get
      println("Status Code: " + response.getStatusCode)
      if (response.isExpected) {
        val actionHandler = response.getObject
        if (actionHandler.isInstanceOf[ActionWrapper]) {
          val actionWrapper = actionHandler.asInstanceOf[ActionWrapper]
          val actionResponses = actionWrapper.getWatch

          for (actionResponse <- actionResponses) {
            if (actionResponse.isInstanceOf[SuccessResponse]) {
              val successResponse = actionResponse.asInstanceOf[SuccessResponse]
              println("Status: " + successResponse.getStatus.getValue)
              println("Code: " + successResponse.getCode.getValue)
              println("Details: ")

              successResponse.getDetails.foreach(entry=>{
                println(entry._1 + ": " + entry._2)
              })
              println("Message: " + successResponse.getMessage.getValue)
            }
            else if (actionResponse.isInstanceOf[APIException]) {
              val exception = actionResponse.asInstanceOf[APIException]
              println("Status: " + exception.getStatus.getValue)
              println("Code: " + exception.getCode.getValue)
              println("Details: ")

              exception.getDetails.foreach(entry=>{
                println(entry._1 + ": " + entry._2)
              })
              println("Message: " + exception.getMessage.getValue)
            }
          }
        }
        else if (actionHandler.isInstanceOf[APIException]) {
          val exception = actionHandler.asInstanceOf[APIException]
          println("Status: " + exception.getStatus.getValue)
          println("Code: " + exception.getCode.getValue)
          println("Details: ")

          exception.getDetails.foreach(entry=>{
            println(entry._1 + ": " + entry._2)
          })
          println("Message: " + exception.getMessage.getValue)
        }
      }
      else {
        val responseObject = response.getModel
        val clas = responseObject.getClass
        val fields = clas.getDeclaredFields
        for (field <- fields) {
          println(field.getName + ":" + field.get(responseObject))
        }
      }
    }
  }
}