+ Start a Discussion
Sfdc Project 17Sfdc Project 17 

Need help in writing data to csv file

i have a list of data in String form, 
                                    Record Id, Name, Stage Name 
                                    "0067F000005RIcsQAG","Robert","Closed Won" 
                                    "0067F000005RMDIQA4","Kylie","Closed Won" 
                                    "0067F000005RMDJQA4","Ernie","Closed Won"
                                    Now, I want help in writing this data into a CSV file using apex and generate that csv file automatically

Any help would be greatly appreciated
Navin SoniNavin Soni
Hi Sfdc,

You can create a CSV file and get through email using this code.
List<Opportunity> opplist = [Select Id, Name , StageName from Opportunity limit 10];
string header = 'Record Id, Name, Stage Name \n';
string finalstr = header ;
for(Opportunity objOpp : opplist)
       string recordString = '"'+objOpp.id+'","'+objOpp.Name+'","'+objOpp.StageName +'"\n';
       finalstr = finalstr +recordString;
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(finalstr);
string csvname= 'Opportunity.csv';
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] toAddresses = new list<string> {'navinsoni2636@gmail.com'};
String subject ='Opportunity CSV';
email.setToAddresses( toAddresses );
email.setPlainTextBody('Opportunity CSV ');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

Navin Soni
Sfdc Project 17Sfdc Project 17
Thank you so much for the resposnse navin,
but i don't want to generate csv from email,
i want to generate the csv file onclick of a link in my vf page
maybe it will use contentDocument or contentDocumentLink concept.
any further assistance or help?