function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Email Problem - Apex -



I have a script that works together with a visualforce page, it is a form. 

The script will take the language in the current page and based on that it will send an email to the a specify service department.

I just use this to get the language 'language = Apexpages.currentPage().getParameters().get('lang');'.

Before that I have a Map ready to accommodate the values: 


Map<String, String[]> serviceEmailAddresses = new Map<String, String[]>();


So the intention is that depending in the user's current page language to send an email to the correct service department, like I mention before.

If the page language is 'en' the email will go to, if the current page language is 'es' the email goes to and so on, these are the languages we use: 'en', 'be', 'es','de', 'fr', 'it', 'nl' and 'pt'.


Each of those values have an email assign to it. 


public ServiceForm() {
        language = Apexpages.currentPage().getParameters().get('lang');
        if ((language == 'en')&&(serviceType != 'Spare Parts Order')) {
            serviceEmailAddresses.put('en', new String[] {''});
        serviceEmailAddresses.put('en', new String[] {''});
        serviceEmailAddresses.put('be', new String[] {''});
        serviceEmailAddresses.put('es', new String[] {''});
        serviceEmailAddresses.put('de', new String[] {''});
        serviceEmailAddresses.put('fr', new String[] {''});
        serviceEmailAddresses.put('it', new String[] {''});
        serviceEmailAddresses.put('nl', new String[] {''});
        serviceEmailAddresses.put('pt', new String[] {''});
        if (language != 'be'&& language != 'en' && language != 'es' && language != 'de' && language != 'fr' && language != 'it' && language != 'nl' && language != 'pt') {
            language = 'en';

 At the end we use this to send an email to the service department:


 private void sendServiceMail() {
        Messaging.SingleEmailMessage serviceMail = new Messaging.SingleEmailMessage();
        serviceMail.setSubject('' + companyName + ', ' + addressCodeCity + ' - ' + serviceType + ' - ONLINE REQUEST');
        String serviceMailMessage = Label.Email_Subject_Service + ':\n\n';
        serviceMailMessage = serviceMailMessage + setPlainTextMessage('\n\n');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { serviceMail });        

 The third line I believe is correct, can anyone let me know otherwise?: 

                  serviceMail.setToAddresses (serviceEmailAddresses.get(language)); 


 The form works fine but for some unknown reason, sometimes it sends emails to the wrong inbox. Last time a client from Spain submitted his form     and it went to the uk.serviceparts address!! 

 Does anyone knows why this is happening? The script looks pretty straightforward ... 


Any help on this will be much appreciated. 


No one?:(

Hi Miles.,


Please Check corrsponding user's Locale and time zone.,




Nantha, thanks for your comment but everything starts when the user opens a visualforce page from our website. 

The custom controller picks up the language from the actual page and everything starts from there. 




Spanish(Mexico) having the Language code like es_MX. So please check if the user falling in to this category., because in you code there is no condition fores_MX, so it is moving to en default(Check your if condition below the Email config)