+ Start a Discussion
PamSalesforcePamSalesforce 

Generate Text file using apex code

Hi,

 

I want to generate a text file using Apex code based on certain criteria.If anyone has implemented it or know how to do so .Please help me or post the code snippet.

 

Thanks.

XactiumBenXactiumBen

You'll have to create a document record or an attachment record that will store your text data.  Something like:

 

Document d = new Document(); d.Name = 'my text file'; String myContent = 'aabbcc'; d.Body = Blob.valueOf(myContent); d.ContentType = 'text/plain';

d.Type = 'txt';

insert d;

 

This will create a plain text file.

PamSalesforcePamSalesforce

Hi,

Thanks for the reply. It worked.

YikaYika

I am new programming in Salesforce, so I do not know where to put this code, can you help me?

What I need is the following: when I click on a button I need to create a plane text file with a list of opportunities with very specific format and calculations.  Any ideas? I really appreciate any help.

Vijay N 18Vijay N 18
Hi There,
Where can i check the new created document.
I checked it under documents but it was not there.
Am i missing something
Thomas NoyesThomas Noyes
Minor correction to code above to create the text document. You do have to add folderId. When run from developer console, it told me.

d.folderId = <your folder id>;  // Perhaps get from SOQL query to Documents by a special named folder. 
// Better though to probably already HAVE a document you update periodically and just query it in SOQL, fetch and clone it to new name perhaps.

Also  perhaps a 'global' org folder for some kinds of text documents might be good. Not sure if names need to be unique. But might be good to even update an existing text document - perhaps to allow download of some text file from Salesforce data you need to use for another purpose such as a static file on your website. We do something like that on our website. Not elegant but works. Sure something better and more automated exists but beyond our budget! And needs updating infrequently.