I am working on some code that retrieves certain cases and then emails those cases as a csv. This is working to some extent. The problem I have is that one of the fields is 'Description' which is a long text area field, this means the values here can contain newlines and special characters.

When a case has a description with at least 1 newline, it breaks the formatting of the csv file I receive as it converts those extra lines in the description into new rows in the csv file.

You can see commented in the code below what I've done to solve this but I am not sure this is the correct way to do it. At this point, I am getting the value in a single column but now I have many html encoded characters in that column in the CSV.

Code that generates rows/files:

string header = 'Record Id, Case Number , Created Date, Modified Date, subject, description, Web Name, Web Email, \n';
            string finalstr = header ;

               for(Case c: junklist)

                  string description=encodingutil.urlencode(c.description,'UTF-8').replace('+',' '); // Doing this has somehow solved the problem. 

                  string recordString = c.id+','+c.casenumber+','+c.CreatedDate+','+c.LastModifiedDate +','+ c.subject+','+ description 
                  +','+c.suppliedname+','+c.suppliedemail+ '\n';
                  finalstr = finalstr +recordString;


3 Answers 3


Can you not simply use String.replace to replace the new line character (\n) with e.g. a space:

String description = c.description.replace('\n', ' ');

Also, if some of the other special characters include " and , etc. that will mess up your csv it would be worth doing some kind of replace/escape on those too.

  • Of course, replaceAll will get rid of all of them and not just the first one :) Oct 18, 2012 at 9:54
  • Does this work though? When I tried it, just replacing \n didn't seem to work. I had to use \r\n ! Oct 18, 2012 at 13:26
  • I guess it depends on how the text was input, I had a similar problem recently when parsing the BillingStreet on an Account, I just used \n and it worked fine. Could it be you are on a Mac or Linux distro? I wonder if they always create newline as \r\n. Oct 18, 2012 at 14:35
  • 1
    Thanks guys. I ended up using this: description = c.description.replace('\n', ' ').escapeCsv();
    – PepeFloyd
    Oct 18, 2012 at 14:52
  • 2
    \n is *nix style line endings, \r\n is windows style, it's often best to just use an extra script statement and description.replaceAll('\r',''); description.replaceAall('\n',''); Oct 19, 2012 at 3:49

Just tried this quickly, and replacing \r\n with space seems to get rid of the newline. You might want to replace with stops.

System.debug([Select Id, Description from Case].Description.replaceAll('\r\n',' '));

Generally when working with CSVs, you can delimit columns with quotation marks or some other character for this; the delimiting character is then escaped in the data.

An example would be "Column\nA", "Column ""B"""

Most programs, including Excel, will honor this when opening the CSV.

You must log in to answer this question.

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