Earlier today a colleague asked me if I had an example of using Python to create an attachment in Salesforce. I didn’t, but that didn’t stop me from creating a couple. Sadly, our documentation here isn’t the greatest. You can look at the Insert or Update Blob Data for the REST API or Attachment for our SOAP API. If you’re like me, neither document is good enough; I had to continue to dig around to figure out exactly what I needed.

Nevertheless, I got it working. I’d like to share with you two examples: uploading attachments with the REST API and uploading attachments with the SOAP API. For both examples I will use Python.

Why Python?

Well, not only is it fun, but I think Python is also remarkably easy to read. It shouldn’t be too much work for you to adjust to your language of choice.

A few things worth nothing before we go to far:

  1. You will need to base64 encode your file.
  2. An attachment attaches to an object in Salesforce (i.e. Account, Contact, etc.). You’ll need to know the Id (called the ParentId) to which you want to attach.
  3. Python has some great libraries that I’m going to use. This has the added benefit of keeping the code really simple and focused on the task of uploading the attachment.

Without further ado …

Python and Attachments with the REST API

To run this script you’ll need to do the following:

  • Create a text.txt file in the same directory as the Python script.
  • Add your user name, password, and security token.
  • Install simple_salesforce with pip install simple_salesforce. I recommend you first use virtualenv to manage your environment.
  • Update the instance value (or extract it from the login info).

The code is pretty straightforward. simple_salesforce is great when you want to quickly work with the REST API.

Python and Attachments with the SOAP API

To run this script you’ll need to do the following:

  • Create a text.txt file in the same directory as the Python script.
  • Add your user name, password, and security token.
  • Install beatbox with pip install beatbox. I recommend you first use virtualenv to manage your environment.

Again, this code is pretty straightforward. beatbox is a fantastic way to interact with the SOAP API.

It’s likely over time I’ll learn more that I should add to this post. For now, I’ll leave it as it is; hopefully someone finds it useful.

Enjoy!