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
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.



 
Michael BreedenMichael Breeden
Looking at the above code, it does seem to require a folderId as you say, but I haven't been able to figure out what a folderId is. I  made a folder at c:\Temp for testing but when I run it I get:
Document d = new Document();
d.Name = 'mytextfile.txt';
d.folderId = 'Temp'; // Error is: System.StringException: Invalid id: Temp
String myContent = 'aabbcc';
d.Body = Blob.valueOf(myContent);
d.ContentType = 'text/plain';
d.Type = 'txt';
insert d;
I need to extract actual data from one Salesforce Org to process it and move it to another Org. I can use APEX (with SOQL) to extract the data. I can process any data that I can get ahold of, but I'm having trouble getting it out to where I can proces it. Any tips would be appreciated.