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
nabil711nabil711 

custom button that generate a flat file

Hi

 

I need to create a custom button that generate a flat file .txt

 

is there any method to create a file on apex code?

 

Thanks for your help

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

d.id will contain the ID of the file you just saved (starting with the line insert d).

All Answers

sfdcfoxsfdcfox

You can write to a string, and save that string as a blob, or output the string in a visualforce page. By setting the contentType of a visualforce page to "text/plain", you can dump out the file as plain text.

nabil711nabil711

Thanks 

 

I created a custom buttom that execute an apex method.

 

this method create the file (document d = new document();.......) and must return the url.

 

global class GenerateFile {

WebService static string Generer(string adr) {


Document d=new Document();

//d.FolderId='005d0000000bWCG';
d.FolderId=UserInfo.getUserId();
d.Name = 'my text file';
String myContent = 'ADDRESSSE : '+adr;
d.Body = Blob.valueOf(myContent);
d.ContentType = 'text/plain';
d.Type = 'txt';

insert d;

string IdFichier='';  // id file

string chemin='/servlet/servlet.FileDownload?file='+IdFichier;

return chemin;

}
}

 

how can i get the idFile ????

 

 

sfdcfoxsfdcfox

d.id will contain the ID of the file you just saved (starting with the line insert d).

This was selected as the best answer
nabil711nabil711

Thanks it works :smileyvery-happy: