Export Attachments with SQLForce

The SQLForce.Attachment reader module exports attachments from Salesforce in just a couple lines of code. Attachments can be read into memory or exported directly to disk.

Read Attachments Into Memory

If an Attachment’s unique Salesforce Id is known, the following will read the Attachment into memory:

from SQLForce import AttachmentReader

attachment = AttachmentReader.readAttachment( session, attachmentId )

print(“I read attachment: ” + attachment.name )

The “attachment” class returned by readAttachment() has the following fields:

Field Description
id The unique Id of the Attachment.
name The Attachment filename.
parentId The unique Id of the record that owns the Attachment.
contentType The Attachment’s MIME type.
content The content of the Attachment, decoded from base64. Salesforce stores and returns attachments in base64.

Another way to read Attachments into memory is to specify a WHERE clause for the Salesforce Attachment object. The following example will read all Attachments for a specific Account into memory:

from SQLForce import AttachmentReader

accountId = ‘001000000000000’

attachments = AttachmentReader.readAttachmentsWHERE( session, “accountId='” + accountId + “‘”)

for rec in attachments:

print(“I read attachment: ” + rec.name

Export Attachments to Disk

If the Attachment being read is to large to fit into memory, or if it should be saved to disk regardless, Attachments can be exported directly to disk. This is accomplished using the AtachmentReader.exportBy methods.

“exportBy” methods require that a target directory be specified. Exported attachments will be stored in this directory using the following rules:

  • Attachments are exported in their original format using their original filename.
    • e.g. Excel is exported as Excel, not a base64 file as it is stored in Salesforce.
  • Attachments are stored in a subdirectory based on the unique Id of their parent.

There are three exportBy methods:

  • exportByAttachmentIds()
    • Takes a list of unique Attachment Ids.
  • exportByParentIds()
    • Takes a list of Attachment parent Ids (i.e. the record that owns the attachment).
  • exportByAttachmentWHERE()
    • Takes a SOQL WHERE clause on the Salesforce Attachment object.

Example: Populate the directory /tmp/BigCoAttachments with all Attachments from Accounts whose names start with “TheBigCo”.

import SQLForce
from SQLForce import AttachmentReader

session = SQLForce.Session(“MyProfile”)

accountIds = []

for rec in session.selectRecords(“SELECT id FROM Account WHERE name LIKE ‘TheBigCo%'”):

accountIds.append(rec.id)

AttachmentReader.exportByParentIds(session, accountIds, ‘/tmp/BigCoAttachments’ )

Example: Export Attachments given their unique Ids but do not create sub-directories based on the Attachment parent Id.

import SQLForce
from SQLForce import AttachmentReader

session = SQLForce.Session(“MyProfile”)

attachmentIds = [‘Find’, ‘These’, ‘Using’, ‘Your’, ‘Method’, ‘of’, ‘choice’]

AttachmentReader.exportByAttachmentIds(session, attachmentIds, ‘/tmp/MyAttachments’, createSubDirs=False)

Example: Export Attachments owned by a particular parent using a WHERE clause.

import SQLForce
from SQLForce import AttachmentReader

session = SQLForce.Session(“MyProfile”)

AttachmentReader.exportByAttachmentWHERE(session, “parentId=’0334aaasfc'”, ‘/tmp/MyAttachments’)