I created a scheduled job which sends an email to System Administrator everyday. The scheduled job works in English and Japanese. I created a custom label and used translation workbench to translate the text. When I tested, I switched to both language and made sure that both works fine.
I put the job in a managed package and then installed it on a test environment, using System Administrator account that uses Japanese language. On the next day, an email arrived in Japanese language. So, I switched the System Administrator account's language to English and waited for another email. But the next email was also in Japanese. Then I guessed that System Administrator account doesn't own the scheduled job. Still, I want to send the email in System Administrator's chosen language.
I can retrieve System Administrator's chosen language but I don't know if I could choose to use that language when the executed job send out the email. Is there any good workaround?
This is my sending email class.
public static boolean send(String body, String subject, String[] toAddresses) {
// Define the email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
// Sets the paramaters of the email
email.setSubject( subject );
email.setToAddresses( toAddresses );
email.setPlainTextBody( body );
// Sends the email
try{
Messaging.SendEmailResult [] r =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
return true;
}
catch(Exception e){
return false;
}
}
This is where the subject and body be retrieved from custom label.
if (sendEmail){
String subject = Label.MailSubject;
String body = String.format(Label.MailBody, new String[] {Label.LinkText});
System.debug(LoggingLevel.Error, body);
BodaisSendEmail.send(body, subject, toAddress);
}