Salesforce Email To Case using Inbound Email Service

Salesforce Email To Case using Inbound Email Service

Sample Apex Class:
global class createCaseInbound implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail( Messaging.InboundEmail email, Messaging.InboundEnvelope env ) {
   
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();         
        String myPlainText = email.plainTextBody;
           
        try {
        
            Case objCase = new Case ( Description = myPlainText, Subject = email.Subject );
            objCase.Origin = ‘Email’;
            objCase.Status = ‘New’;
            insert objCase;      
            
            EmailMessage emailMessage = new EmailMessage();  
            emailMessage.Status = ‘0’; // email is New
            emailMessage.FromAddress = email.FromAddress;
            if ( email.toAddresses.size() > 0 )
                emailMessage.ToAddress = String.join( email.toAddresses, ‘;’ );
            if ( email.ccAddresses != null && email.ccAddresses.size() > 0 )
                emailMessage.CcAddress = String.join( email.ccAddresses, ‘;’ );
            emailMessage.FromName = email.FromName; // from name  
            emailMessage.Subject = email.Subject; // email subject  
            emailMessage.TextBody = myPlainText; // email body  
            emailMessage.RelatedToId = objCase.Id;
            insert emailMessage; // insert    
                
            result.success = true;       
            
        } catch ( Exception e ) {
        
            System.debug( ‘Error is: ‘ + e.getMessage() );   
            result.success = false;     
            
        }        
        return result;
        
    }
    
}

Email Service:
 

 

Output:

Leave a Reply