You need to sign in to do that
Don't have an account?
sudhakartrao1.3965789150856177E12
Japanese text in email attachment
Hi,
I am extracting salesforce data in csv and sending in email using scheduled apex.
Every thing looks find excpet the Japanese text display in CSV file looks junk.
What is the content type of the attachment to handle japanese text as well.
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(finalstr);
string csvname= 'Foreign Companies List.csv';
csvAttc.setContentType('text/csv; charset=UTF-8');
csvAttc.setFileName(csvname);
csvAttc.setBody(csvBlob);
I am extracting salesforce data in csv and sending in email using scheduled apex.
Every thing looks find excpet the Japanese text display in CSV file looks junk.
What is the content type of the attachment to handle japanese text as well.
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(finalstr);
string csvname= 'Foreign Companies List.csv';
csvAttc.setContentType('text/csv; charset=UTF-8');
csvAttc.setFileName(csvname);
csvAttc.setBody(csvBlob);
Just need to add encoding as UTF-8. Once you get the mail with attachment try opening it with some text editor instead of excel.
This issue if faced by some people previously. http://salesforce.stackexchange.com/questions/18546/unable-to-display-utf-8-character-in-a-csv-file-generated-from-apex-code
global class foreignCompanies Implements Schedulable
{
global void execute(SchedulableContext sc)
{
sendmail();
}
public void sendmail()
{
string c ;
List<Account> acclist = [Select id,Name,BRN_UEN_NRIC__c,CreatedDate,CreatedBy.Name from Account where Is_Foreign_Company__c=TRUE and record_type__c='Advertiser' order by createddate desc limit 1];
string header = 'Id ,Account Name,BRN/UEN/NRIC/Foreign ID,Created Date,Creator \n';
string finalstr = header ;
for(Account a: acclist)
{
c = a.Name.replace(',',' ');
string recordString = a.id+','+c+','+a.BRN_UEN_NRIC__c+','+a.CreatedDate+','+a.CreatedBy.Name+'\n';
finalstr = finalstr +recordString;
}
finalstr = finalstr.replace('null','');
Datetime dateTimetemp = System.now();
Date dateTemp = Date.newInstance(dateTimetemp.year(),dateTimetemp.month(),dateTimetemp.day());
String x=dateTemp.format();
Map<String,ForeignCompany__c> mapRecipients = ForeignCompany__c.getAll();
List<String> arrytoAddresses = new List<String>{};
List<String> arryccAddresses = new List<String>{};
for(ForeignCompany__c objRecipient : mapRecipients.values()){
system.debug('+++'+objRecipient.To__c);
if(objRecipient.To__c==true)
arrytoAddresses.add(objRecipient.Email__c);
else
arryccAddresses.add(objRecipient.Email__c);
}
system.debug('+++'+arrytoAddresses);
system.debug('+++'+arryccAddresses);
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(finalstr);
string csvname= 'Foreign Companies List.csv';
csvAttc.setFileName(csvname);
csvAttc.setBody(csvBlob);
csvAttc.setContentType('text/csv;charset=UTF-8');
//csvAttc.ContentType = 'text/csv; charset=UTF-8';
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String subject ='Foreign Companies List '+x;
email.setCharset('UTF-8');
email.setSubject(subject);
email.setToAddresses(arrytoAddresses);
email.setCcAddresses(arryccAddresses );
email.setPlainTextBody('This is a system generated daily report of Foreign Companies');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
}
Let me know where to add Encoding UTF-8. In order to open in some text editor user have to save the csv from the email.
But users should be able to open directly from email i.e csv file.
here, it is mentioned that the problem is not with csv it is with the excel.
http://salesforce.stackexchange.com/questions/18546/unable-to-display-utf-8-character-in-a-csv-file-generated-from-apex-code
This problem plagued me for weeks, and I found a REALLY simple solution! I love it when that happens....
When you are creating your .csv, just prepend a Byte Order Marker(BOM) at the beginning that will signal Excel to open it as UTF-8. So in your example above change this line to include this escaped BOM. Now Excel will recognize it correctly and open without having to import or save it in Notepad nonsense.
Please, 'like' if this was helpful. =o)