Ruby SDK Samples - Attachment Operations
require 'ZOHOCRMSDK2_0'
class Attachment
def self.upload_attachment(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 record_id and module_api_name as parameters
ao = ZOHOCRMSDK::Attachments::AttachmentsOperations.new(module_api_name,record_id)
# Get instance of FileBodyWrapper class that will contain the request file
fbw = ZOHOCRMSDK::Attachments::FileBodyWrapper.new
# """
# StreamWrapper can be initialized in any of the following ways
# * param 1 -> fileName
# * param 2 -> Read Stream.
# * param 3 -> Absolute File Path of the file to be attached
# """
# stream_wrapper = StreamWrapper.new(name,stream,absolutefilepath)
sw = ZOHOCRMSDK::Util::StreamWrapper.new(nil, nil, absolute_file_path)
# Set file to the FileBodyWrapper instance
fbw.file = sw
# Call upload_attachment method that takes FileBodyWrapper instance as parameter
response = ao.upload_attachment(fbw)
unless response.nil?
status_code = response.status_code
# Get the status code from response
print "\n Status Code :" + status_code.to_s
# Check if expected instance is received.
if response.is_expected
# Get object from response
action_handler = response.data_object
# Check if expected ActionWrapper instance is received.
if action_handler.is_a? ZOHOCRMSDK::Attachments::ActionWrapper
action_wrapper = action_handler
action_responses = action_wrapper.data
action_responses.each do |action_response|
# Check if expected SuccessResponse instance is received.
if action_response.is_a? ZOHOCRMSDK::Attachments::SuccessResponse
success_response = action_response
# Get the Code
print 'code:'
print success_response.code.value
# Get the Status
print "\n status:"
print success_response.status.value
# Get the Message
print "\n message:"
print success_response.message.value
success_response.details.each do |k, v|
print "\n"
print k
print v
print "\n"
end
# Check if the request returned an exception
elsif action_response.is_a? ZOHOCRMSDK::Attachments::APIException
api_exception = action_response
# Get the Code
print 'code:'
print api_exception.code.value
# Get the Status
print "\n status:"
print api_exception.status.value
# Get the Message
print "\n message:"
print api_exception.message.value
# Get the details map
api_exception.details.each do |k, v|
print "\n"
print k
print v
print "\n"
end
print "\n"
end
end
# Check if the request returned an exception
elsif action_handler.is_a? ZOHOCRMSDK::Attachments::APIException
exception = action_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
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
require 'ZOHOCRMSDK2_0'
class Attachment
def self.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 ParameterMap Class
pm = ZOHOCRMSDK::ParameterMap.new
# Add the ids to parameter map instance
attachment_ids.each do |attachment_id|
pm.add(ZOHOCRMSDK::Attachments::AttachmentsOperations::DeleteAttachmentsParam.ids, attachment_id)
end
# Get instance of AttachmentsOperations Class that takes recordId and moduleAPIName as parameter
ao = ZOHOCRMSDK::Attachments::AttachmentsOperations.new(module_api_name,record_id)
# Call delete_attachments method that takes pm as parameter
response = ao.delete_attachments(pm)
unless response.nil?
status_code = response.status_code
# Get the status code from response
print "\n Status Code :" + status_code.to_s
# Check if expected instance is received.
if response.is_expected
# Get object from response
action_handler = response.data_object
# Check if expected ActionWrapper instance is received.
if action_handler.is_a? ZOHOCRMSDK::Attachments::ActionWrapper
action_wrapper = action_handler
action_responses = action_wrapper.data
action_responses.each do |action_response|
# Check if expected SuccessResponse instance is received.
if action_response.is_a? ZOHOCRMSDK::Attachments::SuccessResponse
success_response = action_response
# Get the Code
print 'code:'
print success_response.code.value
# Get the Status
print "\n status:"
print success_response.status.value
# Get the Message
print "\n message:"
print success_response.message.value
# Get the details map
success_response.details.each do |k, v|
print "\n"
print k
print v
print "\n"
end
# Check if the request returned an exception
elsif action_response.is_a? ZOHOCRMSDK::Attachments::APIException
api_exception = action_response
# Get the Code
print 'code:'
print api_exception.code.value
# Get the Status
print "\n status:"
print api_exception.status.value
# Get the Message
print "\n message:"
print api_exception.message.value
# Get the details map
api_exception.details.each do |k, v|
print "\n"
print k
print v
print "\n"
end
print "\n"
end
end
# Check if the request returned an exception
elsif action_handler.is_a? ZOHOCRMSDK::Attachments::APIException
exception = action_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
require 'ZOHOCRMSDK2_0'
class Attachment
def self.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";
# """
ao = ZOHOCRMSDK::Attachments::AttachmentsOperations.new(module_api_name,record_id)
response = ao.download_attachment(attachment_id)
unless response.nil?
status_code = response.status_code
# Get the status code from response
print "\n Status Code :" + status_code.to_s
if status_code == 204
print('No Content')
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 receive
if response_handler.is_a? ZOHOCRMSDK::Attachments::FileBodyWrapper
file_body_wrapper = response_handler
stream_wrapper = file_body_wrapper.file
File.open(destination_folder + '/' + stream_wrapper.name, 'w') do |f|
f.write(stream_wrapper.stream)
end
# Check if expected APIException instance is received.
elsif response_handler.is_a? ZOHOCRMSDK::Attachments::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
require 'ZOHOCRMSDK2_0'
class Attachment
def self.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 recordId and moduleAPIName as parameter
ao = ZOHOCRMSDK::Attachments::AttachmentsOperations.new(module_api_name,record_id)
# Call delete_attachment method that takes attachment_id as parameter
response = ao.delete_attachment(attachment_id)
unless response.nil?
status_code = response.status_code
# Get the status code from response
print "\n Status Code :" + status_code.to_s
# Check if expected instance is received.
if response.is_expected
# Get object from response
action_handler = response.data_object
# Check if expected ActionWrapper instance is received.
if action_handler.is_a? ZOHOCRMSDK::Attachments::ActionWrapper
action_wrapper = action_handler
action_responses = action_wrapper.data
action_responses.each do |action_response|
# Check if expected SuccessResponse instance is received.
if action_response.is_a? ZOHOCRMSDK::Attachments::SuccessResponse
success_response = action_response
# Get the Code
print 'code:'
print success_response.code.value
# Get the Status
print "\n status:"
print success_response.status.value
# Get the Message
print "\n message:"
print success_response.message.value
success_response.details.each do |k, v|
print "\n"
print k
print v
print "\n"
end
# Check if the request returned an exception
elsif action_response.is_a? ZOHOCRMSDK::Attachments::APIException
api_exception = action_response
# Get the Code
print 'code:'
print api_exception.code.value
# Get the Status
print "\n status:"
print api_exception.status.value
# Get the Message
print "\n message:"
print api_exception.message.value
# Get the details map
api_exception.details.each do |k, v|
print "\n"
print k
print v
print "\n"
end
print "\n"
end
end
# Check if the request returned an exception
elsif action_handler.is_a? ZOHOCRMSDK::Attachments::APIException
exception = action_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
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
require 'ZOHOCRMSDK2_0'
class Attachment
def self.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 ParameterMap Class
pm = ZOHOCRMSDK::ParameterMap.new
# Add the attachment_url to parameter Instance
pm.add(ZOHOCRMSDK::Attachments::AttachmentsOperations::UploadLinkAttachmentParam.attachmentUrl, attachment_url)
# Get instance of AttachmentsOperations Class that takes recordId and moduleAPIName as parameter
ao = ZOHOCRMSDK::Attachments::AttachmentsOperations.new(module_api_name,record_id)
# Call upload_link_attachment method that takes ParameterMap instance as parameter
response = ao.upload_link_attachment(pm)
unless response.nil?
status_code = response.status_code
# Get the status code from response
print "\n Status Code :" + status_code.to_s
# Check if expected instance is received.
if response.is_expected
# Get object from response
action_handler = response.data_object
# Check if expected ActionWrapper instance is received.
if action_handler.is_a? ZOHOCRMSDK::Attachments::ActionWrapper
action_wrapper = action_handler
action_responses = action_wrapper.data
action_responses.each do |action_response|
# Check if expected SuccessResponse instance is received.
if action_response.is_a? ZOHOCRMSDK::Attachments::SuccessResponse
success_response = action_response
# Get the Code
print 'code:'
print success_response.code.value
# Get the Status
print "\n status:"
print success_response.status.value
# Get the Message
print "\n message:"
print success_response.message.value
success_response.details.each do |k, v|
print "\n"
print k
print v
print "\n"
end
# Check if the request returned an exception
elsif action_response.is_a? ZOHOCRMSDK::Attachments::APIException
api_exception = action_response
# Get the Code
print 'code:'
print api_exception.code.value
# Get the Status
print "\n status:"
print api_exception.status.value
# Get the Message
print "\n message:"
print api_exception.message.value
# Get the details map
api_exception.details.each do |k, v|
print "\n"
print k
print v
print "\n"
end
print "\n"
end
end
# Check if the request returned an exception
elsif action_handler.is_a? ZOHOCRMSDK::Attachments::APIException
exception = action_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