+ Start a Discussion
bpl3792bpl3792 

Is it possible to generate a .csv file and add it to the attachments object programatically?

We want to be able to log an object for historical purposes but it's way too much data to do that through salesforce so I figured we could dump the data into a CSV file. I want to be able to dump the CSV file itself directly to the attachments object if that's possible. 

The way I'm currently generating the CSV file is by using the following in my visualforce page...

<apex:page standardController="Facility_Carrier_Contract__c" extensions="mc_FCCCodeHistory" id="page" standardStylesheets="false" showHeader="false" sidebar="false" contenttype="application/x-excel#export.csv">
{!exportData}</apex:page>

 

 

Best Answer chosen by Admin (Salesforce Developers) 
ShamilShamil

If you want to create .csv in Apex you could use the following code snippet:

 

String csvContent = 'column1,column2\nvalue1,value2';
Attachment attachment = new Attachment();
attachment.Body = Blob.valueOf(csvContent);
attachment.Name = 'csvFile.csv';
attachment.ParentId = 'your_parent_record_id'; 
insert attachment;

 HTH

All Answers

ShamilShamil

If you want to create .csv in Apex you could use the following code snippet:

 

String csvContent = 'column1,column2\nvalue1,value2';
Attachment attachment = new Attachment();
attachment.Body = Blob.valueOf(csvContent);
attachment.Name = 'csvFile.csv';
attachment.ParentId = 'your_parent_record_id'; 
insert attachment;

 HTH

This was selected as the best answer
bpl3792bpl3792

Wow, that was incredibly easy. Thanks!

Prem GuptaPrem Gupta
But what about if the csv contains string with space. How to process it like store in List<String> ?
sam paul 13sam paul 13
Hello Everyone!!! 
I have it exact questions(60/65 questions) with verified answers dump for Salesforce Administrator (ADM 201, Advanced Administrator (ADM 211), Platform Developer 1 (CRT 450), Platform App Builder (CRT 403), SalesCloud Consultant (CRT-251) and ServiceCloud Consultant (CRT-261) Spring 2020. Many of them cleared their first shot. 200% assured & accuracy. Mail me at sampaul.yes@gmail.com
Sundar Kanna 7Sundar Kanna 7
Please contact me for  Latest dumps SU'21 passed by lots users. The dumps updated by just a week before. And, It's very accurate questions with verified answes. I have Admin 201, Platform Developer 1 and App Builder SU'21 edition with exact screenshot dumps. You can mail me at anytime - sundar.kanna119@gmail.com