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
Mohidheen NMohidheen N 

Wants to convert string into CSV via apex code

I wants to convet the below string format into csv in Apex, do anyone having any idea or sample apex code to convert into csv document file and save the attachemtn in csv file format in attachment object.

NAICS CODE,LOC #,SUB LOC #,LOCATION NAME,ADDRESS,ADDRESS LINE 2,ADDRESS LINE 3,CITY,STATE/PROVINCE,COUNTY
551114,0,0,,adc,,,ABC,TX,ABCd
531110,2,1,,7729 WESTVIEW DR,,,ABC,TX,ABCd
334513,3,1,AAA-A,1330 WIRT RD,,,ABC,TX,ABCd
334513,3,2,AAA-B,1330 WIRT RD,,,ABC,TX,ABCd

Note: Column header count is ten in the above sample but it may very everytime.
Malika Pathak 9Malika Pathak 9
<apex:page controller="exportCSVController" cache="true" contentType="text/csv#Export.csv" language="en-US">
    "Col A","Col B"
    <apex:repeat value="{!myList}" var="a">
        <apex:repeat value="{!a}" var="asub">
        "{!asub.val1}","{!asub.val2}"
        </apex:repeat>
    </apex:repeat>
</apex:page>
 

Hi 
Mohidheen N,

Please find the solution.

public class exportCSVController {

    public List<List<myClass>> myList {get;set;}
    
    public exportCSVController() {
        myList = new List<List<myClass>>();
        List<myClass> temp = new List<myClass>();
        
        for(Integer i = 0; i < 2500; i++){
            if(temp.size() < 1000){
                myClass m = new myClass();
                m.val1 = 'val1 ' + i;
                m.val2 = 'val2 ' + i;
                temp.add(m);
            }
            else{
                myList.add(temp);
                temp = new List<myClass>();
                myClass m = new myClass();
                m.val1 = 'val1 ' + i;
                m.val2 = 'val2 ' + i;
                temp.add(m);
            }
        }
        myList.add(temp);
    }
    

    public class myClass{
        public string val1 {get;set;}
        public string val2 {get;set;}
    }
}


 VisualForce Page:

Or You can use batch class

 

global class SetupAuditTrailBatch implements Database.Batchable<sObject>, Database.Stateful {
   
    global String csvColumnHeader;
    global List<String> csvRowValues = new List<String>();
   
    global Database.QueryLocator start(Database.BatchableContext BC){
        //Query all SetupAuditTrail records.
        String query = 'SELECT CreatedDate, CreatedBy.Username, Display, Section, Action, DelegateUser, CreatedById, CreatedBy.Name FROM SetupAuditTrail ORDER BY CreatedDate DESC';
        return Database.getQueryLocator(query);
    }
   
    global void execute(Database.BatchableContext BC, List<sObject> scope){
        //Process retrieved SetupAuditTrail records and format field values.
        for(SetupAuditTrail currSetupAudit : (List<SetupAuditTrail>) scope){
            String formattedDate = currSetupAudit.CreatedDate.format('M/d/yyyy h:mm:ss a z');
            String userName = currSetupAudit.CreatedBy.Username != null ? currSetupAudit.CreatedBy.Username : '';
            String displayVal = currSetupAudit.Display != null ? String.valueOf(currSetupAudit.Display).escapeCsv() : '';
            String sectionVal = currSetupAudit.Section != null ? currSetupAudit.Section : '';
            String delegateUser = currSetupAudit.DelegateUser != null ? currSetupAudit.DelegateUser : '';
           
            String rowStr = formattedDate + ',' + userName + ',' + displayVal + ',' + sectionVal + ','+ delegateUser;
            csvRowValues.add(rowStr);
        }
    }
   
    global void finish(Database.BatchableContext BC){
        List<Folder> folders = [SELECT Id, Name FROM Folder WHERE Name = 'Setup Audit Trail Logs'];
       
        if(!folders.isEmpty()){
            String documentName = 'SetupAuditTrailLog-'+ Datetime.now().format('MMM') + Datetime.now().year();
            csvColumnHeader = 'Date, User, Action, Section, Delegate User\n';
            String csvFile = csvColumnHeader + String.join(csvRowValues,'\n');
           
            // Insert the generated CSV file in Document object under "Setup Audit Trail Logs".
            Document doc = new Document(Name = documentName, Body = Blob.valueOf(csvFile), FolderId = folders[0].Id, Type = 'csv', ContentType='application/vnd.ms-excel');
            insert doc;
        }
    }
}
Mohidheen NMohidheen N
Hi @Malika Pathak 9,

We get this in rest response and I tried to store in a string datatype but its not accepting it due to Line break in the text. I wants to inteligently understand the column count and then have to store in the list later convert into csv file.