function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rakesh ERakesh E 

How to Store Binary data into salesforce attachment

Hi,

 

i wrote a Custom Web Service in Salesforce to acceprt Base64 encoded files (PDF/Text) and store them as Attachments.

it worked fine. But the other integration partner has changed the format to "Binary" and my code is not working now.

 

so is there any way to store the files which are received in binary format in an attachment.

i heard that we can store binary format also, but unable to find out the exact process.

please let me know if anyone has idea about this.

 

thanks in advance

 

Regards,

Rakesh

 

Ashish_SFDCAshish_SFDC
Hi Dev, 

To store the image, use an <apex:inputFile> element; the uploaded file will be assigned to a value in your controller as a BLOB (Binary Large OBject). From there, you can store it as a Document, Attachment, or Content.

To view the image on the screen, just provide a link to the content (e.g. "/servlet/servlet.FileDownload?file={!file.id}").

https://developer.salesforce.com/forums/ForumsMain?id=906F0000000915KIAQ

Also, see the link below,

http://blogs.developerforce.com/developer-relations/2011/09/using-binary-data-with-rest.html

Regards,
Ashish

Nirmal ChristopherNirmal Christopher
If you are receiving the attachment file as a binary data it is stored as a BLOB data type in Salesforce. In your code use the attachment file somehing like this
 Blob b ='Your binary value result';
attachments.body =b;
insert attachments;

 try  this if it works.
Ajay_SFDCAjay_SFDC
Hi Dev ,

Try the following ways :

1 .HttpResponse res = new HttpResponse();         
    Blob beforeBlob = res.getBodyAsBlob();
    Attachment objAttachmnt = new Attachment(parentId ='URParentID', name= 'Shipment.pdf', body = beforeBlob);
    insert objAttachmnt;

2. Attachment objAttachmnt = new Attachment(parentId = sObjectToBind.Id, name= 'Contract.pdf', body =                  EncodingUtil.base64Decode(YOURBODY));
             insert objAttachmnt;

Thanks ,
   Ajay 
pragaddheeswaran Kpragaddheeswaran K
Thanks, Ajay. your answer helped me a lot...