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
ZurMilesZurMiles 

Email Problem - Apex -

Hello, 

 

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 uk.service@something.com, if the current page language is 'es' the email goes to es.servicio@something.com 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[] {'uk.support@something.com'});
        }else{
       
        serviceEmailAddresses.put('en', new String[] {'uk.serviceparts@something.com'});
        serviceEmailAddresses.put('be', new String[] {'service.benelux@something.com'});
        serviceEmailAddresses.put('es', new String[] {'servicio.tecnico@something.com'});
        serviceEmailAddresses.put('de', new String[] {'kundendienst@something.com'});
        serviceEmailAddresses.put('fr', new String[] {'service.benelux@something.com'});
        serviceEmailAddresses.put('it', new String[] {'assistenzatecnica@something.com'});
        serviceEmailAddresses.put('nl', new String[] {'service.benelux@something.com'});
        serviceEmailAddresses.put('pt', new String[] {'servicio.tecnico@something.com'});
        }        
        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.setToAddresses(serviceEmailAddresses.get(language));
        serviceMail.setSenderDisplayName(Label.Sender_Display_Name);
        serviceMail.setReplyTo(noReplyEmail);
        serviceMail.setSubject('' + companyName + ', ' + addressCodeCity + ' - ' + serviceType + ' - ONLINE REQUEST');
        String serviceMailMessage = Label.Email_Subject_Service + ':\n\n';
        serviceMailMessage = serviceMailMessage + setPlainTextMessage('\n\n');
        serviceMail.setPlainTextBody(serviceMailMessage);
        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. 

Thanks

ZurMilesZurMiles
No one?:(
NanthaNantha

Hi Miles.,

 

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

 

 

ZurMilesZurMiles

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. 

 

 

NanthaNantha

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)