Skip to content

Instantly share code, notes, and snippets.

@jmahmood
Created May 12, 2015 01:28
Show Gist options
  • Save jmahmood/c3e82d07e7190c31c10d to your computer and use it in GitHub Desktop.
Save jmahmood/c3e82d07e7190c31c10d to your computer and use it in GitHub Desktop.
How to upload object attachments from SFDC to an external source.
/*
TODO:
- Handle 404s / other errors
- Handle invalid Attachment ids
-
*/
class testUploadAttachment {
@future(callout=true)
public static HttpResponse remoteAttachmentUpload(Id attachment_id, String remote_url) {
Http conn = new Http();
Attachment attachment_obj = [Select Id, Name, ParentId, Body from Attachment where Id =: attachment_id];
HttpRequest req = new HttpRequest();
req.setEndpoint(remote_url);
req.setMethod('POST');
req.setHeader('Content-Type','application/x-www-form-urlencoded');
req.setBody('attachmentId=' + attachment_obj.Id +
'&Title=' + attachment_obj.Name +
'&parentId=' + attachment_obj.ParentId +
'&Body=' + EncodingUtil.base64Encode(attachment_obj.Body));
HttpResponse res = conn.send(req);
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment