Created
May 12, 2015 01:28
-
-
Save jmahmood/c3e82d07e7190c31c10d to your computer and use it in GitHub Desktop.
How to upload object attachments from SFDC to an external source.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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