Importing Attachments with SQLForce

This article explains how to import Attachments to Salesforce using SQLForce.

To import an Attachment, three things are needed:

  • The unique Salesforce Id of the parent object that will own the Attachment (e.g. an Account Id, Opportunity Id, etc…).
  • The name of the file to attach. This is generally a real file, but not necessarily.
  • Optionally, the content of the file.

Attach an Existing File

The following example will attach an existing file to a record in Salesforce:

from SQLForce import AttachmentWriter

attachmentId = AttachmentWriter.attachFile( session, accountId, “filename.txt”)

The AttachmentWriter will load the contents of “filename.txt” into memory and attach it to the specified account.

The value returned, attachmentId, is the unique Id assigned by Salesforce for the Attachment.

Attach Raw Data

The following example will create a new Attachment in Salesforce using file contents generated in code:

from SQLForce import AttachmentWriter

myData = “Here’s what I want for the attachment content”

attachmentId = AttachmentWriter.attachFile( session, accountId, “filename.txt”, content=myData)

The AttachmentWriter will store the value of “myData” as an Attachment named “filename.txt” on the specified Account.

Optional Parameters

The AttachmentWriter supports a number of optional non-positional parameters:

Parameter Description
contentType Salesforce requires a ContentType (a MIME type) for an Attachment. SQLForce will determine the correct value for nearly any file type using the Python mimetype package, but this parameter can be set directly to override SQLForce.
description The description parameter for the Attachment.