Skip to main content

Python SDK Samples - Attachment Operations

Get Attachments
Upload Attachments
              
              
from zcrmsdk.src.com.zoho.crm.api.attachments import *
from zcrmsdk.src.com.zoho.crm.api.util import StreamWrapper
from zcrmsdk.src.com.zoho.crm.api import ParameterMap
import os
class Attachment(object):
    @staticmethod
    def upload_attachments(module_api_name, record_id, absolute_file_path):
        """
        This method is used to upload attachments and print the response
        :param module_api_name: The API Name of the record's module
        :param record_id: The ID of the record to upload attachment
        :param absolute_file_path: The absolute file path of the file to be attached
        """

        """
        example
        module_api_name= "Leads"
        record_id = 34096432267003
        absolute_file_path = "/Users/user-name/Documents/image.jpg";
        """

        # Get instance of AttachmentsOperations Class that takes module_api_name and record_id as parameters
        attachments_operations = AttachmentsOperations(
            module_api_name, record_id)

        # Get instance of FileBodyWrapper class that will contain the request file
        file_body_wrapper = FileBodyWrapper()

        """
        StreamWrapper can be initialized in any of the following ways
        
        * param 1 -> fileName 
        * param 2 -> Read Stream.
        """
        # stream_wrapper = StreamWrapper(stream=open(absolute_file_path, 'rb'))

        """
        * param 1 -> fileName
        * param 2 -> Read Stream
        * param 3 -> Absolute File Path of the file to be attached
        """

        stream_wrapper = StreamWrapper(file_path=absolute_file_path)

        # Set file to the FileBodyWrapper instance
        file_body_wrapper.set_file(stream_wrapper)

        # Call upload_attachment method that takes FileBodyWrapper instance as parameter
        response = attachments_operations.upload_attachment(file_body_wrapper)

        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())
 
Delete Attachments
              
              
import os
from zcrmsdk.src.com.zoho.crm.api import ParameterMap
from zcrmsdk.src.com.zoho.crm.api.attachments import *
from zcrmsdk.src.com.zoho.crm.api.util import StreamWrapper
class Attachment(object):
    @staticmethod
    def delete_attachments(module_api_name, record_id, attachment_ids):
        """
        This method is used to Delete attachments of a single record with ID and print the response.
        :param module_api_name: The API Name of the record's module
        :param record_id: The ID of the record to delete attachments
        :param attachment_ids: The list of attachment IDs to be deleted
        """

        """
        example
        module_api_name= "Leads"
        record_id = 34096432267003
        attachment_ids = [34096432267012, 34096432267018, 34096432267010]
        """

        # Get instance of AttachmentsOperations Class that takes module_api_name and record_id as parameter
        attachments_operations = AttachmentsOperations(
            module_api_name, record_id)

        # Get instance of ParameterMap Class
        param_instance = ParameterMap()

        # Add the ids to parameter map instance
        for attachment_id in attachment_ids:
            param_instance.add(DeleteAttachmentsParam.ids, attachment_id)

        # Call delete_attachments method that takes paramInstance as parameter
        response = attachments_operations.delete_attachments(param_instance)

        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())
 
Download an Attachment
              
              
import os

from zcrmsdk.src.com.zoho.crm.api import ParameterMap
from zcrmsdk.src.com.zoho.crm.api.attachments import *
from zcrmsdk.src.com.zoho.crm.api.util import StreamWrapper
class Attachment(object):
    @staticmethod
    def download_attachment(module_api_name, record_id, attachment_id, destination_folder):
        """
        This method is used to download an attachment of a single record of a module with ID and attachment ID and write the file in the specified destination.
        :param module_api_name: The API Name of the record's module
        :param record_id: The ID of the record to download attachment
        :param attachment_id: The ID of the attachment to be downloaded
        :param destination_folder: The absolute path of the destination folder to store the attachment
        """

        """
        example
        module_api_name = "Leads";
        record_id = 34096432267003
        attachment_id = 34096432267024
        destination_folder = "/Users/user-name/Desktop";
        """

        # Get instance of AttachmentsOperations Class that takes module_api_name and record_id as parameter
        attachments_operations = AttachmentsOperations(
            module_api_name, record_id)

        # Call download_attachment method that takes attachment_id as parameters
        response = attachments_operations.download_attachment(attachment_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())
 
Delete an Attachment
              
              
import os

from zcrmsdk.src.com.zoho.crm.api import ParameterMap
from zcrmsdk.src.com.zoho.crm.api.attachments import *
from zcrmsdk.src.com.zoho.crm.api.util import StreamWrapper
class Attachment(object):
    @staticmethod
    def delete_attachment(module_api_name, record_id, attachment_id):
        """
        This method is used to delete an attachment of a single record with ID and attachment ID and print the response
        :param module_api_name: The API Name of the record's module
        :param record_id: The ID of the record to delete attachment
        :param attachment_id: The ID of the attachment to be deleted
        """

        """
        example
        module_api_name = "Leads";
        record_id = 34096432267003
        attachment_id = 34096432267024
        """

        # Get instance of AttachmentsOperations Class that takes module_api_name and record_id as parameters
        attachments_operations = AttachmentsOperations(
            module_api_name, record_id)

        # Call delete_attachment method that takes attachment_id as parameter
        response = attachments_operations.delete_attachment(attachment_id)

        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())
 
              
              
import os

from zcrmsdk.src.com.zoho.crm.api import ParameterMap
from zcrmsdk.src.com.zoho.crm.api.attachments import *
from zcrmsdk.src.com.zoho.crm.api.util import StreamWrapper
class Attachment(object):
    @staticmethod
    def upload_link_attachment(module_api_name, record_id, attachment_url):
        """
        This method is used to upload link attachment and print the response.
        :param module_api_name: The API Name of the record's module
        :param record_id: The ID of the record to upload Link attachment
        :param attachment_url: The attachment url of the doc or image link to be attached
        """

        """
        example
        module_api_name = "Leads";
        record_id = 34096432267003
        attachment_url = "https://5.imimg.com/data5/KJ/UP/MY-8655440/zoho-crm-500x500.png";
        """

        # Get instance of AttachmentsOperations Class that takes module_api_name and record_id as parameter
        attachments_operations = AttachmentsOperations(
            module_api_name, record_id)

        # Get instance of ParameterMap Class
        param_instance = ParameterMap()

        # Add the attachment_url to parameter Instance
        param_instance.add(
            UploadLinkAttachmentParam.attachmenturl, attachment_url)

        # Call upload_link_attachment method that takes ParameterMap instance as parameter
        response = attachments_operations.upload_link_attachment(
            param_instance)

        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())