Skip to main content

Python SDK Samples - Bulk Read Operations

Create a Bulk Read Job
              
              
import os
from zcrmsdk.src.com.zoho.crm.api.bulk_read import *
from zcrmsdk.src.com.zoho.crm.api.util import Choice
class BulkRead(object):
    @staticmethod
    def create_bulk_read_job(module_api_name):
        """
        This method is used to create a bulk read job to export records.
        :param module_api_name: The API Name of the record's module
        """

        """
        example
        module_api_name = 'Leads'
        """

        # Get instance of BulkReadOperations Class
        bulk_read_operations = BulkReadOperations()

        # Get instance of RequestWrapper Class that will contain the request body
        request = RequestWrapper()

        # Get instance of CallBack Class
        call_back = CallBack()

        # Set valid callback URL
        call_back.set_url("https://www.example.com/callback")

        # Set the HTTP method of the callback URL. The allowed value is post.
        call_back.set_method(Choice('post'))

        # The Bulk Read Job's details is posted to this URL on successful completion / failure of the job.
        request.set_callback(call_back)

        # Get instance of Query Class
        query = Query()

        # Specifies the API Name of the module to be read.
        module = Module()
        module.set_api_name(module_api_name)
        query.set_module(module)

        # Specifies the unique ID of the custom view, whose records you want to export.
        # query.set_cvid('34096430087501')

        # List of field names
        field_api_names = ['Last_Name']

        # Specifies the API Name of the fields to be fetched
        # query.set_fields(field_api_names)

        # To set page value, By default value is 1.
        query.set_page(1)

        # Get instance of Criteria Class
        criteria = Criteria()

        # To set comparator(eg: equal, greater_than)
        criteria.set_group_operator(Choice('or'))

        criteria_array = []

        group11 = Criteria()

        group11.set_group_operator(Choice("and"))

        group_array11 = []

        group111 = Criteria()

        field1 = Field()

        field1.set_api_name("Company")

        group111.set_field(field1)

        group111.set_comparator(Choice("equal"))

        group111.set_value("Zoho")

        group_array11.append(group111)

        group112 = Criteria()

        field2 = Field()

        field2.set_api_name("Owner")

        group112.set_field(field2)

        group112.set_comparator(Choice("in"))

        group112.set_value(["34770610173021"])

        group_array11.append(group112)

        group11.set_group(group_array11)

        criteria_array.append(group11)

        group12 = Criteria()

        group12.set_group_operator(Choice("or"))

        group_array12 = []

        group121 = Criteria()

        field3 = Field()

        field3.set_api_name("Paid")

        group121.set_field(field3)

        group121.set_comparator(Choice("equal"))

        group121.set_value(True)

        group_array12.append(group121)

        group122 = Criteria()

        field4 = Field()

        field4.set_api_name("Created_Time")

        group122.set_field(field4)

        group122.set_comparator(Choice("between"))

        time = ["2020-06-03T17:31:48+05:30", "2020-06-03T17:31:48+05:30"]

        # To set the value to be compared
        group122.set_value(time)

        group_array12.append(group122)

        group12.set_group(group_array12)

        criteria_array.append(group12)

        criteria.set_group(criteria_array)

        # To filter the records to be exported
        query.set_criteria(criteria)

        # Set the query object
        request.set_query(query)

        # Specify the value for this key as "ics" to export all records in the Events module as an ICS file.
        # request.set_file_type(Choice('ics'))

        # Call create_bulk_read_job method that takes RequestWrapper instance as parameter
        response = bulk_read_operations.create_bulk_read_job(request)

        if response is not None:
            # Get the status code from response
            print('Status Code: ' + str(response.get_status_code()))

            # Get object from response
            response_object = response.get_object()

            if response_object is not None:

                # Check if expected ActionWrapper instance is received.
                if isinstance(response_object, ActionWrapper):
                    action_response_list = response_object.get_data()

                    for action_response in action_response_list:

                        # Check if the request is successful
                        if isinstance(action_response, SuccessResponse):
                            # Get the Status
                            print("Status: " +
                                  action_response.get_status().get_value())

                            # Get the Code
                            print("Code: " + action_response.get_code().get_value())

                            print("Details")

                            # Get the details dict
                            details = action_response.get_details()

                            for key, value in details.items():
                                print(key + ' : ' + str(value))

                            # Get the Message
                            print("Message: " +
                                  action_response.get_message().get_value())

                        # Check if the request returned an exception
                        elif isinstance(action_response, APIException):
                            # Get the Status
                            print("Status: " +
                                  action_response.get_status().get_value())

                            # Get the Code
                            print("Code: " + action_response.get_code().get_value())

                            print("Details")

                            # Get the details dict
                            details = action_response.get_details()

                            for key, value in details.items():
                                print(key + ' : ' + str(value))

                            # Get the Message
                            print("Message: " +
                                  action_response.get_message().get_value())

                # Check if the request returned an exception
                elif isinstance(response_object, APIException):
                    # Get the Status
                    print("Status: " + response_object.get_status().get_value())

                    # Get the Code
                    print("Code: " + response_object.get_code().get_value())

                    print("Details")

                    # Get the details dict
                    details = response_object.get_details()

                    for key, value in details.items():
                        print(key + ' : ' + str(value))

                    # Get the Message
                    print("Message: " + response_object.get_message().get_value())
 
Get Bulk Read Job Details
Download Result
              
              
import os
from zcrmsdk.src.com.zoho.crm.api.bulk_read import *
from zcrmsdk.src.com.zoho.crm.api.util import Choice
class BulkRead(object):
    @staticmethod
    def 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
        bulk_read_operations = BulkReadOperations()

        # Call download_result method that takes job_id as parameter
        response = bulk_read_operations.download_result(job_id)

        if response is not None:

            # Get the status code from response
            print('Status Code: ' + str(response.get_status_code()))

            if response.get_status_code() in [204, 304]:
                print('No Content' if response.get_status_code()
                      == 204 else 'Not Modified')
                return

            # Get object from response
            response_object = response.get_object()

            if response_object is not None:

                # Check if expected FileBodyWrapper instance is received.
                if isinstance(response_object, FileBodyWrapper):

                    # Get StreamWrapper instance from the returned FileBodyWrapper instance
                    stream_wrapper = response_object.get_file()

                    # Construct the file name by joining the destinationFolder and the name from StreamWrapper instance
                    file_name = os.path.join(
                        destination_folder, stream_wrapper.get_name())

                    # Open the destination file where the file needs to be written in 'wb' mode
                    with open(file_name, 'wb') as f:
                        # Get the stream from StreamWrapper instance
                        for chunk in stream_wrapper.get_stream():
                            f.write(chunk)

                        f.close()

                # Check if the request returned an exception
                elif isinstance(response_object, APIException):
                    # Get the Status
                    print("Status: " + response_object.get_status().get_value())

                    # Get the Code
                    print("Code: " + response_object.get_code().get_value())

                    print("Details")

                    # Get the details dict
                    details = response_object.get_details()

                    for key, value in details.items():
                        print(key + ' : ' + str(value))

                    # Get the Message
                    print("Message: " + response_object.get_message().get_value())