Skip to main content

Ruby SDK Samples - Bulk Read Operations

Create a Bulk Read Job
Get Bulk Read Job Details
              
              
require 'ZOHOCRMSDK2_0'
class Bulkread
    def self.get_bulkread_job_details(job_id)
    # """
    # This method is used to get the details of a bulk read job performed previously.
    # :param job_id: The unique ID of the bulk read job.
    # """

    # """
    # example
    # job_id = '34096432461001'
    # """
    # Get instance of BulkReadOperations Class
    bro = ZOHOCRMSDK::BulkRead::BulkReadOperations.new
    # Call get_bulk_read_job_details method that takes jobId as parameter
    response = bro.get_bulk_read_job_details(job_id)
    unless response.nil?
      status_code = response.status_code
      # Get the status code from response
      print "\n Status Code :" + status_code.to_s
      if [204, 304].include? status_code
        print(status_code == 204 ? 'No Content' : 'Not Modified')
        return
      end
      # Check if expected instance is received.
      if response.is_expected
        # Get object from response
        response_handler = response.data_object
        # Check if expected ResponseWrapper instance is received
        if response_handler.is_a? ZOHOCRMSDK::BulkRead::ResponseWrapper
          # Get the list of JobDetail instances
          job_details = response_handler.data
          job_details.each do |job_detail|
            # Get the Job ID of each jobDetail
            print "\nBulk read Job ID: "
            print job_detail.id.to_s
            # Get the Operation of each jobDetail
            print "\nBulk read Operation: "
            print job_detail.operation
            # Get the Result instance of each jobDetail
            print "\nBulk read State: "
            print job_detail.state.value
            print "\n"
            # Get the Result instance of each jobDetail
            result = job_detail.result
            unless result.nil?
              # Get the Page of the Result
              print "\nBulkread Result Page:"
              print result.page
              # Get the Count of the Result
              print "\nBulkread Result Count:"
              print result.count
              # Get the Download URL of the Result
              print "\nBulkread Result Download URL:"
              print result.download_url
              # Get the Per_Page of the Result
              print "\nBulkread Result Per_Page:"
              print result.per_page
              # Get the MoreRecords of the Result
              print "\nBulkread Result MoreRecords:"
              print result.more_records
            end
            # Get the Query instance of each jobDetail
            query = job_detail.query
            unless query.nil?
              # Get the Module Name of the Query
              print "\nBulkread Query Module:"
              print query.module
              # Get the Page of the Query
              print "\nBulkread Query Page:"
              print query.page
              # Get the cvid of the Query
              print "\nBulkread  Query cvid:"
              print query.cvid
              # Get the fields List of the Query
              fields = query.fields
              fields&.each do |field_name|
                print "\nBulk read Query Fields:"
                print field_name
              end
              # Get the Criteria instance of the Query
              criteria = query.criteria
              print_criteria(criteria) unless criteria.nil?
            end
            # Get the CreatedBy User instance of each jobDetail
            created_by = job_detail.created_by
            # Check if created_by is not None
            unless created_by.nil?
              # Get the Name of the created_by User
              print "\nBulkread Created By User-ID:"
              print created_by.id.to_s
              # Get the ID of the created_by User
              print "\nBulkread Created By user-Name:"
              print created_by.name
            end
            # Get the CreatedTime of each jobDetail
            print "\nBulkread CreatedTime:"
            print job_detail.created_time
            # Get the FileType of each jobDetail
            print "\nBulkread File Type:"
            print job_detail.file_type
          end
        elsif response_handler.is_a? ZOHOCRMSDK::BulkRead::APIException
          exception = response_handler
          # Get the Code
          print 'code:'
          print exception.code.value
          # Get the Status
          print "\n status:"
          print exception.status.value
          # Get the Message
          print "\n message:"
          print exception.message.value
          # Get the details map
          exception.details.each do |k, v|
            print "\n"
            print k
            print v
            print "\n"
          end
          print "\n"
        end
      else status_code != 204
           response_object = response.data_object
           response_object.instance_variables.each do |field|
             print field
             print "\n"
             print response_object.instance_variable_get(field)
           end
      end
    end
  end
end
 
Download Result
              
              
require 'ZOHOCRMSDK2_0'
class Bulkread
    def self.download_result(job_id, destination_folder)
    # """
    # This method is used to download the result of Bulk Read operation
    # :param job_id: The unique ID of the bulk read job.
    # :param destination_folder: The absolute path where downloaded file has to be stored.
    # """

    # """
    # example
    # job_id = '34096432461001'
    # """
    # Get instance of BulkReadOperations Class
    bro = ZOHOCRMSDK::BulkRead::BulkReadOperations.new
    # Call download_result method that takes job_id as parameter
    response = bro.download_result(job_id)
    unless response.nil?
      status_code = response.status_code
      # Get the status code from response
      print "\n Status Code :" + status_code.to_s
      if [204, 304].include? status_code
        print(status_code == 204 ? 'No Content' : 'Not Modified')
        return
      end
      # Check if expected instance is received.
      if response.is_expected
        # Get object from response
        response_handler = response.data_object
        # Check if expected FileBodyWrapper instance is received.
        if response_handler.is_a? ZOHOCRMSDK::BulkRead::FileBodyWrapper
          file_body_wrapper = response_handler
          # Get StreamWrapper instance from the returned FileBodyWrapper instance
          stream_wrapper = file_body_wrapper.file
          # Construct the file name by joining the destinationFolder and the name from StreamWrapper instance
          # Open the destination file where the file needs to be written in 'w' mode
          File.open(destination_folder + '/' + stream_wrapper.name, 'w') do |f|
            f.write(stream_wrapper.stream)
          end
        # Check if the request returned an exception`
        elsif response_handler.is_a? ZOHOCRMSDK::BulkRead::APIException
          exception = response_handler
          # Get the Code
          print 'code:'
          print exception.code.value
          # Get the Status
          print "\n status:"
          print exception.status.value
          # Get the Message
          print "\n message:"
          print exception.message.value
          # Get the Details map
          exception.details.each do |k, v|
            print "\n"
            print k
            print v
            print "\n"
          end
          print "\n"
        end
      else
        response_object = response.data_object
        response_object.instance_variables.each do |field|
          print field
          print "\n"
          print response_object.instance_variable_get(field)
        end
      end
    end
  end
end
module.exports = {BulkRead}