Skip to content

Instantly share code, notes, and snippets.

@lfreeland
Created June 18, 2016 02:34
Show Gist options
  • Select an option

  • Save lfreeland/460aedd113b7e1cd6b7e7d430fde0cfc to your computer and use it in GitHub Desktop.

Select an option

Save lfreeland/460aedd113b7e1cd6b7e7d430fde0cfc to your computer and use it in GitHub Desktop.
/// <summary>
/// Tests attaching a file to the specified "AttachmentParentId" record.
/// </summary>
[TestMethod]
public void AttachFileTest()
{
String parentId = ConfigurationManager.AppSettings["AttachmentParentId"];
Assert.IsTrue(String.IsNullOrWhiteSpace(parentId) == false, "The AttachmentParentId app setting is blank. Please specify the id of a salesforce record to attach an attachment file.");
// Create Attachment Job
CreateJobRequest attachmentJobRequest = buildDefaultAttachmentJobRequest();
Job attachmentJob = _apiClient.CreateJob(attachmentJobRequest);
// Create file to attach to record
String filename = "BulkAPIClientAttachment.txt";
File.WriteAllText(filename, "Hello From Attach File Test");
// Create attachment batch request
CreateAttachmentBatchRequest attachmentBatchRequest = new CreateAttachmentBatchRequest();
attachmentBatchRequest.FilePath = filename;
attachmentBatchRequest.JobId = attachmentJob.Id;
attachmentBatchRequest.ParentId = parentId;
// Create attachment batch
Batch attachmentBatch = _apiClient.CreateAttachmentBatch(attachmentBatchRequest);
// Close job so no more batches are added.
_apiClient.CloseJob(attachmentJob.Id);
attachmentJob = _apiClient.GetCompletedJob(attachmentJob.Id);
Assert.AreEqual(1, attachmentJob.NumberRecordsProcessed, "The file was not attached to the specified record.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment