0

I have a VFP which functions as a CSV download. In my controller I return a simple SOQL list from a custom Customer object. I need to modify the Name being returned to remove all commas and then add it back to my list as it is breaking the csv file. I haven't been able to figure out how to modify the string and add it back to the list with no commas.

Example would be |Customer_ID__c|Name|Modified Name (Desired)| |--------------|----|-----------------------| |1|Bobs Company (Atlanta, GA)|Bobs Company (Atlanta GA)| |2|Jims Company (San Francisco, CA)| Jims Company (San Francisco CA)|

public inherited sharing class CSVExportController {
    
    public String customerId {
        get {
            if (customerId == null) {
                customerId = System.currentPageReference().getParameters().get('id');
                
            }return customerId;
        }set;
    }
    
    public List<SPCustomer__c> getCustomerExport {
        get{
            if(getCustomerExport == null){ 
                getCustomerExport = [SELECT Customer_ID__c, Name from SPCustomer__c where Id = :this.customerId limit 1];
            }return getCustomerExport;
        }set;
    }
    
    public String COMMA {
        get {
            return String.fromCharArray(new List<Integer> { 44 });
        }
    }
    
    public String CRLF {
        get {
            return '\r';
        }
    }
    
    public CSVExportController() {}
    
}

Visualforce Page:

<apex:page Controller="CSVExportController" showHeader="false" sidebar="false" cache="true" contentType="application/octet-stream#CustExport.csv">
    <!-- Header Row -->
    <apex:outputText value="Cust_Number"/><apex:outputText value="{!COMMA}"/>
    <apex:outputText value="Cust_Name"/><apex:outputText value="{!COMMA}"/> 
    <apex:outputText value="{!CRLF}"/>
    
    <!-- Data rows -->
    <apex:repeat value="{!getCustomerExport}" var="cust">    
        <apex:outputText value="{!cust.Customer_ID__c}"/><apex:outputText value="{!COMMA}"/>
        <apex:outputText value="{!cust.Name}"/><apex:outputText value="{!COMMA}"/>
        <apex:outputText value="{!CRLF}"/>
    </apex:repeat>
</apex:page>
2
  • 1
    Notes: '\r' isn't CRLF, it's just CR. You want '\r\n' if you want CRLF. Also, COMMA could literally just return ','.
    – sfdcfox
    Commented Apr 22, 2022 at 0:28
  • Yeah, that was an overlook from my part. Appreciate the knowledge drop.
    – Wes
    Commented Apr 22, 2022 at 1:10

1 Answer 1

1

You don't want to remove the commas, you just want to escape them properly. You'd also have problems with quote characters ("). Just do this:

getCustomerExport = [SELECT Customer_ID__c, Name from SPCustomer__c where Id = :this.customerId limit 1];
for(SPCustomer__c record: getCustomerExport) {
  for(String field: getCustomerExport.getPopulatedFieldsAsMap().keySet()) {
    Object value = record.get(field);
    if(value instanceof String) {
      record.put(field, ((String)value).escapeCsv());
    }
  }
}

You might want to read more about how CSV is supposed to work in RFC 4180.

1
  • This worked @sfdcfox. I appreciate your quick, accurate help, and the article on RFC 4180.
    – Wes
    Commented Apr 22, 2022 at 1:11

You must log in to answer this question.

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