In one of the apex call in bound call I am receiving an xml with content in string format as part of the response. Can I store the content in the reqas a txt/xml file in the attachment of a custom object?

I found that if the response I receive have payload as blob then I can store the blob as an attachment. But is it possible to store the content sent in response in as an attachment?


Attachment.Body is of type base64 so, as long as the source string is valid UTF-8, you can set this field using:

myAttach.Body = Blob.valueOf(theXmlString);
myAttach.Name = 'myfilename.xml';
  • Can I store it as .txt file or html file too? – SfdcBat Dec 15 '16 at 16:44
  • 1
    You convert the text to a BLOB; use a file name ending in some text extension (.txt, .html) and when SFDC renders the attachment to the browser, the browser will render as expected (and download will download as expected). The actual data in SFDC has to be stored as base64 in this SObject – cropredy Dec 15 '16 at 16:48
  • If the string is not valid UTF-8 format then can't we use it / is there any limitations? – SfdcBat Dec 15 '16 at 17:00
  • 1
    You can upload from the browser or any API a base64 value that isn't UTF-8 compliant; but if APEX, the string has to be UTF-8 before conversion to base64. Inbound email services , if converting to a string, the resulting string has to be UTF-8 (like an incoming CSV) – cropredy Dec 15 '16 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.