Skip to content

Instantly share code, notes, and snippets.

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

  • Save lfreeland/11d931860e3f25c05bc63dda995c438a to your computer and use it in GitHub Desktop.

Select an option

Save lfreeland/11d931860e3f25c05bc63dda995c438a to your computer and use it in GitHub Desktop.
public Batch CreateAttachmentBatch(CreateAttachmentBatchRequest request)
{
String requestTxtFileCSVContents = "Name,ParentId,Body" + Environment.NewLine;
requestTxtFileCSVContents += request.FilePath + "," + request.ParentId + ",#" + request.FilePath;
using (MemoryStream memoryStream = new MemoryStream())
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
byte[] requestTxtFileCSVContentsBytes = UTF8Encoding.UTF8.GetBytes(requestTxtFileCSVContents);
var requestTxtFileInArchive = archive.CreateEntry("request.txt");
using (var entryStream = requestTxtFileInArchive.Open())
using (var fileToCompressStream = new MemoryStream(requestTxtFileCSVContentsBytes))
{
fileToCompressStream.CopyTo(entryStream);
}
byte[] attachmentFileContentsBytes = File.ReadAllBytes(request.FilePath);
var attachmentFileInArchive = archive.CreateEntry(request.FilePath);
using (var attachmentEntryStream = attachmentFileInArchive.Open())
using (var attachmentFileToCompressStream = new MemoryStream(attachmentFileContentsBytes))
{
attachmentFileToCompressStream.CopyTo(attachmentEntryStream);
}
byte[] zipFileBytes = memoryStream.ToArray();
String requestUrl = "https://" + _sfService.Pod + ".salesforce.com/services/async/31.0/job/" + request.JobId + "/batch";
byte[] responseBytes = invokeRestAPI(requestUrl, zipFileBytes, "POST", "zip/csv");
String resultXML = UTF8Encoding.UTF8.GetString(responseBytes);
return Batch.CreateBatch(resultXML);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment