Scala SDK Samples - Related Records Operations

Get Related Records

              
package com.zoho.crm.sample.relatedrecords

import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.io.OutputStream
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util

import com.zoho.crm.api.HeaderMap
import com.zoho.crm.api.ParameterMap
import com.zoho.crm.api.attachments.Attachment
import com.zoho.crm.api.layouts.Layout
import com.zoho.crm.api.record.{Comment, Consent, FileDetails, LineTax, Participants, PricingDetails, Record, RecurringActivity, RemindAt}
import com.zoho.crm.api.relatedrecords.FileBodyWrapper
import com.zoho.crm.api.relatedrecords.APIException
import com.zoho.crm.api.relatedrecords.ActionHandler
import com.zoho.crm.api.relatedrecords.ActionResponse
import com.zoho.crm.api.relatedrecords.ActionWrapper
import com.zoho.crm.api.relatedrecords.BodyWrapper
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.DelinkRecordsParam
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsParam
import com.zoho.crm.api.relatedrecords.ResponseHandler
import com.zoho.crm.api.relatedrecords.ResponseWrapper
import com.zoho.crm.api.relatedrecords.SuccessResponse
import com.zoho.crm.api.tags.Tag
import com.zoho.crm.api.users.User
import com.zoho.crm.api.util.APIResponse
import com.zoho.crm.api.util.Choice
import com.zoho.crm.api.util.Model
import com.zoho.crm.api.util.StreamWrapper

import scala.collection.mutable.ArrayBuffer


object Relatedrecord {
/**
   *  Get Related Records 
   * This method is used to get the related list records and print the response.
   *
   * @param moduleAPIName      - The API Name of the module to get related records.
   * @param recordId           - The ID of the record to be obtained.
   * @param relatedListAPIName - The API name of the related list. To get the API name of the related list.
   * @throws Exception
   */
  @throws[Exception]
  def getRelatedRecords(moduleAPIName: String, recordId: Long, relatedListAPIName: String): Unit = { //example
    //Get instance of RelatedRecordsOperations Class that takes moduleAPIName, recordId and relatedListAPIName as parameter
    val relatedRecordsOperations = new RelatedRecordsOperations(relatedListAPIName, moduleAPIName)
    //Get instance of ParameterMap Class
    val paramInstance = new ParameterMap
    //paramInstance.add(new GetRelatedRecordsParam().page, 1)
    //    paramInstance.add(new GetRelatedRecordsParam().perPage, 2)
    //Get instance of HeaderMap Class
    val headerInstance = new HeaderMap
    //    val startdatetime = OffsetDateTime.of(2019, 6, 1, 10, 0, 1, 0, ZoneOffset.of("+05:30"))
    //    headerInstance.add(new GetRelatedRecordsHeader().IfModifiedSince, startdatetime)
    //Call getRelatedRecords method that takes paramInstance, headerInstance as parameter
    val responseOption = relatedRecordsOperations.getRelatedRecords(recordId, Option(paramInstance),Option( headerInstance))
    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
      }
      //Check if expected response is received
      if (response.isExpected) { //Get object from response
        val responseHandler = response.getObject
        responseHandler match {
          case responseWrapper: ResponseWrapper => //Get the received ResponseWrapper instance
            //Get the list of obtained Record instances
            val records = responseWrapper.getData

            for (record <- records) { //Get the ID of each Record
              println("RelatedRecord ID: " + record.getId)
              //Get the createdBy User instance of each Record
              var createdByOption = record.getCreatedBy
              //Check if createdBy is not null
              if (createdByOption.isDefined) { //Get the ID of the createdBy User
                var createdBy = createdByOption.get
                println("RelatedRecord Created By User-ID: " + createdBy.getId)
                //Get the name of the createdBy User
                println("RelatedRecord Created By User-Name: " + createdBy.getName)
                //Get the Email of the createdBy User
                println("RelatedRecord Created By User-Email: " + createdBy.getEmail)
              }
              //Get the CreatedTime of each Record
              println("RelatedRecord CreatedTime: " + record.getCreatedTime)
              //Get the modifiedBy User instance of each Record
              var modifiedByOption = record.getModifiedBy
              //Check if modifiedBy is not null
              if (modifiedByOption.isDefined) { //Get the ID of the modifiedBy User
                var modifiedBy = modifiedByOption.get
                println("RelatedRecord Modified By User-ID: " + modifiedBy.getId)
                //Get the name of the modifiedBy User
                println("RelatedRecord Modified By User-Name: " + modifiedBy.getName)
                //Get the Email of the modifiedBy User
                println("RelatedRecord Modified By User-Email: " + modifiedBy.getEmail)
              }
              //Get the ModifiedTime of each Record
              println("RelatedRecord ModifiedTime: " + record.getModifiedTime)
              //Get the list of Tag instance each Record
              val tags = record.getTag
              //Check if tags is not null
              if (tags.length > 0) {

                for (tag <- tags) { //Get the Name of each Tag
                  println("RelatedRecord Tag Name: " + tag.getName)
                  //Get the Id of each Tag
                  println("RelatedRecord Tag ID: " + tag.getId)
                }
              }
              //To get particular field value
              println("RelatedRecord Field Value: " + record.getKeyValue("Last_Name")) // FieldApiName

              println("RelatedRecord KeyValues: ")
              //Get the KeyValue map
              record.getKeyValues.foreach(entry => {
                val keyName = entry._1
                var value = entry._2
                value match {
                  case value1: Option[_] => value = value1.getOrElse(null)
                  case _ =>
                }
                value match {
                  case dataList: ArrayBuffer[Any] =>
                    if (dataList.size > 0) dataList(0) match {
                      case _: FileDetails =>
                        @SuppressWarnings(Array("unchecked")) val fileDetails = value.asInstanceOf[ArrayBuffer[FileDetails]]

                        for (fileDetail <- fileDetails) { //Get the Extn of each FileDetails
                          println("RelatedRecord FileDetails Extn: " + fileDetail.getExtn)
                          //Get the IsPreviewAvailable of each FileDetails
                          println("RelatedRecord FileDetails IsPreviewAvailable: " + fileDetail.getIsPreviewAvailable)
                          //Get the DownloadUrl of each FileDetails
                          println("RelatedRecord FileDetails DownloadUrl: " + fileDetail.getDownloadUrl)
                          //Get the DeleteUrl of each FileDetails
                          println("RelatedRecord FileDetails DeleteUrl: " + fileDetail.getDeleteUrl)
                          //Get the EntityId of each FileDetails
                          println("RelatedRecord FileDetails EntityId: " + fileDetail.getEntityId)
                          //Get the Mode of each FileDetails
                          println("RelatedRecord FileDetails Mode: " + fileDetail.getMode)
                          //Get the OriginalSizeByte of each FileDetails
                          println("RelatedRecord FileDetails OriginalSizeByte: " + fileDetail.getOriginalSizeByte)
                          //Get the PreviewUrl of each FileDetails
                          println("RelatedRecord FileDetails PreviewUrl: " + fileDetail.getPreviewUrl)
                          //Get the FileName of each FileDetails
                          println("RelatedRecord FileDetails FileName: " + fileDetail.getFileName)
                          //Get the FileId of each FileDetails
                          println("RelatedRecord FileDetails FileId: " + fileDetail.getFileId)
                          //Get the AttachmentId of each FileDetails
                          println("RelatedRecord FileDetails AttachmentId: " + fileDetail.getAttachmentId)
                          //Get the FileSize of each FileDetails
                          println("RelatedRecord FileDetails FileSize: " + fileDetail.getFileSize)
                          //Get the CreatorId of each FileDetails
                          println("RelatedRecord FileDetails CreatorId: " + fileDetail.getCreatorId)
                          //Get the LinkDocs of each FileDetails
                          println("RelatedRecord FileDetails LinkDocs: " + fileDetail.getLinkDocs)
                        }
                      case _: Choice[_] =>
                        @SuppressWarnings(Array("unchecked")) val choiceList = dataList.asInstanceOf[(ArrayBuffer[Choice[_$1]]) forSome {type _$1}]
                        println(keyName)
                        println("values")

                        for (choice <- choiceList) {
                          println(choice.getValue)
                        }
                      case _: Tag =>
                        @SuppressWarnings(Array("unchecked")) val tagList = value.asInstanceOf[ArrayBuffer[Tag]]

                        for (tag <- tagList) {
                          println("RelatedRecord Tag Name: " + tag.getName)
                          println("RelatedRecord Tag ID: " + tag.getId)
                        }
                      case _: PricingDetails =>
                        @SuppressWarnings(Array("unchecked")) val pricingDetails = value.asInstanceOf[ArrayBuffer[PricingDetails]]

                        for (pricingDetail <- pricingDetails) {
                          println("RelatedRecord PricingDetails ToRange: " + pricingDetail.getToRange.toString)
                          println("RelatedRecord PricingDetails Discount: " + pricingDetail.getDiscount.toString)
                          println("RelatedRecord PricingDetails ID: " + pricingDetail.getId)
                          println("RelatedRecord PricingDetails FromRange: " + pricingDetail.getFromRange.toString)
                        }
                      case _: Participants =>
                        val participants = value.asInstanceOf[ArrayBuffer[Participants]]
                        for (participant <- participants) {
                          println("RelatedRecord Participants Name: " + participant.getName().toString)
                          println("RelatedRecord Participants Invited: " + participant.getInvited().toString())
                          println("RelatedRecord Participants ID: " + participant.getId().toString)
                          println("RelatedRecord Participants Type: " + participant.getType().toString)
                          println("RelatedRecord Participants Participant: " + participant.getParticipant().toString)
                          println("RelatedRecord Participants Status: " + participant.getStatus().toString)
                        }
                      case _: Record =>
                        @SuppressWarnings(Array("unchecked")) val recordList = dataList.asInstanceOf[ArrayBuffer[Record]]

                        for (record1 <- recordList) { //Get the details map
                          record1.getKeyValues.foreach(entry => {
                            println(entry._1 + ": " + entry._2)
                          })
                        }
                      case _: LineTax =>
                        @SuppressWarnings(Array("unchecked")) val lineTaxes = dataList.asInstanceOf[ArrayBuffer[LineTax]]

                        for (lineTax <- lineTaxes) {
                          println("RelatedRecord ProductDetails LineTax Percentage: " + lineTax.getPercentage.toString)
                          println("RelatedRecord ProductDetails LineTax Name: " + lineTax.getName)
                          println("RelatedRecord ProductDetails LineTax Id: " + lineTax.getId)
                          println("RelatedRecord ProductDetails LineTax Value: " + lineTax.getValue.toString)
                        }
                      case _: Comment =>
                        @SuppressWarnings(Array("unchecked")) val comments = dataList.asInstanceOf[ArrayBuffer[Comment]]

                        for (comment <- comments) {
                          println("RelatedRecord Comment CommentedBy: " + comment.getCommentedBy)
                          println("RelatedRecord Comment CommentedTime: " + comment.getCommentedTime.toString)
                          println("RelatedRecord Comment CommentContent: " + comment.getCommentContent)
                          println("RelatedRecord Comment Id: " + comment.getId)
                        }
                      case _: Attachment => //Get the list of obtained Attachment instances
                        @SuppressWarnings(Array("unchecked")) val attachments = dataList.asInstanceOf[ArrayBuffer[Attachment]]

                        for (attachment <- attachments) { //Get the owner User instance of each attachment
                          val ownerOption = attachment.getOwner
                          //Check if owner is not null
                          if (ownerOption.isDefined) { //Get the Name of the Owner
                            var owner = ownerOption.get
                            println("RelatedRecord Attachment Owner User-Name: " + owner.getName)
                            //Get the ID of the Owner
                            println("RelatedRecord Attachment Owner User-ID: " + owner.getId)
                            //Get the Email of the Owner
                            println("RelatedRecord Attachment Owner User-Email: " + owner.getEmail)
                          }
                          //Get the modified time of each attachment
                          println("RelatedRecord Attachment Modified Time: " + attachment.getModifiedTime.toString)
                          //Get the name of the File
                          println("RelatedRecord Attachment File Name: " + attachment.getFileName)
                          //Get the created time of each attachment
                          println("RelatedRecord Attachment Created Time: " + attachment.getCreatedTime.toString)
                          //Get the Attachment file size
                          println("RelatedRecord Attachment File Size: " + attachment.getSize.toString)
                          //Get the parentId Record instance of each attachment
                          val parentIdOption = attachment.getParentId
                          //Check if parentId is not null
                          if (parentIdOption.isDefined) { //Get the parent record Name of each attachment
                            var parentId = parentIdOption.get
                            println("RelatedRecord Attachment parent record Name: " + parentId.getKeyValue("name"))
                            //Get the parent record ID of each attachment
                            println("RelatedRecord Attachment parent record ID: " + parentId.getId)
                          }
                          //Get the attachment is Editable
                          println("RelatedRecord Attachment is Editable: " + attachment.geteditable().toString)
                          //Get the file ID of each attachment
                          println("RelatedRecord Attachment File ID: " + attachment.getfileId())
                          //Get the type of each attachment
                          println("RelatedRecord Attachment File Type: " + attachment.gettype)
                          //Get the seModule of each attachment
                          println("RelatedRecord Attachment seModule: " + attachment.getseModule())
                          //Get the modifiedBy User instance of each attachment
                          var modifiedByOption = attachment.getModifiedBy
                          if (modifiedByOption.isDefined) { //Get the Name of the modifiedBy User
                            var modifiedBy = modifiedByOption.get
                            println("RelatedRecord Attachment Modified By User-Name: " + modifiedBy.getName)
                            println("RelatedRecord Attachment Modified By User-ID: " + modifiedBy.getId)
                            println("RelatedRecord Attachment Modified By User-Email: " + modifiedBy.getEmail)
                          }
                          //Get the state of each attachment
                          println("RelatedRecord Attachment State: " + attachment.getstate)
                          //Get the ID of each attachment
                          println("RelatedRecord Attachment ID: " + attachment.getId)
                          //Get the createdBy User instance of each attachment
                          var createdByOption = attachment.getCreatedBy
                          if (createdByOption.isDefined) {
                            var createdBy = modifiedByOption.get

                            println("RelatedRecord Attachment Created By User-Name: " + createdBy.getName)
                            println("RelatedRecord Attachment Created By User-ID: " + createdBy.getId)
                            println("RelatedRecord Attachment Created By User-Email: " + createdBy.getEmail)
                          }
                          //Get the linkUrl of each attachment
                          println("RelatedRecord Attachment LinkUrl: " + attachment.getlinkUrl())
                        }
                      case _ => println(keyName + ": " + value)
                    }
                  case recordValue: Record =>
                    println("RelatedRecord " + keyName + " ID: " + recordValue.getId)
                    println("RelatedRecord " + keyName + " Name: " + recordValue.getKeyValue("name"))
                  case layout: Layout =>
                    if (layout != null) {
                      println("RelatedRecord " + keyName + " ID: " + layout.getId)
                      println("RelatedRecord " + keyName + " Name: " + layout.getName)
                    }
                  case user: User =>
                    if (user != null) {
                      println("RelatedRecord " + keyName + " User-ID: " + user.getId)
                      println("RelatedRecord " + keyName + " User-Name: " + user.getName)
                      println("RelatedRecord " + keyName + " User-Email: " + user.getEmail)
                    }
                  case value1: Choice[_] => println(keyName + ": " + value1.getValue)
                  case at: RemindAt => println(keyName + ": " + at.getAlarm)
                  case activity: RecurringActivity =>
                    println(keyName)
                    println("RRULE" + ": " + activity.getRrule)
                  case consent: Consent =>
                    println("RelatedRecord Consent ID: " + consent.getId)
                    //Get the Owner User instance of each attachment
                    val ownerOption = consent.getOwner
                    if (ownerOption.isDefined) { //Get the name of the owner User
                      var owner = ownerOption.get
                      println("RelatedRecord Consent Owner Name: " + owner.getName)
                      //Get the ID of the owner User
                      println("RelatedRecord Consent Owner ID: " + owner.getId)
                      //Get the Email of the owner User
                      println("RelatedRecord Consent Owner Email: " + owner.getEmail)
                    }
                    val consentCreatedByOption = consent.getCreatedBy
                    if (consentCreatedByOption.isDefined) { //Get the name of the CreatedBy User
                      var consentCreatedBy = consentCreatedByOption.get
                      println("RelatedRecord Consent CreatedBy Name: " + consentCreatedBy.getName)
                      //Get the ID of the CreatedBy User
                      println("RelatedRecord Consent CreatedBy ID: " + consentCreatedBy.getId)
                      //Get the Email of the CreatedBy User
                      println("RelatedRecord Consent CreatedBy Email: " + consentCreatedBy.getEmail)
                    }
                    val consentModifiedByOption = consent.getModifiedBy
                    if (consentModifiedByOption.isDefined) { //Get the name of the ModifiedBy User
                      var consentModifiedBy = consentModifiedByOption.get
                      println("RelatedRecord Consent ModifiedBy Name: " + consentModifiedBy.getName)
                      //Get the ID of the ModifiedBy User
                      println("RelatedRecord Consent ModifiedBy ID: " + consentModifiedBy.getId)
                      //Get the Email of the ModifiedBy User
                      println("RelatedRecord Consent ModifiedBy Email: " + consentModifiedBy.getEmail)
                    }
                    println("RelatedRecord Consent CreatedTime: " + consent.getCreatedTime)
                    println("RelatedRecord Consent ModifiedTime: " + consent.getModifiedTime)
                    println("RelatedRecord Consent ContactThroughEmail: " + consent.getContactThroughEmail)
                    println("RelatedRecord Consent ContactThroughSocial: " + consent.getContactThroughSocial)
                    println("RelatedRecord Consent ContactThroughSurvey: " + consent.getContactThroughSurvey)
                    println("RelatedRecord Consent ContactThroughPhone: " + consent.getContactThroughPhone)
                    println("RelatedRecord Consent MailSentTime: " + consent.getMailSentTime.toString)
                    println("RelatedRecord Consent ConsentDate: " + consent.getConsentDate.toString)
                    println("RelatedRecord Consent ConsentRemarks: " + consent.getConsentRemarks)
                    println("RelatedRecord Consent ConsentThrough: " + consent.getConsentThrough)
                    println("RelatedRecord Consent DataProcessingBasis: " + consent.getDataProcessingBasis)
                    //To get custom values
                    println("RelatedRecord Consent Lawful Reason: " + consent.getKeyValue("Lawful_Reason"))
                  case _ => println(keyName + ": " + value)
                }
              })
            }
          case _ => //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: ")

              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 <- fields) { //Get each value
          println(field.getName + ":" + field.get(responseObject))
        }
      }
    }
  }
}
class Relatedrecord {}
 
Update Related Records

              
package com.zoho.crm.sample.relatedrecords

import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.io.OutputStream
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util

import com.zoho.crm.api.HeaderMap
import com.zoho.crm.api.ParameterMap
import com.zoho.crm.api.attachments.Attachment
import com.zoho.crm.api.layouts.Layout
import com.zoho.crm.api.record.{Comment, Consent, FileDetails, LineTax, Participants, PricingDetails, Record, RecurringActivity, RemindAt}
import com.zoho.crm.api.relatedrecords.FileBodyWrapper
import com.zoho.crm.api.relatedrecords.APIException
import com.zoho.crm.api.relatedrecords.ActionHandler
import com.zoho.crm.api.relatedrecords.ActionResponse
import com.zoho.crm.api.relatedrecords.ActionWrapper
import com.zoho.crm.api.relatedrecords.BodyWrapper
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.DelinkRecordsParam
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsParam
import com.zoho.crm.api.relatedrecords.ResponseHandler
import com.zoho.crm.api.relatedrecords.ResponseWrapper
import com.zoho.crm.api.relatedrecords.SuccessResponse
import com.zoho.crm.api.tags.Tag
import com.zoho.crm.api.users.User
import com.zoho.crm.api.util.APIResponse
import com.zoho.crm.api.util.Choice
import com.zoho.crm.api.util.Model
import com.zoho.crm.api.util.StreamWrapper

import scala.collection.mutable.ArrayBuffer

object Relatedrecord {
/**
   *  Update Related Records 
   * This method is used to update the relation between the records and print the response.
   *
   * @param moduleAPIName      - The API Name of the module to update related records.
   * @param recordId           - The ID of the record to be obtained.
   * @param relatedListAPIName - The API name of the related list. To get the API name of the related list.
   * @throws Exception
   */
  @throws[Exception]
  def updateRelatedRecords(moduleAPIName: String, recordId: Long, relatedListAPIName: String): Unit = { //API Name of the module
    val relatedRecordsOperations = new RelatedRecordsOperations(relatedListAPIName, moduleAPIName)
    //Get instance of BodyWrapper Class that will contain the request body
    val request = new BodyWrapper
    //List of Record instances
    val records = new ArrayBuffer[Record]
    //Get instance of Record Class
    val record1 = new Record
    /*
     * Call addKeyValue method that takes two arguments
     * 1 -> A string that is the Field's API Name
     * 2 -> Value
     */
    record1.addKeyValue("id", 3477061000013627001l)
    record1.addKeyValue("Note_Content", "asd")
    //Add Record instance to the list
    records.addOne(record1)
    val record2 = new Record
    record2.addKeyValue("id", 3477061000013412057l)
    record2.addKeyValue("Note_Content", "asd")
    records.addOne(record2)
    //Set the list to Records in BodyWrapper instance
    request.setData(records)
    //Call updateRelatedRecords method that takes BodyWrapper instance as parameter.
    val responseOption = relatedRecordsOperations.updateRelatedRecords(recordId, request)
    if (responseOption.isDefined) {
      var response = responseOption.get
      println("Status Code: " + response.getStatusCode)
      if (response.isExpected) {
        val actionHandler = response.getObject
        actionHandler match {
          case actionWrapper: ActionWrapper => //Get the received ActionWrapper instance
            //Get the list of obtained ActionResponse instances
            val actionResponses = actionWrapper.getData

            for (actionResponse <- actionResponses) { //Check if the request is successful
              actionResponse match {
                case successResponse: SuccessResponse => //Get the received SuccessResponse instance
                  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)
                case exception: 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)
                case _ =>
              }
            }
          case exception: 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)
          case _ =>
        }
      }
      else {
        val responseObject = response.getModel
        val clas = responseObject.getClass
        val fields = clas.getDeclaredFields
        for (field <- fields) {
          println(field.getName + ":" + field.get(responseObject))
        }
      }
    }
  }
}
class Relatedrecord {}
 

              
package com.zoho.crm.sample.relatedrecords

import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.io.OutputStream
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util

import com.zoho.crm.api.HeaderMap
import com.zoho.crm.api.ParameterMap
import com.zoho.crm.api.attachments.Attachment
import com.zoho.crm.api.layouts.Layout
import com.zoho.crm.api.record.{Comment, Consent, FileDetails, LineTax, Participants, PricingDetails, Record, RecurringActivity, RemindAt}
import com.zoho.crm.api.relatedrecords.FileBodyWrapper
import com.zoho.crm.api.relatedrecords.APIException
import com.zoho.crm.api.relatedrecords.ActionHandler
import com.zoho.crm.api.relatedrecords.ActionResponse
import com.zoho.crm.api.relatedrecords.ActionWrapper
import com.zoho.crm.api.relatedrecords.BodyWrapper
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.DelinkRecordsParam
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsParam
import com.zoho.crm.api.relatedrecords.ResponseHandler
import com.zoho.crm.api.relatedrecords.ResponseWrapper
import com.zoho.crm.api.relatedrecords.SuccessResponse
import com.zoho.crm.api.tags.Tag
import com.zoho.crm.api.users.User
import com.zoho.crm.api.util.APIResponse
import com.zoho.crm.api.util.Choice
import com.zoho.crm.api.util.Model
import com.zoho.crm.api.util.StreamWrapper

import scala.collection.mutable.ArrayBuffer


object Relatedrecord {
/**
   *  Delink Records 
   * This method is used to delete the association between modules and print the response.
   *
   * @param moduleAPIName      - The API Name of the module to delink related records.
   * @param recordId           - The ID of the record
   * @param relatedListAPIName - The API name of the related list. To get the API name of the related list.
   * @param relatedListIds     - The List of related record IDs.
   * @throws Exception
   */
  @throws[Exception]
  def delinkRecords(moduleAPIName: String, recordId: Long, relatedListAPIName: String, relatedListIds: ArrayBuffer[String]): Unit = { //List relatedListIds = new ArrayList(Arrays.asList(3477061000005919001l, 3477061000005917011l))
    val relatedRecordsOperations = new RelatedRecordsOperations(relatedListAPIName, moduleAPIName)
    val paramInstance = new ParameterMap
     
    for (relatedListId <- relatedListIds) {
      paramInstance.add(new DelinkRecordsParam().ids, relatedListId)
    }
    //Call delinkRecords method that takes paramInstance instance as parameter.
    val responseOption = relatedRecordsOperations.delinkRecords(recordId, Option(paramInstance))
    if (responseOption.isDefined) {
      var response= responseOption.get
      println("Status Code: " + response.getStatusCode)
      if (response.isExpected) {
        val actionHandler = response.getObject
        actionHandler match {
          case actionWrapper: ActionWrapper =>
            val actionResponses = actionWrapper.getData

            for (actionResponse <- actionResponses) {
              actionResponse match {
                case successResponse: 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)
                case exception: 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)
                case _ =>
              }
            }
          case exception: 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)
          case _ =>
        }
      }
      else {
        val responseObject = response.getModel
        val clas = responseObject.getClass
        val fields = clas.getDeclaredFields
        for (field <- fields) {
          println(field.getName + ":" + field.get(responseObject))
        }
      }
    }
  }
}
class Relatedrecord {}
 
Get Related Records Using External ID

              
package com.zoho.crm.sample.relatedrecords

import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.io.OutputStream
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util

import com.zoho.crm.api.HeaderMap
import com.zoho.crm.api.ParameterMap
import com.zoho.crm.api.attachments.Attachment
import com.zoho.crm.api.layouts.Layout
import com.zoho.crm.api.record.{Comment, Consent, FileDetails, LineTax, Participants, PricingDetails, Record, RecurringActivity, RemindAt}
import com.zoho.crm.api.relatedrecords.FileBodyWrapper
import com.zoho.crm.api.relatedrecords.APIException
import com.zoho.crm.api.relatedrecords.ActionHandler
import com.zoho.crm.api.relatedrecords.ActionResponse
import com.zoho.crm.api.relatedrecords.ActionWrapper
import com.zoho.crm.api.relatedrecords.BodyWrapper
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.DelinkRecordsParam
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsParam
import com.zoho.crm.api.relatedrecords.ResponseHandler
import com.zoho.crm.api.relatedrecords.ResponseWrapper
import com.zoho.crm.api.relatedrecords.SuccessResponse
import com.zoho.crm.api.tags.Tag
import com.zoho.crm.api.users.User
import com.zoho.crm.api.util.APIResponse
import com.zoho.crm.api.util.Choice
import com.zoho.crm.api.util.Model
import com.zoho.crm.api.util.StreamWrapper

import scala.collection.mutable.ArrayBuffer


object Relatedrecord {
/**
   *  Get Related Records Using External Id 
   * This method is used to get the related list records and print the response.
   *
   * @param moduleAPIName      - The API Name of the module to get related records.
   * @param externalValue      -
   * @param relatedListAPIName - The API name of the related list. To get the API name of the related list.
   * @throws Exception
   */
  @throws[Exception]
  def getRelatedRecordsUsingExternalId(moduleAPIName: String, externalValue: String, relatedListAPIName: String): Unit = { //example
    val xExternal = "Leads.External"
    //Get instance of RelatedRecordsOperations Class that takes relatedListAPIName, moduleAPIName and xExternal as parameter
    val relatedRecordsOperations = new RelatedRecordsOperations(relatedListAPIName, moduleAPIName, Option(xExternal))
    //Get instance of ParameterMap Class
    val paramInstance = new ParameterMap
    //    paramInstance.add(new GetRelatedRecordsParam().page, 1)
    //    paramInstance.add(new GetRelatedRecordsParam().perPage, 2)
    //Get instance of HeaderMap Class
    val headerInstance = new HeaderMap
    val startdatetime = OffsetDateTime.of(2019, 6, 1, 10, 0, 1, 0, ZoneOffset.of("+05:30"))
    headerInstance.add(new GetRelatedRecordsHeader().IfModifiedSince, startdatetime)
    //    Call getRelatedRecordsUsingExternalId method that takes externalValue, paramInstance and headerInstance as parameter
    val responseOption = relatedRecordsOperations.getRelatedRecordsUsingExternalId(externalValue, Option(paramInstance), Option( headerInstance))
    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
      }
      //Check if expected response is received
      if (response.isExpected) { //Get object from response
        val responseHandler = response.getObject
        responseHandler match {
          case responseWrapper: ResponseWrapper => //Get the received ResponseWrapper instance
            //Get the list of obtained Record instances
            val records = responseWrapper.getData

            for (record <- records) { //Get the ID of each Record
              println("RelatedRecord ID: " + record.getId)
              //Get the createdBy User instance of each Record
              var createdByOption = record.getCreatedBy
              //Check if createdBy is not null
              if (createdByOption.isDefined) { //Get the ID of the createdBy User
                var createdBy = createdByOption.get
                println("RelatedRecord Created By User-ID: " + createdBy.getId)
                //Get the name of the createdBy User
                println("RelatedRecord Created By User-Name: " + createdBy.getName)
                //Get the Email of the createdBy User
                println("RelatedRecord Created By User-Email: " + createdBy.getEmail)
              }
              //Get the CreatedTime of each Record
              println("RelatedRecord CreatedTime: " + record.getCreatedTime)
              //Get the modifiedBy User instance of each Record
              var modifiedByOption = record.getModifiedBy
              //Check if modifiedBy is not null
              if (modifiedByOption.isDefined) { //Get the ID of the modifiedBy User
                var modifiedBy = modifiedByOption.get
                println("RelatedRecord Modified By User-ID: " + modifiedBy.getId)
                //Get the name of the modifiedBy User
                println("RelatedRecord Modified By User-Name: " + modifiedBy.getName)
                //Get the Email of the modifiedBy User
                println("RelatedRecord Modified By User-Email: " + modifiedBy.getEmail)
              }
              //Get the ModifiedTime of each Record
              println("RelatedRecord ModifiedTime: " + record.getModifiedTime)
              //Get the list of Tag instance each Record
              val tags = record.getTag
              //Check if tags is not null
              if (tags.length > 0) {

                for (tag <- tags) { //Get the Name of each Tag
                  println("RelatedRecord Tag Name: " + tag.getName)
                  //Get the Id of each Tag
                  println("RelatedRecord Tag ID: " + tag.getId)
                }
              }
              //To get particular field value
              println("RelatedRecord Field Value: " + record.getKeyValue("Last_Name")) // FieldApiName

              println("RelatedRecord KeyValues: ")
              //Get the KeyValue map
              record.getKeyValues.foreach(entry => {
                val keyName = entry._1
                var value = entry._2
                value match {
                  case value1: Option[_] => value = value1.getOrElse(null)
                  case _ =>
                }
                value match {
                  case dataList: ArrayBuffer[Any] =>
                    if (dataList.size > 0) dataList(0) match {
                      case _: FileDetails =>
                        @SuppressWarnings(Array("unchecked")) val fileDetails = value.asInstanceOf[ArrayBuffer[FileDetails]]

                        for (fileDetail <- fileDetails) { //Get the Extn of each FileDetails
                          println("RelatedRecord FileDetails Extn: " + fileDetail.getExtn)
                          //Get the IsPreviewAvailable of each FileDetails
                          println("RelatedRecord FileDetails IsPreviewAvailable: " + fileDetail.getIsPreviewAvailable)
                          //Get the DownloadUrl of each FileDetails
                          println("RelatedRecord FileDetails DownloadUrl: " + fileDetail.getDownloadUrl)
                          //Get the DeleteUrl of each FileDetails
                          println("RelatedRecord FileDetails DeleteUrl: " + fileDetail.getDeleteUrl)
                          //Get the EntityId of each FileDetails
                          println("RelatedRecord FileDetails EntityId: " + fileDetail.getEntityId)
                          //Get the Mode of each FileDetails
                          println("RelatedRecord FileDetails Mode: " + fileDetail.getMode)
                          //Get the OriginalSizeByte of each FileDetails
                          println("RelatedRecord FileDetails OriginalSizeByte: " + fileDetail.getOriginalSizeByte)
                          //Get the PreviewUrl of each FileDetails
                          println("RelatedRecord FileDetails PreviewUrl: " + fileDetail.getPreviewUrl)
                          //Get the FileName of each FileDetails
                          println("RelatedRecord FileDetails FileName: " + fileDetail.getFileName)
                          //Get the FileId of each FileDetails
                          println("RelatedRecord FileDetails FileId: " + fileDetail.getFileId)
                          //Get the AttachmentId of each FileDetails
                          println("RelatedRecord FileDetails AttachmentId: " + fileDetail.getAttachmentId)
                          //Get the FileSize of each FileDetails
                          println("RelatedRecord FileDetails FileSize: " + fileDetail.getFileSize)
                          //Get the CreatorId of each FileDetails
                          println("RelatedRecord FileDetails CreatorId: " + fileDetail.getCreatorId)
                          //Get the LinkDocs of each FileDetails
                          println("RelatedRecord FileDetails LinkDocs: " + fileDetail.getLinkDocs)
                        }
                      case _: Choice[_] =>
                        @SuppressWarnings(Array("unchecked")) val choiceList = dataList.asInstanceOf[(ArrayBuffer[Choice[_$1]]) forSome {type _$1}]
                        println(keyName)
                        println("values")

                        for (choice <- choiceList) {
                          println(choice.getValue)
                        }
                      case _: Tag =>
                        @SuppressWarnings(Array("unchecked")) val tagList = value.asInstanceOf[ArrayBuffer[Tag]]

                        for (tag <- tagList) {
                          println("RelatedRecord Tag Name: " + tag.getName)
                          println("RelatedRecord Tag ID: " + tag.getId)
                        }
                      case _: PricingDetails =>
                        @SuppressWarnings(Array("unchecked")) val pricingDetails = value.asInstanceOf[ArrayBuffer[PricingDetails]]

                        for (pricingDetail <- pricingDetails) {
                          println("RelatedRecord PricingDetails ToRange: " + pricingDetail.getToRange.toString)
                          println("RelatedRecord PricingDetails Discount: " + pricingDetail.getDiscount.toString)
                          println("RelatedRecord PricingDetails ID: " + pricingDetail.getId)
                          println("RelatedRecord PricingDetails FromRange: " + pricingDetail.getFromRange.toString)
                        }
                      case _: Participants =>
                        val participants = value.asInstanceOf[ArrayBuffer[Participants]]
                        for (participant <- participants) {
                          println("RelatedRecord Participants Name: " + participant.getName().toString)
                          println("RelatedRecord Participants Invited: " + participant.getInvited().toString())
                          println("RelatedRecord Participants ID: " + participant.getId().toString)
                          println("RelatedRecord Participants Type: " + participant.getType().toString)
                          println("RelatedRecord Participants Participant: " + participant.getParticipant().toString)
                          println("RelatedRecord Participants Status: " + participant.getStatus().toString)
                        }
                      case _: Record =>
                        @SuppressWarnings(Array("unchecked")) val recordList = dataList.asInstanceOf[ArrayBuffer[Record]]

                        for (record1 <- recordList) { //Get the details map


                          record1.getKeyValues.foreach(entry => {
                            println(entry._1 + ": " + entry._2)
                          })
                        }
                      case _: LineTax =>
                        @SuppressWarnings(Array("unchecked")) val lineTaxes = dataList.asInstanceOf[ArrayBuffer[LineTax]]

                        for (lineTax <- lineTaxes) {
                          println("RelatedRecord ProductDetails LineTax Percentage: " + lineTax.getPercentage.toString)
                          println("RelatedRecord ProductDetails LineTax Name: " + lineTax.getName)
                          println("RelatedRecord ProductDetails LineTax Id: " + lineTax.getId)
                          println("RelatedRecord ProductDetails LineTax Value: " + lineTax.getValue.toString)
                        }
                      case _: Comment =>
                        @SuppressWarnings(Array("unchecked")) val comments = dataList.asInstanceOf[ArrayBuffer[Comment]]

                        for (comment <- comments) {
                          println("RelatedRecord Comment CommentedBy: " + comment.getCommentedBy)
                          println("RelatedRecord Comment CommentedTime: " + comment.getCommentedTime.toString)
                          println("RelatedRecord Comment CommentContent: " + comment.getCommentContent)
                          println("RelatedRecord Comment Id: " + comment.getId)
                        }
                      case _: Attachment => //Get the list of obtained Attachment instances
                        @SuppressWarnings(Array("unchecked")) val attachments = dataList.asInstanceOf[ArrayBuffer[Attachment]]


                        for (attachment <- attachments) { //Get the owner User instance of each attachment
                          val ownerOption = attachment.getOwner
                          //Check if owner is not null
                          if (ownerOption.isDefined) { //Get the Name of the Owner
                            var owner = ownerOption.get
                            println("RelatedRecord Attachment Owner User-Name: " + owner.getName)
                            //Get the ID of the Owner
                            println("RelatedRecord Attachment Owner User-ID: " + owner.getId)
                            //Get the Email of the Owner
                            println("RelatedRecord Attachment Owner User-Email: " + owner.getEmail)
                          }
                          //Get the modified time of each attachment
                          println("RelatedRecord Attachment Modified Time: " + attachment.getModifiedTime.toString)
                          //Get the name of the File
                          println("RelatedRecord Attachment File Name: " + attachment.getFileName)
                          //Get the created time of each attachment
                          println("RelatedRecord Attachment Created Time: " + attachment.getCreatedTime.toString)
                          //Get the Attachment file size
                          println("RelatedRecord Attachment File Size: " + attachment.getSize.toString)
                          //Get the parentId Record instance of each attachment
                          val parentIdOption = attachment.getParentId
                          //Check if parentId is not null
                          if (parentIdOption.isDefined) { //Get the parent record Name of each attachment
                            var parentId = parentIdOption.get
                            println("RelatedRecord Attachment parent record Name: " + parentId.getKeyValue("name"))
                            //Get the parent record ID of each attachment
                            println("RelatedRecord Attachment parent record ID: " + parentId.getId)
                          }
                          //Get the attachment is Editable
                          println("RelatedRecord Attachment is Editable: " + attachment.geteditable().toString)
                          //Get the file ID of each attachment
                          println("RelatedRecord Attachment File ID: " + attachment.getfileId())
                          //Get the type of each attachment
                          println("RelatedRecord Attachment File Type: " + attachment.gettype)
                          //Get the seModule of each attachment
                          println("RelatedRecord Attachment seModule: " + attachment.getseModule())
                          //Get the modifiedBy User instance of each attachment
                          var modifiedByOption = attachment.getModifiedBy
                          if (modifiedByOption.isDefined) { //Get the Name of the modifiedBy User
                            var modifiedBy = modifiedByOption.get
                            println("RelatedRecord Attachment Modified By User-Name: " + modifiedBy.getName)
                            println("RelatedRecord Attachment Modified By User-ID: " + modifiedBy.getId)
                            println("RelatedRecord Attachment Modified By User-Email: " + modifiedBy.getEmail)
                          }
                          //Get the state of each attachment
                          println("RelatedRecord Attachment State: " + attachment.getstate)
                          //Get the ID of each attachment
                          println("RelatedRecord Attachment ID: " + attachment.getId)
                          //Get the createdBy User instance of each attachment
                          var createdByOption = attachment.getCreatedBy
                          if (createdByOption.isDefined) {
                            var createdBy = modifiedByOption.get

                            println("RelatedRecord Attachment Created By User-Name: " + createdBy.getName)
                            println("RelatedRecord Attachment Created By User-ID: " + createdBy.getId)
                            println("RelatedRecord Attachment Created By User-Email: " + createdBy.getEmail)
                          }
                          //Get the linkUrl of each attachment
                          println("RelatedRecord Attachment LinkUrl: " + attachment.getlinkUrl())
                        }
                      case _ => println(keyName + ": " + value)
                    }
                  case recordValue: Record =>
                    println("RelatedRecord " + keyName + " ID: " + recordValue.getId)
                    println("RelatedRecord " + keyName + " Name: " + recordValue.getKeyValue("name"))
                  case layout: Layout =>
                    if (layout != null) {
                      println("RelatedRecord " + keyName + " ID: " + layout.getId)
                      println("RelatedRecord " + keyName + " Name: " + layout.getName)
                    }
                  case user: User =>
                    if (user != null) {
                      println("RelatedRecord " + keyName + " User-ID: " + user.getId)
                      println("RelatedRecord " + keyName + " User-Name: " + user.getName)
                      println("RelatedRecord " + keyName + " User-Email: " + user.getEmail)
                    }
                  case value1: Choice[_] => println(keyName + ": " + value1.getValue)
                  case at: RemindAt => println(keyName + ": " + at.getAlarm)
                  case activity: RecurringActivity =>
                    println(keyName)
                    println("RRULE" + ": " + activity.getRrule)
                  case consent: Consent =>
                    println("RelatedRecord Consent ID: " + consent.getId)
                    //Get the Owner User instance of each attachment
                    val ownerOption = consent.getOwner
                    if (ownerOption.isDefined) { //Get the name of the owner User
                      var owner = ownerOption.get
                      println("RelatedRecord Consent Owner Name: " + owner.getName)
                      //Get the ID of the owner User
                      println("RelatedRecord Consent Owner ID: " + owner.getId)
                      //Get the Email of the owner User
                      println("RelatedRecord Consent Owner Email: " + owner.getEmail)
                    }
                    val consentCreatedByOption = consent.getCreatedBy
                    if (consentCreatedByOption.isDefined) { //Get the name of the CreatedBy User
                      var consentCreatedBy = consentCreatedByOption.get
                      println("RelatedRecord Consent CreatedBy Name: " + consentCreatedBy.getName)
                      //Get the ID of the CreatedBy User
                      println("RelatedRecord Consent CreatedBy ID: " + consentCreatedBy.getId)
                      //Get the Email of the CreatedBy User
                      println("RelatedRecord Consent CreatedBy Email: " + consentCreatedBy.getEmail)
                    }
                    val consentModifiedByOption = consent.getModifiedBy
                    if (consentModifiedByOption.isDefined) { //Get the name of the ModifiedBy User
                      var consentModifiedBy = consentModifiedByOption.get
                      println("RelatedRecord Consent ModifiedBy Name: " + consentModifiedBy.getName)
                      //Get the ID of the ModifiedBy User
                      println("RelatedRecord Consent ModifiedBy ID: " + consentModifiedBy.getId)
                      //Get the Email of the ModifiedBy User
                      println("RelatedRecord Consent ModifiedBy Email: " + consentModifiedBy.getEmail)
                    }
                    println("RelatedRecord Consent CreatedTime: " + consent.getCreatedTime)
                    println("RelatedRecord Consent ModifiedTime: " + consent.getModifiedTime)
                    println("RelatedRecord Consent ContactThroughEmail: " + consent.getContactThroughEmail)
                    println("RelatedRecord Consent ContactThroughSocial: " + consent.getContactThroughSocial)
                    println("RelatedRecord Consent ContactThroughSurvey: " + consent.getContactThroughSurvey)
                    println("RelatedRecord Consent ContactThroughPhone: " + consent.getContactThroughPhone)
                    println("RelatedRecord Consent MailSentTime: " + consent.getMailSentTime.toString)
                    println("RelatedRecord Consent ConsentDate: " + consent.getConsentDate.toString)
                    println("RelatedRecord Consent ConsentRemarks: " + consent.getConsentRemarks)
                    println("RelatedRecord Consent ConsentThrough: " + consent.getConsentThrough)
                    println("RelatedRecord Consent DataProcessingBasis: " + consent.getDataProcessingBasis)
                    //To get custom values
                    println("RelatedRecord Consent Lawful Reason: " + consent.getKeyValue("Lawful_Reason"))
                  case _ => println(keyName + ": " + value)
                }
              })
            }
          case _ => //Check if the request returned an exception
            responseHandler match {
              case exception: APIException => //Get the received APIException instance
                //Get the Status
                println("Status: " + exception.getStatus.getValue)
                //Get the Code
                println("Code: " + exception.getCode.getValue)
                println("Details: ")

                exception.getDetails.foreach(entry => {
                  println(entry._1 + ": " + entry._2)
                })
                //Get the Message
                println("Message: " + exception.getMessage.getValue)
              case _ =>
            }
        }
      }
      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))
        }
      }
    }
  }
}
class Relatedrecord {}
 
Update Related Records Using External ID

              
package com.zoho.crm.sample.relatedrecords

import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.io.OutputStream
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util

import com.zoho.crm.api.HeaderMap
import com.zoho.crm.api.ParameterMap
import com.zoho.crm.api.attachments.Attachment
import com.zoho.crm.api.layouts.Layout
import com.zoho.crm.api.record.{Comment, Consent, FileDetails, LineTax, Participants, PricingDetails, Record, RecurringActivity, RemindAt}
import com.zoho.crm.api.relatedrecords.FileBodyWrapper
import com.zoho.crm.api.relatedrecords.APIException
import com.zoho.crm.api.relatedrecords.ActionHandler
import com.zoho.crm.api.relatedrecords.ActionResponse
import com.zoho.crm.api.relatedrecords.ActionWrapper
import com.zoho.crm.api.relatedrecords.BodyWrapper
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.DelinkRecordsParam
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsParam
import com.zoho.crm.api.relatedrecords.ResponseHandler
import com.zoho.crm.api.relatedrecords.ResponseWrapper
import com.zoho.crm.api.relatedrecords.SuccessResponse
import com.zoho.crm.api.tags.Tag
import com.zoho.crm.api.users.User
import com.zoho.crm.api.util.APIResponse
import com.zoho.crm.api.util.Choice
import com.zoho.crm.api.util.Model
import com.zoho.crm.api.util.StreamWrapper

import scala.collection.mutable.ArrayBuffer


object Relatedrecord {
/**
   *  Update Related Records Using External Id
   * This method is used to update the relation between the records and print the response.
   *
   * @param moduleAPIName      - The API Name of the module to update related records.
   * @param externalValue      -
   * @param relatedListAPIName - The API name of the related list. To get the API name of the related list.
   * @throws Exception
   */
  @throws[Exception]
  def updateRelatedRecordsUsingExternalId(moduleAPIName: String, externalValue: String, relatedListAPIName: String): Unit = { //API Name of the module
    val xExternal = "Leads.External,Products.Products_External"
    val relatedRecordsOperations = new RelatedRecordsOperations(relatedListAPIName, moduleAPIName, Option(xExternal))
    //Get instance of BodyWrapper Class that will contain the request body
    val request = new BodyWrapper
    //List of Record instances
    val records = new ArrayBuffer[Record]
    //Get instance of Record Class
    val record1 = new Record
    /*
      * Call addKeyValue method that takes two arguments
      * 1 -> A string that is the Field's API Name
      * 2 -> Value
      */
    record1.addKeyValue("Products_External", "TestExternalLead121")
    record1.addKeyValue("Note_Content", "asd")
    //Add Record instance to the list
    records.addOne(record1)
    val record2 = new Record
    record2.addKeyValue("Products_External", "TestExternal122")
    record2.addKeyValue("Note_Content", "asd")
    records.addOne(record2)
    //Set the list to Records in BodyWrapper instance
    request.setData(records)
    //Call updateRelatedRecordsUsingExternalId method that takes externalValue and BodyWrapper instance as parameter.
    val responseOption = relatedRecordsOperations.updateRelatedRecordsUsingExternalId(externalValue, request)
    if (responseOption.isDefined) {
      var response = responseOption.get
      println("Status Code: " + response.getStatusCode)
      if (response.isExpected) {
        val actionHandler = response.getObject
        actionHandler match {
          case actionWrapper: ActionWrapper => //Get the received ActionWrapper instance
            //Get the list of obtained ActionResponse instances
            val actionResponses = actionWrapper.getData

            for (actionResponse <- actionResponses) { //Check if the request is successful
              actionResponse match {
                case successResponse: SuccessResponse => //Get the received SuccessResponse instance
                  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)
                case exception: 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)
                case _ =>
              }
            }
          case exception: 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)
          case _ =>
        }
      }
      else {
        val responseObject = response.getModel
        val clas = responseObject.getClass
        val fields = clas.getDeclaredFields
        for (field <- fields) {
          println(field.getName + ":" + field.get(responseObject))
        }
      }
    }
  }
}
class Relatedrecord {}
 
Delete Related Records Using External ID

              
package com.zoho.crm.sample.relatedrecords

import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.io.OutputStream
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util

import com.zoho.crm.api.HeaderMap
import com.zoho.crm.api.ParameterMap
import com.zoho.crm.api.attachments.Attachment
import com.zoho.crm.api.layouts.Layout
import com.zoho.crm.api.record.{Comment, Consent, FileDetails, LineTax, Participants, PricingDetails, Record, RecurringActivity, RemindAt}
import com.zoho.crm.api.relatedrecords.FileBodyWrapper
import com.zoho.crm.api.relatedrecords.APIException
import com.zoho.crm.api.relatedrecords.ActionHandler
import com.zoho.crm.api.relatedrecords.ActionResponse
import com.zoho.crm.api.relatedrecords.ActionWrapper
import com.zoho.crm.api.relatedrecords.BodyWrapper
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.DelinkRecordsParam
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsParam
import com.zoho.crm.api.relatedrecords.ResponseHandler
import com.zoho.crm.api.relatedrecords.ResponseWrapper
import com.zoho.crm.api.relatedrecords.SuccessResponse
import com.zoho.crm.api.tags.Tag
import com.zoho.crm.api.users.User
import com.zoho.crm.api.util.APIResponse
import com.zoho.crm.api.util.Choice
import com.zoho.crm.api.util.Model
import com.zoho.crm.api.util.StreamWrapper

import scala.collection.mutable.ArrayBuffer

object Relatedrecord {
/**
   *  Delete RelatedRecords Using External Id 
   * This method is used to delete the association between modules and print the response.
   *
   * @param moduleAPIName      - The API Name of the module to delink related records.
   * @param externalValue      -
   * @param relatedListAPIName - The API name of the related list. To get the API name of the related list.
   * @param relatedListIds     - The List of related record IDs.
   * @throws Exception
   */
  @throws[Exception]
  def deleteRelatedRecordsUsingExternalId(moduleAPIName: String, externalValue: String, relatedListAPIName: String, relatedListIds: ArrayBuffer[String]): Unit = { //List relatedListIds = new ArrayList(Arrays.asList(3477061000005919001l, 3477061000005917011l))
    val xExternal = "Leads.External,Products.Products_External"
    val relatedRecordsOperations = new RelatedRecordsOperations(relatedListAPIName, moduleAPIName, Option(xExternal))
    val paramInstance = new ParameterMap

    for (relatedListId <- relatedListIds) {
      paramInstance.add(new DelinkRecordsParam().ids, relatedListId)
    }
    //    Call deleteRelatedRecordsUsingExternalId method that takes externalValue and paramInstance instance as parameter.
    val responseOption = relatedRecordsOperations.deleteRelatedRecordsUsingExternalId(externalValue, Option(paramInstance))
    if (responseOption.isDefined) {
      var response= responseOption.get
      println("Status Code: " + response.getStatusCode)
      if (response.isExpected) {
        val actionHandler = response.getObject
        actionHandler match {
          case actionWrapper: ActionWrapper =>
            val actionResponses = actionWrapper.getData

            for (actionResponse <- actionResponses) {
              actionResponse match {
                case successResponse: 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)
                case exception: 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)
                case _ =>
              }
            }
          case exception: 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)
          case _ =>
        }
      }
      else {
        val responseObject = response.getModel
        val clas = responseObject.getClass
        val fields = clas.getDeclaredFields
        for (field <- fields) {
          println(field.getName + ":" + field.get(responseObject))
        }
      }
    }
  }
}
class Relatedrecord {}
 
Get Related Record Using External ID

              
package com.zoho.crm.sample.relatedrecords

import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.io.OutputStream
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util

import com.zoho.crm.api.HeaderMap
import com.zoho.crm.api.ParameterMap
import com.zoho.crm.api.attachments.Attachment
import com.zoho.crm.api.layouts.Layout
import com.zoho.crm.api.record.{Comment, Consent, FileDetails, LineTax, Participants, PricingDetails, Record, RecurringActivity, RemindAt}
import com.zoho.crm.api.relatedrecords.FileBodyWrapper
import com.zoho.crm.api.relatedrecords.APIException
import com.zoho.crm.api.relatedrecords.ActionHandler
import com.zoho.crm.api.relatedrecords.ActionResponse
import com.zoho.crm.api.relatedrecords.ActionWrapper
import com.zoho.crm.api.relatedrecords.BodyWrapper
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.DelinkRecordsParam
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsParam
import com.zoho.crm.api.relatedrecords.ResponseHandler
import com.zoho.crm.api.relatedrecords.ResponseWrapper
import com.zoho.crm.api.relatedrecords.SuccessResponse
import com.zoho.crm.api.tags.Tag
import com.zoho.crm.api.users.User
import com.zoho.crm.api.util.APIResponse
import com.zoho.crm.api.util.Choice
import com.zoho.crm.api.util.Model
import com.zoho.crm.api.util.StreamWrapper

import scala.collection.mutable.ArrayBuffer


object Relatedrecord {
/**
   *  Get Related Record Using External Id 
   * This method is used to get the single related list record and print the response.
   *
   * @param moduleAPIName      - The API Name of the module to get related record.
   * @param externalValue      -
   * @param relatedListAPIName - The API name of the related list. To get the API name of the related list.
   * @param externalFieldValue -
   * @param destinationFolder  - The absolute path of the destination folder to store the file.
   * @throws Exception
   */
  @throws[Exception]
  def getRelatedRecordUsingExternalId(moduleAPIName: String, externalValue: String, relatedListAPIName: String, externalFieldValue: String, destinationFolder: String): Unit = { //Long relatedListId = 3477061000004994115l
    val xExternal = "Leads.External,Products.Products_External"
    val relatedRecordsOperations = new RelatedRecordsOperations(relatedListAPIName, moduleAPIName, Option(xExternal))
    val headerInstance = new HeaderMap
    val startdatetime = OffsetDateTime.of(2019, 6, 1, 10, 0, 1, 0, ZoneOffset.of("+05:30"))
    headerInstance.add(new GetRelatedRecordHeader().IfModifiedSince, startdatetime)
    //Call getRelatedRecordUsingExternalId method that takes headerInstance and relatedRecordId as parameter
    val responseOption = relatedRecordsOperations.getRelatedRecordUsingExternalId(externalFieldValue, externalValue, Option(headerInstance))
    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
      }
      //Check if expected response is received
      if (response.isExpected) { //Get object from response
        val responseHandler = response.getObject
        responseHandler match {
          case responseWrapper: ResponseWrapper => //Get the received ResponseWrapper instance
            //Get the list of obtained Record instances
            val records = responseWrapper.getData

            for (record <- records) { //Get the ID of each Record
              println("RelatedRecord ID: " + record.getId)
              //Get the createdBy User instance of each Record
              var createdByOption = record.getCreatedBy
              //Check if createdBy is not null
              if (createdByOption.isDefined) { //Get the ID of the createdBy User
                var createdBy = createdByOption.get
                println("RelatedRecord Created By User-ID: " + createdBy.getId)
                //Get the name of the createdBy User
                println("RelatedRecord Created By User-Name: " + createdBy.getName)
                //Get the Email of the createdBy User
                println("RelatedRecord Created By User-Email: " + createdBy.getEmail)
              }
              //Get the CreatedTime of each Record
              println("RelatedRecord CreatedTime: " + record.getCreatedTime)
              //Get the modifiedBy User instance of each Record
              var modifiedByOption = record.getModifiedBy
              //Check if modifiedBy is not null
              if (modifiedByOption.isDefined) { //Get the ID of the modifiedBy User
                var modifiedBy = modifiedByOption.get
                println("RelatedRecord Modified By User-ID: " + modifiedBy.getId)
                //Get the name of the modifiedBy User
                println("RelatedRecord Modified By User-Name: " + modifiedBy.getName)
                //Get the Email of the modifiedBy User
                println("RelatedRecord Modified By User-Email: " + modifiedBy.getEmail)
              }
              //Get the ModifiedTime of each Record
              println("RelatedRecord ModifiedTime: " + record.getModifiedTime)
              //Get the list of Tag instance each Record
              val tags = record.getTag
              //Check if tags is not null
              if (tags.length > 0) {

                for (tag <- tags) { //Get the Name of each Tag
                  println("RelatedRecord Tag Name: " + tag.getName)
                  //Get the Id of each Tag
                  println("RelatedRecord Tag ID: " + tag.getId)
                }
              }
              //To get particular field value
              println("RelatedRecord Field Value: " + record.getKeyValue("Last_Name")) // FieldApiName

              println("RelatedRecord KeyValues: ")
              //Get the KeyValue map
              record.getKeyValues.foreach(entry => {
                val keyName = entry._1
                var value = entry._2
                value match {
                  case value1: Option[_] => value = value1.getOrElse(null)
                  case _ =>
                }
                value match {
                  case dataList: ArrayBuffer[Any] =>
                    if (dataList.size > 0) dataList(0) match {
                      case _: FileDetails =>
                        @SuppressWarnings(Array("unchecked")) val fileDetails = value.asInstanceOf[ArrayBuffer[FileDetails]]

                        for (fileDetail <- fileDetails) { //Get the Extn of each FileDetails
                          println("RelatedRecord FileDetails Extn: " + fileDetail.getExtn)
                          //Get the IsPreviewAvailable of each FileDetails
                          println("RelatedRecord FileDetails IsPreviewAvailable: " + fileDetail.getIsPreviewAvailable)
                          //Get the DownloadUrl of each FileDetails
                          println("RelatedRecord FileDetails DownloadUrl: " + fileDetail.getDownloadUrl)
                          //Get the DeleteUrl of each FileDetails
                          println("RelatedRecord FileDetails DeleteUrl: " + fileDetail.getDeleteUrl)
                          //Get the EntityId of each FileDetails
                          println("RelatedRecord FileDetails EntityId: " + fileDetail.getEntityId)
                          //Get the Mode of each FileDetails
                          println("RelatedRecord FileDetails Mode: " + fileDetail.getMode)
                          //Get the OriginalSizeByte of each FileDetails
                          println("RelatedRecord FileDetails OriginalSizeByte: " + fileDetail.getOriginalSizeByte)
                          //Get the PreviewUrl of each FileDetails
                          println("RelatedRecord FileDetails PreviewUrl: " + fileDetail.getPreviewUrl)
                          //Get the FileName of each FileDetails
                          println("RelatedRecord FileDetails FileName: " + fileDetail.getFileName)
                          //Get the FileId of each FileDetails
                          println("RelatedRecord FileDetails FileId: " + fileDetail.getFileId)
                          //Get the AttachmentId of each FileDetails
                          println("RelatedRecord FileDetails AttachmentId: " + fileDetail.getAttachmentId)
                          //Get the FileSize of each FileDetails
                          println("RelatedRecord FileDetails FileSize: " + fileDetail.getFileSize)
                          //Get the CreatorId of each FileDetails
                          println("RelatedRecord FileDetails CreatorId: " + fileDetail.getCreatorId)
                          //Get the LinkDocs of each FileDetails
                          println("RelatedRecord FileDetails LinkDocs: " + fileDetail.getLinkDocs)
                        }
                      case _: Choice[_] =>
                        @SuppressWarnings(Array("unchecked")) val choiceList = dataList.asInstanceOf[(ArrayBuffer[Choice[_$1]]) forSome {type _$1}]
                        println(keyName)
                        println("values")

                        for (choice <- choiceList) {
                          println(choice.getValue)
                        }
                      case _: Tag =>
                        @SuppressWarnings(Array("unchecked")) val tagList = value.asInstanceOf[ArrayBuffer[Tag]]

                        for (tag <- tagList) {
                          println("RelatedRecord Tag Name: " + tag.getName)
                          println("RelatedRecord Tag ID: " + tag.getId)
                        }
                      case _: PricingDetails =>
                        @SuppressWarnings(Array("unchecked")) val pricingDetails = value.asInstanceOf[ArrayBuffer[PricingDetails]]

                        for (pricingDetail <- pricingDetails) {
                          println("RelatedRecord PricingDetails ToRange: " + pricingDetail.getToRange.toString)
                          println("RelatedRecord PricingDetails Discount: " + pricingDetail.getDiscount.toString)
                          println("RelatedRecord PricingDetails ID: " + pricingDetail.getId)
                          println("RelatedRecord PricingDetails FromRange: " + pricingDetail.getFromRange.toString)
                        }
                      case _: Participants =>
                        val participants = value.asInstanceOf[ArrayBuffer[Participants]]
                        for (participant <- participants) {
                          println("RelatedRecord Participants Name: " + participant.getName().toString)
                          println("RelatedRecord Participants Invited: " + participant.getInvited().toString())
                          println("RelatedRecord Participants ID: " + participant.getId().toString)
                          println("RelatedRecord Participants Type: " + participant.getType().toString)
                          println("RelatedRecord Participants Participant: " + participant.getParticipant().toString)
                          println("RelatedRecord Participants Status: " + participant.getStatus().toString)
                        }
                      case _: Record =>
                        @SuppressWarnings(Array("unchecked")) val recordList = dataList.asInstanceOf[ArrayBuffer[Record]]

                        for (record1 <- recordList) { //Get the details map


                          record1.getKeyValues.foreach(entry => {
                            println(entry._1 + ": " + entry._2)
                          })
                        }
                      case _: LineTax =>
                        @SuppressWarnings(Array("unchecked")) val lineTaxes = dataList.asInstanceOf[ArrayBuffer[LineTax]]

                        for (lineTax <- lineTaxes) {
                          println("RelatedRecord ProductDetails LineTax Percentage: " + lineTax.getPercentage.toString)
                          println("RelatedRecord ProductDetails LineTax Name: " + lineTax.getName)
                          println("RelatedRecord ProductDetails LineTax Id: " + lineTax.getId)
                          println("RelatedRecord ProductDetails LineTax Value: " + lineTax.getValue.toString)
                        }
                      case _: Comment =>
                        @SuppressWarnings(Array("unchecked")) val comments = dataList.asInstanceOf[ArrayBuffer[Comment]]

                        for (comment <- comments) {
                          println("RelatedRecord Comment CommentedBy: " + comment.getCommentedBy)
                          println("RelatedRecord Comment CommentedTime: " + comment.getCommentedTime.toString)
                          println("RelatedRecord Comment CommentContent: " + comment.getCommentContent)
                          println("RelatedRecord Comment Id: " + comment.getId)
                        }
                      case _: Attachment => //Get the list of obtained Attachment instances
                        @SuppressWarnings(Array("unchecked")) val attachments = dataList.asInstanceOf[ArrayBuffer[Attachment]]


                        for (attachment <- attachments) { //Get the owner User instance of each attachment
                          val ownerOption = attachment.getOwner
                          //Check if owner is not null
                          if (ownerOption.isDefined) { //Get the Name of the Owner
                            var owner = ownerOption.get
                            println("RelatedRecord Attachment Owner User-Name: " + owner.getName)
                            //Get the ID of the Owner
                            println("RelatedRecord Attachment Owner User-ID: " + owner.getId)
                            //Get the Email of the Owner
                            println("RelatedRecord Attachment Owner User-Email: " + owner.getEmail)
                          }
                          //Get the modified time of each attachment
                          println("RelatedRecord Attachment Modified Time: " + attachment.getModifiedTime.toString)
                          //Get the name of the File
                          println("RelatedRecord Attachment File Name: " + attachment.getFileName)
                          //Get the created time of each attachment
                          println("RelatedRecord Attachment Created Time: " + attachment.getCreatedTime.toString)
                          //Get the Attachment file size
                          println("RelatedRecord Attachment File Size: " + attachment.getSize.toString)
                          //Get the parentId Record instance of each attachment
                          val parentIdOption = attachment.getParentId
                          //Check if parentId is not null
                          if (parentIdOption.isDefined) { //Get the parent record Name of each attachment
                            var parentId = parentIdOption.get
                            println("RelatedRecord Attachment parent record Name: " + parentId.getKeyValue("name"))
                            //Get the parent record ID of each attachment
                            println("RelatedRecord Attachment parent record ID: " + parentId.getId)
                          }
                          //Get the attachment is Editable
                          println("RelatedRecord Attachment is Editable: " + attachment.geteditable().toString)
                          //Get the file ID of each attachment
                          println("RelatedRecord Attachment File ID: " + attachment.getfileId())
                          //Get the type of each attachment
                          println("RelatedRecord Attachment File Type: " + attachment.gettype)
                          //Get the seModule of each attachment
                          println("RelatedRecord Attachment seModule: " + attachment.getseModule())
                          //Get the modifiedBy User instance of each attachment
                          var modifiedByOption = attachment.getModifiedBy
                          if (modifiedByOption.isDefined) { //Get the Name of the modifiedBy User
                            var modifiedBy = modifiedByOption.get
                            println("RelatedRecord Attachment Modified By User-Name: " + modifiedBy.getName)
                            println("RelatedRecord Attachment Modified By User-ID: " + modifiedBy.getId)
                            println("RelatedRecord Attachment Modified By User-Email: " + modifiedBy.getEmail)
                          }
                          //Get the state of each attachment
                          println("RelatedRecord Attachment State: " + attachment.getstate)
                          //Get the ID of each attachment
                          println("RelatedRecord Attachment ID: " + attachment.getId)
                          //Get the createdBy User instance of each attachment
                          var createdByOption = attachment.getCreatedBy
                          if (createdByOption.isDefined) {
                            var createdBy = modifiedByOption.get

                            println("RelatedRecord Attachment Created By User-Name: " + createdBy.getName)
                            println("RelatedRecord Attachment Created By User-ID: " + createdBy.getId)
                            println("RelatedRecord Attachment Created By User-Email: " + createdBy.getEmail)
                          }
                          //Get the linkUrl of each attachment
                          println("RelatedRecord Attachment LinkUrl: " + attachment.getlinkUrl())
                        }
                      case _ => println(keyName + ": " + value)
                    }
                  case recordValue: Record =>
                    println("RelatedRecord " + keyName + " ID: " + recordValue.getId)
                    println("RelatedRecord " + keyName + " Name: " + recordValue.getKeyValue("name"))
                  case layout: Layout =>
                    if (layout != null) {
                      println("RelatedRecord " + keyName + " ID: " + layout.getId)
                      println("RelatedRecord " + keyName + " Name: " + layout.getName)
                    }
                  case user: User =>
                    if (user != null) {
                      println("RelatedRecord " + keyName + " User-ID: " + user.getId)
                      println("RelatedRecord " + keyName + " User-Name: " + user.getName)
                      println("RelatedRecord " + keyName + " User-Email: " + user.getEmail)
                    }
                  case value1: Choice[_] => println(keyName + ": " + value1.getValue)
                  case at: RemindAt => println(keyName + ": " + at.getAlarm)
                  case activity: RecurringActivity =>
                    println(keyName)
                    println("RRULE" + ": " + activity.getRrule)
                  case consent: Consent =>
                    println("RelatedRecord Consent ID: " + consent.getId)
                    //Get the Owner User instance of each attachment
                    val ownerOption = consent.getOwner
                    if (ownerOption.isDefined) { //Get the name of the owner User
                      var owner = ownerOption.get
                      println("RelatedRecord Consent Owner Name: " + owner.getName)
                      //Get the ID of the owner User
                      println("RelatedRecord Consent Owner ID: " + owner.getId)
                      //Get the Email of the owner User
                      println("RelatedRecord Consent Owner Email: " + owner.getEmail)
                    }
                    val consentCreatedByOption = consent.getCreatedBy
                    if (consentCreatedByOption.isDefined) { //Get the name of the CreatedBy User
                      var consentCreatedBy = consentCreatedByOption.get
                      println("RelatedRecord Consent CreatedBy Name: " + consentCreatedBy.getName)
                      //Get the ID of the CreatedBy User
                      println("RelatedRecord Consent CreatedBy ID: " + consentCreatedBy.getId)
                      //Get the Email of the CreatedBy User
                      println("RelatedRecord Consent CreatedBy Email: " + consentCreatedBy.getEmail)
                    }
                    val consentModifiedByOption = consent.getModifiedBy
                    if (consentModifiedByOption.isDefined) { //Get the name of the ModifiedBy User
                      var consentModifiedBy = consentModifiedByOption.get
                      println("RelatedRecord Consent ModifiedBy Name: " + consentModifiedBy.getName)
                      //Get the ID of the ModifiedBy User
                      println("RelatedRecord Consent ModifiedBy ID: " + consentModifiedBy.getId)
                      //Get the Email of the ModifiedBy User
                      println("RelatedRecord Consent ModifiedBy Email: " + consentModifiedBy.getEmail)
                    }
                    println("RelatedRecord Consent CreatedTime: " + consent.getCreatedTime)
                    println("RelatedRecord Consent ModifiedTime: " + consent.getModifiedTime)
                    println("RelatedRecord Consent ContactThroughEmail: " + consent.getContactThroughEmail)
                    println("RelatedRecord Consent ContactThroughSocial: " + consent.getContactThroughSocial)
                    println("RelatedRecord Consent ContactThroughSurvey: " + consent.getContactThroughSurvey)
                    println("RelatedRecord Consent ContactThroughPhone: " + consent.getContactThroughPhone)
                    println("RelatedRecord Consent MailSentTime: " + consent.getMailSentTime.toString)
                    println("RelatedRecord Consent ConsentDate: " + consent.getConsentDate.toString)
                    println("RelatedRecord Consent ConsentRemarks: " + consent.getConsentRemarks)
                    println("RelatedRecord Consent ConsentThrough: " + consent.getConsentThrough)
                    println("RelatedRecord Consent DataProcessingBasis: " + consent.getDataProcessingBasis)
                    //To get custom values
                    println("RelatedRecord Consent Lawful Reason: " + consent.getKeyValue("Lawful_Reason"))
                  case _ => println(keyName + ": " + value)
                }
              })
            }
          case fileBodyWrapper: FileBodyWrapper =>
            //Get StreamWrapper instance from the returned FileBodyWrapper instance
            val streamWrapper = fileBodyWrapper.getFile.get
            //Create a file instance with the absolute_file_path
            val file = new File(destinationFolder + File.separatorChar + streamWrapper.getName.get)
            //Get InputStream from the response
            val is = streamWrapper.getStream.get
            //Create an OutputStream for the destination file
            val os = new FileOutputStream(file)
            val buffer = new Array[Byte](1024)
            var bytesRead = 0
            //read the InputStream till the end
            while ( {
              (bytesRead = is.read(buffer)) != -1
            }) { //write data to OutputStream
              os.write(buffer, 0, bytesRead)
            }
            //Close the InputStream
            is.close()
            //Flush and close the OutputStream
            os.flush()
            os.close()
          case _ => //Check if the request returned an exception
            responseHandler match {
              case exception: APIException => //Get the received APIException instance
                //Get the Status
                println("Status: " + exception.getStatus.getValue)
                //Get the Code
                println("Code: " + exception.getCode.getValue)
                println("Details: ")

                exception.getDetails.foreach(entry => {
                  println(entry._1 + ": " + entry._2)
                })
                //Get the Message
                println("Message: " + exception.getMessage.getValue)
              case _ =>
            }
        }
      }
      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))
        }
      }
    }
  }
}
class Relatedrecord {}
 
Update Related Record Using External ID

              
package com.zoho.crm.sample.relatedrecords

import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.io.OutputStream
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util

import com.zoho.crm.api.HeaderMap
import com.zoho.crm.api.ParameterMap
import com.zoho.crm.api.attachments.Attachment
import com.zoho.crm.api.layouts.Layout
import com.zoho.crm.api.record.{Comment, Consent, FileDetails, LineTax, Participants, PricingDetails, Record, RecurringActivity, RemindAt}
import com.zoho.crm.api.relatedrecords.FileBodyWrapper
import com.zoho.crm.api.relatedrecords.APIException
import com.zoho.crm.api.relatedrecords.ActionHandler
import com.zoho.crm.api.relatedrecords.ActionResponse
import com.zoho.crm.api.relatedrecords.ActionWrapper
import com.zoho.crm.api.relatedrecords.BodyWrapper
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.DelinkRecordsParam
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsParam
import com.zoho.crm.api.relatedrecords.ResponseHandler
import com.zoho.crm.api.relatedrecords.ResponseWrapper
import com.zoho.crm.api.relatedrecords.SuccessResponse
import com.zoho.crm.api.tags.Tag
import com.zoho.crm.api.users.User
import com.zoho.crm.api.util.APIResponse
import com.zoho.crm.api.util.Choice
import com.zoho.crm.api.util.Model
import com.zoho.crm.api.util.StreamWrapper

import scala.collection.mutable.ArrayBuffer


object Relatedrecord {
/**
   *  Update Related Record Using External Id 
   * This method is used to update the relation between the records and print the response.
   *
   * @param moduleAPIName      - The API Name of the module to update related record.
   * @param externalValue      -
   * @param relatedListAPIName - The API name of the related list. To get the API name of the related list.
   * @param externalFieldValue -
   * @throws Exception
   */
  @throws[Exception]
  def updateRelatedRecordUsingExternalId(moduleAPIName: String, externalValue: String, relatedListAPIName: String, externalFieldValue: String): Unit = {
    val xExternal = "Leads.External,Products.Products_External"
    val relatedRecordsOperations =new RelatedRecordsOperations(relatedListAPIName, moduleAPIName, Option(xExternal))
    val request = new BodyWrapper
    val records = new ArrayBuffer[Record]
    val record1 = new Record
    record1.addKeyValue("Note_Content", "asd")
    records.addOne(record1)
    request.setData(records)
    //Call updateRelatedRecordUsingExternalId method that takes externalFieldValue, externalValue and BodyWrapper instance as parameter.
    val responseOption = relatedRecordsOperations.updateRelatedRecordUsingExternalId(externalFieldValue, externalValue, request)
    if (responseOption.isDefined) {
      var response= responseOption.get
      println("Status Code: " + response.getStatusCode)
      if (response.isExpected) {
        val actionHandler = response.getObject
        actionHandler match {
          case actionWrapper: ActionWrapper =>
            val actionResponses = actionWrapper.getData

            for (actionResponse <- actionResponses) {
              actionResponse match {
                case successResponse: 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)
                case exception: 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)
                case _ =>
              }
            }
          case exception: 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)
          case _ =>
        }
      }
      else {
        val responseObject = response.getModel
        val clas = responseObject.getClass
        val fields = clas.getDeclaredFields
        for (field <- fields) {
          println(field.getName + ":" + field.get(responseObject))
        }
      }
    }
  }
}
class Relatedrecord {}
 
Delete Related Record Using External ID

              
package com.zoho.crm.sample.relatedrecords

import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.io.OutputStream
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util

import com.zoho.crm.api.HeaderMap
import com.zoho.crm.api.ParameterMap
import com.zoho.crm.api.attachments.Attachment
import com.zoho.crm.api.layouts.Layout
import com.zoho.crm.api.record.{Comment, Consent, FileDetails, LineTax, Participants, PricingDetails, Record, RecurringActivity, RemindAt}
import com.zoho.crm.api.relatedrecords.FileBodyWrapper
import com.zoho.crm.api.relatedrecords.APIException
import com.zoho.crm.api.relatedrecords.ActionHandler
import com.zoho.crm.api.relatedrecords.ActionResponse
import com.zoho.crm.api.relatedrecords.ActionWrapper
import com.zoho.crm.api.relatedrecords.BodyWrapper
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.DelinkRecordsParam
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsParam
import com.zoho.crm.api.relatedrecords.ResponseHandler
import com.zoho.crm.api.relatedrecords.ResponseWrapper
import com.zoho.crm.api.relatedrecords.SuccessResponse
import com.zoho.crm.api.tags.Tag
import com.zoho.crm.api.users.User
import com.zoho.crm.api.util.APIResponse
import com.zoho.crm.api.util.Choice
import com.zoho.crm.api.util.Model
import com.zoho.crm.api.util.StreamWrapper

import scala.collection.mutable.ArrayBuffer


object Relatedrecord {
/**
     *  Delete Related Record Using External Id 
   * This method is used to delete the association between modules and print the response.
   *
   * @param moduleAPIName      - The API Name of the module to delink related record.
   * @param externalValue      -
   * @param relatedListAPIName - The API name of the related list. To get the API name of the related list.
   * @param externalFieldValue -
   * @throws Exception
   */
  @throws[Exception]
  def deleteRelatedRecordUsingExternalId(moduleAPIName: String, externalValue: String, relatedListAPIName: String, externalFieldValue: String): Unit = {
    val xExternal = "Leads.External,Products.Products_External"
    val relatedRecordsOperations = new RelatedRecordsOperations(relatedListAPIName, moduleAPIName, Option(xExternal))
    //Call deleteRelatedRecordUsingExternalId method that takes relatedListId as parameter.
    val responseOption = relatedRecordsOperations.deleteRelatedRecordUsingExternalId(externalFieldValue, externalValue)
    if (responseOption.isDefined) {
      var response= responseOption.get
      println("Status Code: " + response.getStatusCode)
      if (response.isExpected) {
        val actionHandler = response.getObject
        actionHandler match {
          case actionWrapper: ActionWrapper =>
            val actionResponses = actionWrapper.getData

            for (actionResponse <- actionResponses) {
              actionResponse match {
                case successResponse: 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)
                case exception: 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)
                case _ =>
              }
            }
          case exception: 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)
          case _ =>
        }
      }
      else {
        val responseObject = response.getModel
        val clas = responseObject.getClass
        val fields = clas.getDeclaredFields
        for (field <- fields) {
          println(field.getName + ":" + field.get(responseObject))
        }
      }
    }
  }
}
class Relatedrecord {}
 
Get a Related Record

              
package com.zoho.crm.sample.relatedrecords

import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.io.OutputStream
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util

import com.zoho.crm.api.HeaderMap
import com.zoho.crm.api.ParameterMap
import com.zoho.crm.api.attachments.Attachment
import com.zoho.crm.api.layouts.Layout
import com.zoho.crm.api.record.{Comment, Consent, FileDetails, LineTax, Participants, PricingDetails, Record, RecurringActivity, RemindAt}
import com.zoho.crm.api.relatedrecords.FileBodyWrapper
import com.zoho.crm.api.relatedrecords.APIException
import com.zoho.crm.api.relatedrecords.ActionHandler
import com.zoho.crm.api.relatedrecords.ActionResponse
import com.zoho.crm.api.relatedrecords.ActionWrapper
import com.zoho.crm.api.relatedrecords.BodyWrapper
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.DelinkRecordsParam
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsParam
import com.zoho.crm.api.relatedrecords.ResponseHandler
import com.zoho.crm.api.relatedrecords.ResponseWrapper
import com.zoho.crm.api.relatedrecords.SuccessResponse
import com.zoho.crm.api.tags.Tag
import com.zoho.crm.api.users.User
import com.zoho.crm.api.util.APIResponse
import com.zoho.crm.api.util.Choice
import com.zoho.crm.api.util.Model
import com.zoho.crm.api.util.StreamWrapper

import scala.collection.mutable.ArrayBuffer


object Relatedrecord {
/**
   *  Get Related Record 
   * This method is used to get the single related list record and print the response.
   *
   * @param moduleAPIName      - The API Name of the module to get related record.
   * @param recordId           - The ID of the record to be get Related List.
   * @param relatedListAPIName - The API name of the related list. To get the API name of the related list.
   * @param relatedListId      - The ID of the related record.
   * @param destinationFolder  - The absolute path of the destination folder to store the file.
   * @throws Exception
   */
  @throws[Exception]
  def getRelatedRecord(moduleAPIName: String, recordId: Long, relatedListAPIName: String, relatedListId: Long, destinationFolder: String): Unit = { //Long relatedListId = 3477061000004994115l
    val relatedRecordsOperations = new RelatedRecordsOperations(relatedListAPIName, moduleAPIName)
    val headerInstance = new HeaderMap
    val startdatetime = OffsetDateTime.of(2019, 6, 1, 10, 0, 1, 0, ZoneOffset.of("+05:30"))
    headerInstance.add(new GetRelatedRecordHeader().IfModifiedSince, startdatetime)
    //Call getRelatedRecord method that takes headerInstance and relatedRecordId as parameter
    val responseOption = relatedRecordsOperations.getRelatedRecord(relatedListId, recordId, Option(headerInstance ))
    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
      }
      //Check if expected response is received
      if (response.isExpected) { //Get object from response
        val responseHandler = response.getObject
        responseHandler match {
          case responseWrapper: ResponseWrapper => //Get the received ResponseWrapper instance
            //Get the list of obtained Record instances
            val records = responseWrapper.getData

            for (record <- records) { //Get the ID of each Record
              println("RelatedRecord ID: " + record.getId)
              //Get the createdBy User instance of each Record
              var createdByOption = record.getCreatedBy
              //Check if createdBy is not null
              if (createdByOption.isDefined) { //Get the ID of the createdBy User
                var createdBy = createdByOption.get
                println("RelatedRecord Created By User-ID: " + createdBy.getId)
                //Get the name of the createdBy User
                println("RelatedRecord Created By User-Name: " + createdBy.getName)
                //Get the Email of the createdBy User
                println("RelatedRecord Created By User-Email: " + createdBy.getEmail)
              }
              //Get the CreatedTime of each Record
              println("RelatedRecord CreatedTime: " + record.getCreatedTime)
              //Get the modifiedBy User instance of each Record
              var modifiedByOption = record.getModifiedBy
              //Check if modifiedBy is not null
              if (modifiedByOption.isDefined) { //Get the ID of the modifiedBy User
                var modifiedBy = modifiedByOption.get
                println("RelatedRecord Modified By User-ID: " + modifiedBy.getId)
                //Get the name of the modifiedBy User
                println("RelatedRecord Modified By User-Name: " + modifiedBy.getName)
                //Get the Email of the modifiedBy User
                println("RelatedRecord Modified By User-Email: " + modifiedBy.getEmail)
              }
              //Get the ModifiedTime of each Record
              println("RelatedRecord ModifiedTime: " + record.getModifiedTime)
              //Get the list of Tag instance each Record
              val tags = record.getTag
              //Check if tags is not null
              if (tags.length > 0) {

                for (tag <- tags) { //Get the Name of each Tag
                  println("RelatedRecord Tag Name: " + tag.getName)
                  //Get the Id of each Tag
                  println("RelatedRecord Tag ID: " + tag.getId)
                }
              }
              //To get particular field value
              println("RelatedRecord Field Value: " + record.getKeyValue("Last_Name")) // FieldApiName

              println("RelatedRecord KeyValues: ")
              //Get the KeyValue map
              record.getKeyValues.foreach(entry => {
                val keyName = entry._1
                var value = entry._2
                value match {
                  case value2: Option[_] => value = value2.getOrElse(null)
                  case _ =>
                }
                value match {
                  case dataList: ArrayBuffer[Any] =>
                    if (dataList.size > 0) dataList(0) match {
                      case _: FileDetails =>
                        @SuppressWarnings(Array("unchecked")) val fileDetails = value.asInstanceOf[ArrayBuffer[FileDetails]]

                        for (fileDetail <- fileDetails) { //Get the Extn of each FileDetails
                          println("RelatedRecord FileDetails Extn: " + fileDetail.getExtn)
                          //Get the IsPreviewAvailable of each FileDetails
                          println("RelatedRecord FileDetails IsPreviewAvailable: " + fileDetail.getIsPreviewAvailable)
                          //Get the DownloadUrl of each FileDetails
                          println("RelatedRecord FileDetails DownloadUrl: " + fileDetail.getDownloadUrl)
                          //Get the DeleteUrl of each FileDetails
                          println("RelatedRecord FileDetails DeleteUrl: " + fileDetail.getDeleteUrl)
                          //Get the EntityId of each FileDetails
                          println("RelatedRecord FileDetails EntityId: " + fileDetail.getEntityId)
                          //Get the Mode of each FileDetails
                          println("RelatedRecord FileDetails Mode: " + fileDetail.getMode)
                          //Get the OriginalSizeByte of each FileDetails
                          println("RelatedRecord FileDetails OriginalSizeByte: " + fileDetail.getOriginalSizeByte)
                          //Get the PreviewUrl of each FileDetails
                          println("RelatedRecord FileDetails PreviewUrl: " + fileDetail.getPreviewUrl)
                          //Get the FileName of each FileDetails
                          println("RelatedRecord FileDetails FileName: " + fileDetail.getFileName)
                          //Get the FileId of each FileDetails
                          println("RelatedRecord FileDetails FileId: " + fileDetail.getFileId)
                          //Get the AttachmentId of each FileDetails
                          println("RelatedRecord FileDetails AttachmentId: " + fileDetail.getAttachmentId)
                          //Get the FileSize of each FileDetails
                          println("RelatedRecord FileDetails FileSize: " + fileDetail.getFileSize)
                          //Get the CreatorId of each FileDetails
                          println("RelatedRecord FileDetails CreatorId: " + fileDetail.getCreatorId)
                          //Get the LinkDocs of each FileDetails
                          println("RelatedRecord FileDetails LinkDocs: " + fileDetail.getLinkDocs)
                        }
                      case _: Choice[_] =>
                        @SuppressWarnings(Array("unchecked")) val choiceList = dataList.asInstanceOf[(ArrayBuffer[Choice[_$1]]) forSome {type _$1}]
                        println(keyName)
                        println("values")

                        for (choice <- choiceList) {
                          println(choice.getValue)
                        }
                      case _: Tag =>
                        @SuppressWarnings(Array("unchecked")) val tagList = value.asInstanceOf[ArrayBuffer[Tag]]

                        for (tag <- tagList) {
                          println("RelatedRecord Tag Name: " + tag.getName)
                          println("RelatedRecord Tag ID: " + tag.getId)
                        }
                      case _: PricingDetails =>
                        @SuppressWarnings(Array("unchecked")) val pricingDetails = value.asInstanceOf[ArrayBuffer[PricingDetails]]

                        for (pricingDetail <- pricingDetails) {
                          println("RelatedRecord PricingDetails ToRange: " + pricingDetail.getToRange.toString)
                          println("RelatedRecord PricingDetails Discount: " + pricingDetail.getDiscount.toString)
                          println("RelatedRecord PricingDetails ID: " + pricingDetail.getId)
                          println("RelatedRecord PricingDetails FromRange: " + pricingDetail.getFromRange.toString)
                        }
                      case _: Participants =>
                        val participants = value.asInstanceOf[ArrayBuffer[Participants]]
                        for (participant <- participants) {
                          println("RelatedRecord Participants Name: " + participant.getName().toString)
                          println("RelatedRecord Participants Invited: " + participant.getInvited().toString())
                          println("RelatedRecord Participants ID: " + participant.getId().toString)
                          println("RelatedRecord Participants Type: " + participant.getType().toString)
                          println("RelatedRecord Participants Participant: " + participant.getParticipant().toString)
                          println("RelatedRecord Participants Status: " + participant.getStatus().toString)
                        }
                      case _: Record =>
                        @SuppressWarnings(Array("unchecked")) val recordList = dataList.asInstanceOf[ArrayBuffer[Record]]

                        for (record1 <- recordList) { //Get the details map


                          record1.getKeyValues.foreach(entry => {
                            println(entry._1 + ": " + entry._2)
                          })
                        }
                      case _: LineTax =>
                        @SuppressWarnings(Array("unchecked")) val lineTaxes = dataList.asInstanceOf[ArrayBuffer[LineTax]]

                        for (lineTax <- lineTaxes) {
                          println("RelatedRecord ProductDetails LineTax Percentage: " + lineTax.getPercentage.toString)
                          println("RelatedRecord ProductDetails LineTax Name: " + lineTax.getName)
                          println("RelatedRecord ProductDetails LineTax Id: " + lineTax.getId)
                          println("RelatedRecord ProductDetails LineTax Value: " + lineTax.getValue.toString)
                        }
                      case _: Comment =>
                        @SuppressWarnings(Array("unchecked")) val comments = dataList.asInstanceOf[ArrayBuffer[Comment]]

                        for (comment <- comments) {
                          println("RelatedRecord Comment CommentedBy: " + comment.getCommentedBy)
                          println("RelatedRecord Comment CommentedTime: " + comment.getCommentedTime.toString)
                          println("RelatedRecord Comment CommentContent: " + comment.getCommentContent)
                          println("RelatedRecord Comment Id: " + comment.getId)
                        }
                      case _: Attachment => //Get the list of obtained Attachment instances
                        @SuppressWarnings(Array("unchecked")) val attachments = dataList.asInstanceOf[ArrayBuffer[Attachment]]


                        for (attachment <- attachments) { //Get the owner User instance of each attachment
                          val ownerOption = attachment.getOwner
                          //Check if owner is not null
                          if (ownerOption.isDefined) { //Get the Name of the Owner
                            var owner = ownerOption.get
                            println("RelatedRecord Attachment Owner User-Name: " + owner.getName)
                            //Get the ID of the Owner
                            println("RelatedRecord Attachment Owner User-ID: " + owner.getId)
                            //Get the Email of the Owner
                            println("RelatedRecord Attachment Owner User-Email: " + owner.getEmail)
                          }
                          //Get the modified time of each attachment
                          println("RelatedRecord Attachment Modified Time: " + attachment.getModifiedTime.toString)
                          //Get the name of the File
                          println("RelatedRecord Attachment File Name: " + attachment.getFileName)
                          //Get the created time of each attachment
                          println("RelatedRecord Attachment Created Time: " + attachment.getCreatedTime.toString)
                          //Get the Attachment file size
                          println("RelatedRecord Attachment File Size: " + attachment.getSize.toString)
                          //Get the parentId Record instance of each attachment
                          val parentIdOption = attachment.getParentId
                          //Check if parentId is not null
                          if (parentIdOption.isDefined) { //Get the parent record Name of each attachment
                            var parentId = parentIdOption.get
                            println("RelatedRecord Attachment parent record Name: " + parentId.getKeyValue("name"))
                            //Get the parent record ID of each attachment
                            println("RelatedRecord Attachment parent record ID: " + parentId.getId)
                          }
                          //Get the attachment is Editable
                          println("RelatedRecord Attachment is Editable: " + attachment.geteditable().toString)
                          //Get the file ID of each attachment
                          println("RelatedRecord Attachment File ID: " + attachment.getfileId())
                          //Get the type of each attachment
                          println("RelatedRecord Attachment File Type: " + attachment.gettype)
                          //Get the seModule of each attachment
                          println("RelatedRecord Attachment seModule: " + attachment.getseModule())
                          //Get the modifiedBy User instance of each attachment
                          var modifiedByOption = attachment.getModifiedBy
                          if (modifiedByOption.isDefined) { //Get the Name of the modifiedBy User
                            var modifiedBy = modifiedByOption.get
                            println("RelatedRecord Attachment Modified By User-Name: " + modifiedBy.getName)
                            println("RelatedRecord Attachment Modified By User-ID: " + modifiedBy.getId)
                            println("RelatedRecord Attachment Modified By User-Email: " + modifiedBy.getEmail)
                          }
                          //Get the state of each attachment
                          println("RelatedRecord Attachment State: " + attachment.getstate)
                          //Get the ID of each attachment
                          println("RelatedRecord Attachment ID: " + attachment.getId)
                          //Get the createdBy User instance of each attachment
                          var createdByOption = attachment.getCreatedBy
                          if (createdByOption.isDefined) {
                            var createdBy = modifiedByOption.get

                            println("RelatedRecord Attachment Created By User-Name: " + createdBy.getName)
                            println("RelatedRecord Attachment Created By User-ID: " + createdBy.getId)
                            println("RelatedRecord Attachment Created By User-Email: " + createdBy.getEmail)
                          }
                          //Get the linkUrl of each attachment
                          println("RelatedRecord Attachment LinkUrl: " + attachment.getlinkUrl())
                        }
                      case _ => println(keyName + ": " + value)
                    }
                  case recordValue: Record =>
                    println("RelatedRecord " + keyName + " ID: " + recordValue.getId)
                    println("RelatedRecord " + keyName + " Name: " + recordValue.getKeyValue("name"))
                  case layout: Layout =>
                    if (layout != null) {
                      println("RelatedRecord " + keyName + " ID: " + layout.getId)
                      println("RelatedRecord " + keyName + " Name: " + layout.getName)
                    }
                  case user: User =>
                    if (user != null) {
                      println("RelatedRecord " + keyName + " User-ID: " + user.getId)
                      println("RelatedRecord " + keyName + " User-Name: " + user.getName)
                      println("RelatedRecord " + keyName + " User-Email: " + user.getEmail)
                    }
                  case value1: Choice[_] => println(keyName + ": " + value1.getValue)
                  case at: RemindAt => println(keyName + ": " + at.getAlarm)
                  case activity: RecurringActivity =>
                    println(keyName)
                    println("RRULE" + ": " + activity.getRrule)
                  case consent: Consent =>
                    println("RelatedRecord Consent ID: " + consent.getId)
                    //Get the Owner User instance of each attachment
                    val ownerOption = consent.getOwner
                    if (ownerOption.isDefined) { //Get the name of the owner User
                      var owner = ownerOption.get
                      println("RelatedRecord Consent Owner Name: " + owner.getName)
                      //Get the ID of the owner User
                      println("RelatedRecord Consent Owner ID: " + owner.getId)
                      //Get the Email of the owner User
                      println("RelatedRecord Consent Owner Email: " + owner.getEmail)
                    }
                    val consentCreatedByOption = consent.getCreatedBy
                    if (consentCreatedByOption.isDefined) { //Get the name of the CreatedBy User
                      var consentCreatedBy = consentCreatedByOption.get
                      println("RelatedRecord Consent CreatedBy Name: " + consentCreatedBy.getName)
                      //Get the ID of the CreatedBy User
                      println("RelatedRecord Consent CreatedBy ID: " + consentCreatedBy.getId)
                      //Get the Email of the CreatedBy User
                      println("RelatedRecord Consent CreatedBy Email: " + consentCreatedBy.getEmail)
                    }
                    val consentModifiedByOption = consent.getModifiedBy
                    if (consentModifiedByOption.isDefined) { //Get the name of the ModifiedBy User
                      var consentModifiedBy = consentModifiedByOption.get
                      println("RelatedRecord Consent ModifiedBy Name: " + consentModifiedBy.getName)
                      //Get the ID of the ModifiedBy User
                      println("RelatedRecord Consent ModifiedBy ID: " + consentModifiedBy.getId)
                      //Get the Email of the ModifiedBy User
                      println("RelatedRecord Consent ModifiedBy Email: " + consentModifiedBy.getEmail)
                    }
                    println("RelatedRecord Consent CreatedTime: " + consent.getCreatedTime)
                    println("RelatedRecord Consent ModifiedTime: " + consent.getModifiedTime)
                    println("RelatedRecord Consent ContactThroughEmail: " + consent.getContactThroughEmail)
                    println("RelatedRecord Consent ContactThroughSocial: " + consent.getContactThroughSocial)
                    println("RelatedRecord Consent ContactThroughSurvey: " + consent.getContactThroughSurvey)
                    println("RelatedRecord Consent ContactThroughPhone: " + consent.getContactThroughPhone)
                    println("RelatedRecord Consent MailSentTime: " + consent.getMailSentTime.toString)
                    println("RelatedRecord Consent ConsentDate: " + consent.getConsentDate.toString)
                    println("RelatedRecord Consent ConsentRemarks: " + consent.getConsentRemarks)
                    println("RelatedRecord Consent ConsentThrough: " + consent.getConsentThrough)
                    println("RelatedRecord Consent DataProcessingBasis: " + consent.getDataProcessingBasis)
                    //To get custom values
                    println("RelatedRecord Consent Lawful Reason: " + consent.getKeyValue("Lawful_Reason"))
                  case _ => println(keyName + ": " + value)
                }
              })
            }
          case fileBodyWrapper: FileBodyWrapper =>
            //Get StreamWrapper instance from the returned FileBodyWrapper instance
            val streamWrapper = fileBodyWrapper.getFile.get
            //Create a file instance with the absolute_file_path
            val file = new File(destinationFolder + File.separatorChar + streamWrapper.getName.get)
            //Get InputStream from the response
            val is = streamWrapper.getStream.get
            //Create an OutputStream for the destination file
            val os = new FileOutputStream(file)
            val buffer = new Array[Byte](1024)
            var bytesRead = 0
            //read the InputStream till the end
            while ( {
              (bytesRead = is.read(buffer)) != -1
            }) { //write data to OutputStream
              os.write(buffer, 0, bytesRead)
            }
            //Close the InputStream
            is.close()
            //Flush and close the OutputStream
            os.flush()
            os.close()
          case _ => //Check if the request returned an exception
            responseHandler match {
              case exception: APIException => //Get the received APIException instance
                //Get the Status
                println("Status: " + exception.getStatus.getValue)
                //Get the Code
                println("Code: " + exception.getCode.getValue)
                println("Details: ")

                exception.getDetails.foreach(entry => {
                  println(entry._1 + ": " + entry._2)
                })
                //Get the Message
                println("Message: " + exception.getMessage.getValue)
              case _ =>
            }
        }
      }
      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))
        }
      }
    }
  }
}
class Relatedrecord {}
 
Update a Related Record

              
package com.zoho.crm.sample.relatedrecords

import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.io.OutputStream
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util

import com.zoho.crm.api.HeaderMap
import com.zoho.crm.api.ParameterMap
import com.zoho.crm.api.attachments.Attachment
import com.zoho.crm.api.layouts.Layout
import com.zoho.crm.api.record.{Comment, Consent, FileDetails, LineTax, Participants, PricingDetails, Record, RecurringActivity, RemindAt}
import com.zoho.crm.api.relatedrecords.FileBodyWrapper
import com.zoho.crm.api.relatedrecords.APIException
import com.zoho.crm.api.relatedrecords.ActionHandler
import com.zoho.crm.api.relatedrecords.ActionResponse
import com.zoho.crm.api.relatedrecords.ActionWrapper
import com.zoho.crm.api.relatedrecords.BodyWrapper
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.DelinkRecordsParam
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsParam
import com.zoho.crm.api.relatedrecords.ResponseHandler
import com.zoho.crm.api.relatedrecords.ResponseWrapper
import com.zoho.crm.api.relatedrecords.SuccessResponse
import com.zoho.crm.api.tags.Tag
import com.zoho.crm.api.users.User
import com.zoho.crm.api.util.APIResponse
import com.zoho.crm.api.util.Choice
import com.zoho.crm.api.util.Model
import com.zoho.crm.api.util.StreamWrapper

import scala.collection.mutable.ArrayBuffer


object Relatedrecord {
/**
   *  Update Related Record 
   * This method is used to update the relation between the records and print the response.
   *
   * @param moduleAPIName      - The API Name of the module to update related record.
   * @param recordId           - The ID of the record to be obtained.
   * @param relatedListAPIName - The API name of the related list. To get the API name of the related list.
   * @param relatedListId      - The ID of the related record.
   * @throws Exception
   */
  @throws[Exception]
  def updateRelatedRecord(moduleAPIName: String, recordId: Long, relatedListAPIName: String, relatedListId: Long): Unit = {
    val relatedRecordsOperations =new RelatedRecordsOperations(relatedListAPIName, moduleAPIName)
    val request = new BodyWrapper
    val records = new ArrayBuffer[Record]
    val record1 = new Record
    record1.addKeyValue("Note_Content", "asd")
    records.addOne(record1)
    request.setData(records)
    //Call updateRelatedRecord method that takes BodyWrapper instance, relatedRecordId as parameter.
    val responseOption = relatedRecordsOperations.updateRelatedRecord(relatedListId, recordId, request)
    if (responseOption.isDefined) {
      var response= responseOption.get
      println("Status Code: " + response.getStatusCode)
      if (response.isExpected) {
        val actionHandler = response.getObject
        actionHandler match {
          case actionWrapper: ActionWrapper =>
            val actionResponses = actionWrapper.getData

            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)
              }
            }
          case exception: 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)
          case _ =>
        }
      }
      else {
        val responseObject = response.getModel
        val clas = responseObject.getClass
        val fields = clas.getDeclaredFields
        for (field <- fields) {
          println(field.getName + ":" + field.get(responseObject))
        }
      }
    }
  }
}
class Relatedrecord {}
 

              
package com.zoho.crm.sample.relatedrecords

import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.io.OutputStream
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util

import com.zoho.crm.api.HeaderMap
import com.zoho.crm.api.ParameterMap
import com.zoho.crm.api.attachments.Attachment
import com.zoho.crm.api.layouts.Layout
import com.zoho.crm.api.record.{Comment, Consent, FileDetails, LineTax, Participants, PricingDetails, Record, RecurringActivity, RemindAt}
import com.zoho.crm.api.relatedrecords.FileBodyWrapper
import com.zoho.crm.api.relatedrecords.APIException
import com.zoho.crm.api.relatedrecords.ActionHandler
import com.zoho.crm.api.relatedrecords.ActionResponse
import com.zoho.crm.api.relatedrecords.ActionWrapper
import com.zoho.crm.api.relatedrecords.BodyWrapper
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.DelinkRecordsParam
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsHeader
import com.zoho.crm.api.relatedrecords.RelatedRecordsOperations.GetRelatedRecordsParam
import com.zoho.crm.api.relatedrecords.ResponseHandler
import com.zoho.crm.api.relatedrecords.ResponseWrapper
import com.zoho.crm.api.relatedrecords.SuccessResponse
import com.zoho.crm.api.tags.Tag
import com.zoho.crm.api.users.User
import com.zoho.crm.api.util.APIResponse
import com.zoho.crm.api.util.Choice
import com.zoho.crm.api.util.Model
import com.zoho.crm.api.util.StreamWrapper

import scala.collection.mutable.ArrayBuffer


object Relatedrecord {
/**
   *  Delink Record 
   * This method is used to delete the association between modules and print the response.
   *
   * @param moduleAPIName      - The API Name of the module to delink related record.
   * @param recordId           - The ID of the record to be obtained.
   * @param relatedListAPIName - The API name of the related list. To get the API name of the related list.
   * @param relatedListId      - The ID of the related record.
   * @throws Exception
   */
  @throws[Exception]
  def delinkRecord(moduleAPIName: String, recordId: Long, relatedListAPIName: String, relatedListId: Long): Unit = {
    val relatedRecordsOperations = new RelatedRecordsOperations(relatedListAPIName, moduleAPIName)
    //Call delinkRecord method that takes relatedListId as parameter.
    val responseOption = relatedRecordsOperations.delinkRecord(relatedListId, recordId)
    if (responseOption.isDefined) {
      var response= responseOption.get
      println("Status Code: " + response.getStatusCode)
      if (response.isExpected) {
        val actionHandler = response.getObject
        actionHandler match {
          case actionWrapper: ActionWrapper =>
            val actionResponses = actionWrapper.getData

            for (actionResponse <- actionResponses) {
              actionResponse match {
                case successResponse: 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)
                case exception: 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)
                case _ =>
              }
            }
          case exception: 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)
          case _ =>
        }
      }
      else {
        val responseObject = response.getModel
        val clas = responseObject.getClass
        val fields = clas.getDeclaredFields
        for (field <- fields) {
          println(field.getName + ":" + field.get(responseObject))
        }
      }
    }
  }
}
class Relatedrecord {}