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});
}
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.