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
Shaan Khokhar 9Shaan Khokhar 9 

How To Make Email-To-Case Use Subjects To Combine Them

Hi all,
We have a scenario where someone has a system that when they reply, it doesn't directly reply meaning the case thread ID is lost.

However, the system they use include there own support case number and this will be the same number, but, there subject changes after creation of the case to include 'Updated'. 

I've attempted to utilize my code here inside the Apex Trigger of Email Message but it doesn't seem to be doing anything:

 

trigger AssociateEmail on EmailMessage (before insert) {

    for(EmailMessage eMsg : Trigger.new) {
        String json = JSON.serialize(eMsg.Subject);
        String jsondata = json.replaceAll('[^0-9]','');
        
        System.debug('test');
        System.debug(jsondata);

        List<Case> c = [select Id from Case where Subject = :jsondata LIMIT 1];
        
        
        if(c.size() > 0) {
            
            eMsg.Id = c[0].Id;
        }
    }
}
David Zhu 🔥David Zhu 🔥
I assume you want to associate this email to the case matching the subject. You may need to change line 15 to:
eMsg.ParentId = c[0].Id;