for(sObject sObj : scope) {
            AggregateResult ar = (AggregateResult)sObj;
            Integer IdCount = (Integer)ar.get('cnt');
            String accName = (String)ar.get('accName');

                csvRow = accName + ','+ IdCount;

here is the code where I create the CSV File Here the problem is when the CSV file is created the field value of accName use the comma(,) like (sunny,roxx) then the created CSV file columns are not assigedenter image description here

here you see in the image 'mortgage & investment agenct' comes to other columns please give me a suggestion for solved this error thanks in advance

  • As explained in how to escape comma in csv your string should be enclosed in double-quotes.
    – McCubo
    Aug 30, 2022 at 11:30
  • Can you please give the answer of this question how to resolve, I will mark your answer is best answer
    – sunny roxx
    Aug 30, 2022 at 11:37
  • Can you please give the answer to this example of how to resolve it, I will mark your answer as the best answer thanks.
    – sunny roxx
    Aug 30, 2022 at 11:44
  • The answer provided to you does not escape characters properly. You will experience errors in some cases.
    – sfdcfox
    Aug 30, 2022 at 12:38

2 Answers 2


the only thing you need to do is update the line where you assign the value to csvRow as follow:

csvRow = '"' + accName + '",'+ IdCount;

you only need to concatenate " before variable accName and also after it. hope it helps!

  • Thank you very much for giving your valuable time.
    – sunny roxx
    Aug 30, 2022 at 11:52
  • Your code doesn't handle quotation characters in the string. The "Famous" Dave's would break the CSV.
    – sfdcfox
    Aug 30, 2022 at 12:33

You should use escapeCSV to make sure that you're dealing with CSV characters correctly.

csvRow = accName.escapeCsv() + ','+ IdCount;

You must log in to answer this question.

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