0

I have a custom object in which I have a long text area field. I am querying it in apex and building a CSV and sending an email with csv as attachment.

The value in the long text area field and which is also expected in CSV is this text has double "quotes" But in CSV I am getting the value as this text has double quotes""

please let me know if it is possible to handle double quotes when generating a CSV and if so please let me know how to do it.

Adding more info

Query = [Select Id,CreatedDate,owner,Model__c,SerialNumber__c,RepairDetailDescription__c from case Where Id in : caseIds ];

string finalString = 'Created Date,owner,Model,SerialNumber,RepairDetailDescription  \n';

for(case c : Database.query(Query)){
    string recordString = '"'c.CreatedDate+'","'+c.owner+'","'+c.Model__c+'","'+c.SerialNumber__c+'","'+c.RepairDetailDescription__c+'"\n';
    finalString = finalString + recordString;
}


  if(selectedfinalstr!=null){   
                Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
                blob csvBlob = Blob.valueOf(finalString);
                string csvname= 'Selected Reco Export.csv';
                csvAttc.setFileName(csvname);
                csvAttc.setBody(csvBlob);
                Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
                OrgWideEmailAddress owe = [SELECT ID,IsAllowAllProfiles,DisplayName,Address FROM OrgWideEmailAddress WHERE IsAllowAllProfiles = TRUE LIMIT 1];
                String userName = UserInfo.getUserName();
                User activeUser = [Select Email From User where Username = : userName limit 1];
                String userEmail = activeUser.Email;
                String[] toAddresses = new list<string> {userEmail};
                String subject ='Recommendation';
                email.setOrgWideEmailAddressId(owe.Id);
                email.setSubject(subject);
                email.setToAddresses( toAddresses );                
                email.setHtmlBody('Find the attached Exported recommendation details.');                
                email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
                Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
             }  
4
  • 1
    Can you please add your query code and the code where you build the CSV?
    – Moonpie
    Commented Jun 14, 2021 at 12:15
  • a code of what you are doing will help to debug where the problem lies. Right now there are too many assumptions to make based on your problem statement as there is no source to look at why there could be a problem
    – GirishP
    Commented Jun 14, 2021 at 13:25
  • I have added extra details to the post hope this helps in debugging
    – Sisir
    Commented Jun 14, 2021 at 13:54
  • One thing to note (which could be a copy/paste issue) is that in your for loop you have string recordString = '"'c.CreatedDate+.... It seems that you are missing a +: string recordString = '"' + c.CreatedDate + .... I find that these things are easier to spot if I put spaces around my +es.
    – Moonpie
    Commented Jun 14, 2021 at 18:46

2 Answers 2

1

When generating CSVs, Use the escapeCsv method on all string cells as you won't know in advance whether any database data might or might not have an embedded " or line-break

String s = 'abc "def" ghi';
System.debug(LoggingLevel.INFO,s.escapeCsv());

|USER_DEBUG|[2]|INFO|"abc ""def"" ghi"

RFC4180 defines valid CSV format

If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote. For example:

   "aaa","b""bb","ccc"
0

Because in the string recordString = ... line you are surrounding everything by double quotes, then you need to escape any double quotes that exist in your field value.

Assuming that c.RepairDetailDescription__c is the field (potentially) containing double quotes, you could do something like this:

for(case c : Database.query(Query)){
   String doubleQuoteEscapedField = c.RepairDetailDescription__c.replace('"','""');
   string recordString = '"' + c.CreatedDate + '","' + c.owner + '","' + c.Model__c + '","' + c.SerialNumber__c + '","'+ doubleQuoteEscapedField + '"\n';
   finalString = finalString + recordString;
}

Or you could try to reverse the use of your single and double quotes on the line where you build your recordString - but this would work ONLY if the field in question is guaranteed to ABSOLUTELY NEVER contain single quotes. Because if it did, you would have to do similar to my above answer in order to escape the single quotes.

You must log in to answer this question.

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