we are trying to export data into csv file using Apex and Visualforce page in Salesforce . The data is being exported but we have an issue . Here we are exporting record from an object(Text Fields) . Whenever we have a comma in a field value . Data gets moved to anaother cell . Example : I have a record with Name =" Department, Name " when exported : "Department " in one cell and " name " in another . we are trying to resolve this issue usingin this way .

 List<CDI_Pre__c> preClassifieds = [SELECT Name FROM CDI_Pre__c];
                  for(CDI___c preClassified :preClassifieds){
                      wrapper w = new wrapper();
      String name1=preClassified.CDI_Additional_Info__c==null?'':preClassified.Name.contains('"')?preClassified.Name.replaceAll('"',''):preClassified.Name;
     w.name = name1.contains(',')?'"'+name1+'"':name1;

**The above solution didnt work for name field ** Any suggestion is appreciated , Thankyou

enter image description here

  • 1
    You should read all of RFC4180 section 2, which, to the extent that CSV is standardized, defines the CSV standard. You're not currently handling embedded quotes as specified in the standard, and you're not handling embedded newlines at all. Consider using a JavaScript library to generate CSV data.
    – David Reed
    Nov 13, 2020 at 15:08

1 Answer 1


Try this code example:

w.name = '""';
if (String.isNotBlank(reClassified.CDI_Additional_Info__c) && String.isNotBlank(preClassified.Name)) {
    w.name = '"' + reClassified.Name.replace('"', '') + '"';


"" will not make your csv any worth if you do not have " in column values. Also use replace here, not replaceAll.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .