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
DayeneDayene 

Actionate custom email handler from Email-to-Case

Hi there,
I have the following apex email handler which pourpose is to update de priority of a case create from email-to-case according to the priority that is informed on the subject of the email that originated the case:


global with sharing class EmailToCasePriorityHandler implements Messaging.InboundEmailHandler{
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
        System.debug('get in the email class');
        // Parse the email to extract the priority
        String subject = email.subject;
        Pattern pattern = Pattern.compile('(Case #(\\d+) - Priority (\\d)');
        Matcher matcher = pattern.matcher(subject);
        System.debug('subject' + subject);
        if (!matcher.matches()) {
            System.debug('does not match' );
            result.message = 'Email subject is not in the right format.';
            return result;
        }
        String priority = matcher.group(2);
        // Retrieve the case and update the priority
        Case theCase = [SELECT Id, Priority, CaseNumber FROM Case WHERE Subject = : subject LIMIT 1];
        if (theCase == null) {
            result.message = 'Case not found.';
            return result;
        }
        theCase.Priority = priority;
        update theCase;
        // Set the response message and status
        result.message = 'Case ' + theCase.Id + ' priority updated to ' + priority;
        result.success = true;
        return result;
    }
}


I have set up the email-to-case and it is working fine.

I have also configured the Email Services to this apex class.

But for some reason when a case is create from email-to-case this class is not being called.

What can it be?

And is there a better way to implement this custom email handler? Because I am using the subject of the email to retrieve the correct case to update.

Thanks!

Shivdeep KumarShivdeep Kumar
Hi Dayene,

As per my understanding, you have created a Email-to-case service and Email Service Handler as well. you want to call your service handler whenever the case is created from both way.
To achieve this functionality , Please look into the below link.
http://Messaging.InboundEmailHandler
SubratSubrat (Salesforce Developers) 
Hello Dayene ,

I have cam across an article that could clarify your doubts on the same .
Requesting you to go through this once --> https://github.com/susom/salesforce-email-handler 

Hope it helps !
Thank you.