+ Start a Discussion
Pankaj_GanwaniPankaj_Ganwani 

Removing html numbers from csv created using apex

Hi All,

When we are creating csv file using apex code, I am getting html number corresponding to the special symbol(',','''). I have mentioned my code below:
PANG__Child_Object__c obj = [SELECT Id, PANG__test_rich__c from PANG__Child_Object__c where Id =: 'a0K28000006bqaA'];
        string recordString = '"'+obj.id+'","'+obj.PANG__test_rich__c.replaceAll('\\<[^\\>]*\\>','  ')+'"';
        //recordString = recordString.escapeEcmaScript();
        Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
        blob csvBlob = Blob.valueOf(recordString);
        string csvname= 'Account.csv';
        csvAttc.setFileName(csvname);
        csvAttc.setBody(csvBlob);
        csvAttc.setContentType('text/csv');
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new List<String>{'email'});
        mail.setSubject('test');
        mail.setcharset('UTF-8');
        mail.setHtmlBody('find attachment');
        mail.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});

        Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{mail});

I am getting '&#39;'(html number of single quote) instead of single quote in received file. Please let me know how to fix this.

Thanks,
Pankaj
Raj VakatiRaj Vakati
Hello Pankaj, 

Please use this below code. Write a regular expression if you have more characters like this for ease. 

 String recordString = '"'+obj.id+'","'+obj.PANG__test_rich__c.replaceAll('\\<[^\\>]*\\>','  ')+'"';
recordString = recordString.replace('&#39','\'');
System.debug('recordString'+recordString);
Let me know any help needed .