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
Kathleen LehnigkKathleen Lehnigk 

Apex pattern matching from email received with different language options

Hi there,

We have some apex class, that will pick up the original sender from an email-to-case, if the email is forwarded into Salesforce (so, i.e. subject line starts with FW:

While this works well in English, we have some subsidiaries, who will forward emails and the "From" value will then show in their local language (i.e. "Von" in German or "Fra" in Danish).

Below is the code part, that checks for the From email part on the email to take that email address and then later use it to ass as the SuppliedEmail on the case.

I've tried to find in the forums any way of creating different language versions, but nothing I've tried worked.

So, my hope is, that someone from the Community will be able to help with this part.

Thanks a lot
Kathleen
 
//This class will search through the email bodies to see if
    //we can find something similar to this pattern
    //From: Doe, John <john.doe@test.com>
    private static list<case> parseEmails(list<emailMessage> emList){
        try{
            map<id,case> cMap = new map<id,case>();
            map<id,string> emailMap = new map<id,string>();
            
            for(emailMessage em: emList){
                //looking for this pattern in the email body 'From: Doe, John <mailto:john.doe@test.com>'
                             
                pattern myPattern = pattern.compile('From:[a-zA-Z\\s\\,-]{1,100}[<|\\[][a-zA-Z0-9\\.\\-\\_\\:]{1,100}@[a-zA-Z0-9\\.\\-\\_]{1,100}[>|\\]]');
                matcher myMatcher = myPattern.matcher(em.TextBody);
                
                if(myMatcher.find()){
                    integer start = myMatcher.start();
                    integer stop = myMatcher.end();

                    string match = em.TextBody.substring(start,stop);
                    
                    //At this point you should have a string like this 'From: Doe, John <mailto:john.doe@test.com>'
                    //Now we will parse the name and the email from here
                    if(match.contains('[')){
                        start = match.indexOf('[');
                    }else{
                        start = match.indexOf('<');
                    }
                    
                    
                    string email = match.substring(start+1,match.length()-1).replace('mailto:','');
                    
                    //parse the name
                    string name = match.substring(0,start);
                    name = name.replace('from:','').replace('From:','').trim();
                    
                    //store the email address and case seperate
                    emailMap.put(em.id,email);
                    cMap.put(
                        em.id,
                        new case(
                            id = em.ParentId,
                            SuppliedEmail = email,
                            SuppliedName = name,
                            contactId = null
                        )
                    );
                    
                }