Scala SDK Samples - Query (COQL) Operations

Get Records through a COQL Query
              
              
package com.zoho.crm.sample.query

import java.lang.reflect.Field
import java.util

import com.zoho.crm.api.query.ResponseHandler
import com.zoho.crm.api.query.ResponseWrapper
import com.zoho.crm.api.record.Info
import com.zoho.crm.api.query.APIException
import com.zoho.crm.api.query.BodyWrapper
import com.zoho.crm.api.query.QueryOperations
import com.zoho.crm.api.util.APIResponse
import com.zoho.crm.api.util.Model

import scala.collection.mutable.ArrayBuffer


object Queries {
  /**
   *  Get Records 
   * This method is used to get records from the module through a COQL query.
   *
   * @throws Exception
   */
  @SuppressWarnings(Array("unchecked"))
  @throws[Exception]
  def getRecords(): Unit = { //Get instance of QueryOperations Class
    val queryOperations = new QueryOperations
    //Get instance of BodyWrapper Class that will contain the request body
    val bodyWrapper = new BodyWrapper
    val selectQuery = "select Last_Name from Leads where Last_Name is not null limit 10"
    bodyWrapper.setSelectQuery(Option(selectQuery))
    //Call getRecords method that takes BodyWrapper instance as parameter
    val responseOption = queryOperations.getRecords(bodyWrapper)
    if (responseOption.isDefined) { //check response
      var response= responseOption.get
      println("Status Code: " + response.getStatusCode)
      //Check if expected response is received
      if (response.isExpected) { //Get the object from response
        val responseHandler = response.getObject
        if (responseHandler.isInstanceOf[ResponseWrapper]) { //Get the received ResponseWrapper instance
          val responseWrapper = responseHandler.asInstanceOf[ResponseWrapper]
          //Get the obtained Record instances
          val records = responseWrapper.getData
          for (record {
              val keyName = entry._1
              val value = entry._2
              if (value.isInstanceOf[ArrayBuffer[Any]]) {
                println("Record KeyName : " + keyName)
                val dataList = value.asInstanceOf[ArrayBuffer[Any]]
                for (data {
                      println(entry._1 + ": " + entry._2)
                    })

                  }
                  else println(data)
                }
              }
              else if (value.isInstanceOf[util.Map[_, _]]) {
                println("Record KeyName : " + keyName + " - Value : ")
                value.asInstanceOf[collection.Map[String,Any]].foreach(entry=>{
                  println(entry._1 + ": " + entry._2)
                })

              }
              else println("Record KeyName : " + keyName + " - Value : " + value)
            })
          }
          //Get the Object obtained Info instance
          val infoOption = responseWrapper.getInfo
          //Check if info is not null
          if (infoOption.isDefined) {
            val info = infoOption.get
            if (info.getCount != null) { //Get the Count of the Info
              println("Record Info Count: " + info.getCount.toString)
            }
            if (info.getMoreRecords != null) { //Get the MoreRecords of the Info
              println("Record Info MoreRecords: " + info.getMoreRecords.toString)
            }
          }
        }
        else { //Check if the request returned an exception
          if (responseHandler.isInstanceOf[APIException]) { //Get the received APIException instance
            val exception = responseHandler.asInstanceOf[APIException]
            //Get the Status
            println("Status: " + exception.getStatus.getValue)
            //Get the Code
            println("Code: " + exception.getCode.getValue)
            println("Details: ")
            //Get the details map
            exception.getDetails.foreach(entry=>{
              println(entry._1 + ": " + entry._2)
            })
            //Get the Message
            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