Tag Archives: Attachment

SFBulkAPIStarter: Attachment Support Added

SFBulkAPIStarter Overview

A couple years ago I was enhancing an ETL tool at work to add Salesforce Bulk API support so our data migration projects would execute faster. To help others understand how to use the Salesforce Bulk API, the SFBulkAPIStarter open source project was started.

It’s a C# library that has starter code demonstrating how to use the Salesforce Bulk API. The test project has test classes demonstrating how to do various Bulk API  tasks such as inserting, upserting, querying, and deleting records.

To take it for a spin, download it from Nuget.

Attachment Support Added

It’s been a while since I had contributed to the SFBulkAPIStarter project so I added support for adding an attachment to a specified record.

Attachment High-Level Steps

The steps to add an attachment to a Salesforce record were easier than expected:

  1. Create an Insert Attachment Bulk Job.
  2. Create a request.txt file containing one or more records with the following fields
    • Name – Name of the attachment to show in Salesforce.
    • Body – The “#” prefixed path of the file in the zip file.
    • ParentId – The Id of the Salesforce record to attach the attachment to.
  3. Create a zip file with the request.txt file and the file(s) to attach.
  4. Create a batch in the job whose request file is the zip file.

Attachment Code

Here’s the attachment method in the BulkAPIClient class that handles the attachment logic. Currently, this method handles attaching one attachment and not multiple.

Test Code

Here’s the test from the TestBulkApiClient class that attaches a file to a specified Salesforce record.

How often do you use the Bulk API? How about attaching an attachment to a record?

Happy Coding,

Luke