Skip to main content

Scala SDK Samples - Roles Operations

Get All Roles
Get a Specific Role
              
              
package com.zoho.crm.sample.role

import java.util
import com.zoho.crm.api.roles.{APIException, ResponseWrapper, RolesOperations}

object Roles {
  /**
   *  Get Role 
   * This method is used to retrieve the data of single role through an API request and print the response.
   *
   * @param roleId The ID of the Role to be obtained
   * @throws Exception
   */
  @throws[Exception]
  def getRole(roleId: Long): Unit = { //example
    //Long roleId = 347706103881
    val rolesOperations = new RolesOperations
    //Call getRole method that takes roleId as parameter
    val responseOption = rolesOperations.getRole(roleId)
    if (responseOption.isDefined) { //check response
      var response= responseOption.get
      println("Status Code: " + response.getStatusCode)
      if (util.Arrays.asList(204, 304).contains(response.getStatusCode)) {
        println(if (response.getStatusCode == 204) "No Content"
        else "Not Modified")
        return
      }
      if (response.isExpected) {
        val responseHandler = response.getObject
        if (responseHandler.isInstanceOf[ResponseWrapper]) {
          val responseWrapper = responseHandler.asInstanceOf[ResponseWrapper]
          val roles = responseWrapper.getRoles
          for (role <- roles) {
            println("Role DisplayLabel: " + role.getDisplayLabel)
            val forecastManagerOption = role.getForecastManager
            //Check if forecastManager is not null
            if (forecastManagerOption.isDefined) { //Get the ID of the forecast Manager
              val forecastManager= forecastManagerOption.get
              println("Role Forecast Manager User-ID: " + forecastManager.getId)
              //Get the name of the forecast Manager
              println("Role Forecast Manager User-Name: " + forecastManager.getName)
            }
            println("Role ShareWithPeers: " + role.getShareWithPeers.toString)
            println("Role Name: " + role.getName)
            println("Role Description: " + role.getDescription)
            println("Role ID: " + role.getId)
            val reportingToOption = role.getReportingTo
            //Check if reportingTo is not null
            if (reportingToOption.isDefined) { //Get the ID of the reportingTo User
              val reportingTo = reportingToOption.get
              println("Role ReportingTo User-ID: " + reportingTo.getId)
              //Get the name of the reportingTo User
              println("Role ReportingTo User-Name: " + reportingTo.getName)
            }
            println("Role AdminUser: " + role.getAdminUser.toString)
          }
        }
        else if (responseHandler.isInstanceOf[APIException]) {
          val exception = responseHandler.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))
        }
      }
    }
  }
}