Kaizen #45 - Handling Attachments API in Java and Java SDK (Part 2/2)

Kaizen #45 - Handling Attachments API in Java and Java SDK (Part 2/2)

Hello everyone!

Welcome back to yet another post in the Kaizen series. In our last post, we started our discussion about handling the attachments in Java and Java SDK where we discussed listing the attachments and uploading the attachments to a record in Zoho CRM.

This week, we will discuss the other two operations in attachments API.
1. Download an attachment 
2. Delete an attachment.


1. Download an Attachment
To download a file attached to a record.

Request method: GET
Request URL:  {{api-domain}}/crm/v2/{module_api_name}/{record_id}/Attachments/{attachment_id}

Sample URL: {{api-domain}}/crm/v2/Leads/3719520000000659047/attachments/3719520000000992004

Java SDK Code
The Java SDK employs downloadAttachment() method for the operation which primarily requires the record_id(3719520000000659047) and the attachment_id(3719520000000992004) for its successful execution. The attachment_id can be fetched from List Attachments API

public void downloadAttachment() throws Exception{
  ZCRMRecord record=ZCRMRecord.getInstance("Leads",3719520000000659047L);
  FileAPIResponse responseIn=record.downloadAttachment(3719520000000992004L);
       String filepath="/Users/srivathsan-8829/Documents";
        File file=new File(filepath+responseIn.getFileName()); 
        System.out.println( "HTTP Status Code:"+responseIn.getStatusCode());
        System.out.println( "File Name:"+responseIn.getFileName());
        InputStream fin = responseIn.getFileAsStream();
        int i=0;    
        while((i=fin.read())!=-1){    
         System.out.print((char)i);    
        }    
    }

Java Code
public void downloadAttachments() throws Exception
 {
  CloseableHttpClient httpclient = HttpClients.createDefault();
     HttpGet httpget = new HttpGet("{{api-domain}}/crm/v2/Leads/3719520000000659047/attachments/3719520000000991001");
     httpget.addHeader("Authorization", "Zoho-oauthtoken 1000.9XXXX.6XXXX");
     System.out.println("Request Type: "+httpget.getMethod());
     HttpResponse httpresponse = httpclient.execute(httpget);
     Scanner sc = new Scanner(httpresponse.getEntity().getContent());
     System.out.println(httpresponse.getStatusLine());
     while(sc.hasNext()) {
          System.out.println(sc.nextLine());
     }
}

Sample Response:


2. Delete an Attachment
To delete a file attached to a record.

Request method: DELETE
Request URL:  {{api-domain}}/crm/v2/{module_api_name}/{record_id}/Attachments/{attachment_id}

Sample URL:  {{api-domain}}/crm/v2/Leads/3719520000000659047/attachments/3719520000000981001

Java SDK Code
Similar to download attachment operation, delete attachment requires the record_id and the attachment_id as the pre-requisites. The operation is performed with the assistance from deleteAttachment() method. The attachment_id can be fetched from List Attachments API

public void deleteAttachment() throws Exception{
  ZCRMRecord record=ZCRMRecord.getInstance("Leads",3719520000000659047L);
  APIResponse responseIn=record.deleteAttachment(3719520000000981001L);
  System.out.println( "Status:"+ responseIn.getMessage());
        System.out.println( "Message:"+ responseIn.getStatus());
        System.out.println("http code"+ responseIn.getStatusCode());
        System.out.println( " Code:"+responseIn.getResponseJSON().toString());
 }

Java Code
public void deleteAttachments() throws Exception
 {
  CloseableHttpClient httpclient = HttpClients.createDefault();
     HttpDelete httpdelete = new HttpDelete("{{api-domain}}/crm/v2/Leads/3719520000000659047/attachments/3719520000000981001");
     httpdelete.addHeader("Authorization", "Zoho-oauthtoken 1000.9XXXX.6XXXX");
     System.out.println("Request Type: "+httpdelete.getMethod());
     HttpResponse httpresponse = httpclient.execute(httpdelete);
     Scanner sc = new Scanner(httpresponse.getEntity().getContent());
     System.out.println(httpresponse.getStatusLine());
     while(sc.hasNext()) {
          System.out.println(sc.nextLine());
     }
 }

Sample Response
{
    "data": [
        {
            "code": "SUCCESS",
            "details": {
                "id": "3719520000000981001"
            },
            "message": "record deleted",
            "status": "success"
        }
    ]
}

With this post, we conclude our discussion about handling the attachments API with Java SDK and Java. We hope you found this post useful. Let us know your thoughts in the comment section or reach us out at support@zohocrm.com.

Cheers!









    Access your files securely from anywhere

        Zoho Developer Community




                                  Zoho Desk Resources

                                  • Desk Community Learning Series


                                  • Digest


                                  • Functions


                                  • Meetups


                                  • Kbase


                                  • Resources


                                  • Glossary


                                  • Desk Marketplace


                                  • MVP Corner


                                  • Word of the Day



                                      Zoho Marketing Automation


                                              Manage your brands on social media



                                                    Zoho TeamInbox Resources

                                                      Zoho DataPrep Resources



                                                        Zoho CRM Plus Resources

                                                          Zoho Books Resources


                                                            Zoho Subscriptions Resources

                                                              Zoho Projects Resources


                                                                Zoho Sprints Resources


                                                                  Qntrl Resources


                                                                    Zoho Creator Resources



                                                                        Zoho Campaigns Resources


                                                                          Zoho CRM Resources

                                                                          • CRM Community Learning Series

                                                                            CRM Community Learning Series


                                                                          • Kaizen

                                                                            Kaizen

                                                                          • Functions

                                                                            Functions

                                                                          • Meetups

                                                                            Meetups

                                                                          • Kbase

                                                                            Kbase

                                                                          • Resources

                                                                            Resources

                                                                          • Digest

                                                                            Digest

                                                                          • CRM Marketplace

                                                                            CRM Marketplace

                                                                          • MVP Corner

                                                                            MVP Corner





                                                                              Design. Discuss. Deliver.

                                                                              Create visually engaging stories with Zoho Show.

                                                                              Get Started Now