Retrieving CRM Attachments

Jul 7, 2022 (4y ago)

This post contains Python code to retrieve and download attachments from Zoho CRM Deals. It uses the Zoho CRM API to fetch attachment information and download the files. It cab be applied for other modules of Zoho as well

Setup

First, let's import the necessary libraries:

import requests
import json
import pandas as pd
import os

Reading CRM Deals Data

We start by reading a CSV file containing CRM Deals information:

csv = pd.read_csv('CRMDeals.csv')
print(csv)

This will display the contents of the CSV file, which includes deal IDs and account names.

Retrieving Attachments

The following code block retrieves attachments for each deal and attempts to download them:

token = '#Token#'

for x in csv['id']:
    r1 = requests.get(
        url=f'https://www.zohoapis.com/crm/v2/Deals/{x}/Attachments',
        headers={
            "orgId": "#OrgID#",
            "Authorization": f"Zoho-oauthtoken {token}"
        }
    )
    print(r1.json())
    try:
        j1 = r1.json()
        df1 = pd.DataFrame.from_dict(j1["data"])
        for y in df1['id']:
            r2 = requests.get(
                url=f'https://www.zohoapis.com/crm/v2/Deals/{x}/Attachments/{y}',
                headers={
                    "orgId": "657802527",
                    "Authorization": f"Zoho-oauthtoken {token}"
                }
            )
            open(f"{y}_{df1['account_name'][y]}_Contract.pdf", 'wb').write(r2.content)
    except Exception as e:
        print(type(e))

This code does the following:

  1. Iterates through each deal ID in the CSV file.
  2. Retrieves the list of attachments for each deal.
  3. For each attachment, it downloads the file and saves it with a name that includes the attachment ID and account name.

Notes

Remember to handle your API tokens securely and not expose them in public repositories or shared notebooks.