+ Start a Discussion
Sharon PragerSharon Prager 

Help with Inbound email testing

I am not a developer, but managed to put together an email handler class. Most immediate question, how do I write a test for this so I can put in production? Next question, how do I parse so that I can grab everything between ";". For example, Status: Closed;  Priority:  Low. Thanks!

global class CaseUpdateEmailHandler implements Messaging.InboundEmailHandler {
  global Messaging.InboundEmailResult handleInboundEmail(
  Messaging.InboundEmail email,
  Messaging.InboundEnvelope envelope)
  {
    String subject = email.subject;
    Pattern idPattern = Pattern.compile('500[A-Za-z0-9]{15}');
    Matcher matcher = idPattern.matcher(subject);
    if (!matcher.find()) System.assert(false, 'No Case Id in subject!');

 Case caset = [SELECT Status, Priority FROM Case WHERE Id = :matcher.group(0)];
  String[] emailBody = email.plainTextBody.split('\n', 0);
  String Status1 = emailBody[0].substring(7);
  String Priority1 = emailBody[1].substring(9);
  
  caset.Status = Status1;
  caset.Priority = Priority1;
    update caset;

    Messaging.InboundEmailresult result = new Messaging.InboundEmailResult();
    result.message = 'Case Status is now' + caset.Status; 
    return result;
  }
}
Best Answer chosen by Sharon Prager
SKolakanSKolakan
Sharon,
This should get you started. Tweak as you need
 
@isTest
public class CaseUpdateEmailHandler_Test {
    
      static testMethod void testEmailHandler(){
        
        //Create a dummy account and other records that your case object need. 
        //I recommend using a smartfactory or test data utility to create test data.
     	Account testAccount = new Account(name='Test Company Name');
     	insert testAccount;
  
        //Create a dummy case. Fill the fields you need
	 	Case ca = new Case(Subject='Test Case Update Email Handler');
        ca.AccountId = testAccount.Id;
		insert ca;
        
        // create a new email and envelope object
        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
        
        // setup the data for the email
        email.subject = 'Test email handler:' + ca.Id; //Since you are looking for case Id in the subject, add Id of the case you created
        email.fromname = 'FirstName LastName';
        email.plainTextBody = 'Status:New' +  '\n' + 'Priority:High';
        env.fromAddress = 'someaddress@email.com';
  
        CaseUpdateEmailHandler emailProcess = new CaseUpdateEmailHandler();
        Messaging.InboundEmailResult result = emailProcess.handleInboundEmail(email, env);
        
        //Assert results
		
        //Test for various cases like what happens if there is no email body,no case Id in the subject etc
    }
}

 

All Answers

SKolakanSKolakan
Sharon,

Please see this article: http://blog.jeffdouglas.com/2010/03/12/writing-an-inbound-email-service-for-salesforce-com/
Sharon PragerSharon Prager
Thank you for the quick reply! I actually consulted that article (and many more) to figure out how to write the code. This is all foreign language to me, but I will try to make sense of it! 
SKolakanSKolakan
Sharon,
This should get you started. Tweak as you need
 
@isTest
public class CaseUpdateEmailHandler_Test {
    
      static testMethod void testEmailHandler(){
        
        //Create a dummy account and other records that your case object need. 
        //I recommend using a smartfactory or test data utility to create test data.
     	Account testAccount = new Account(name='Test Company Name');
     	insert testAccount;
  
        //Create a dummy case. Fill the fields you need
	 	Case ca = new Case(Subject='Test Case Update Email Handler');
        ca.AccountId = testAccount.Id;
		insert ca;
        
        // create a new email and envelope object
        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
        
        // setup the data for the email
        email.subject = 'Test email handler:' + ca.Id; //Since you are looking for case Id in the subject, add Id of the case you created
        email.fromname = 'FirstName LastName';
        email.plainTextBody = 'Status:New' +  '\n' + 'Priority:High';
        env.fromAddress = 'someaddress@email.com';
  
        CaseUpdateEmailHandler emailProcess = new CaseUpdateEmailHandler();
        Messaging.InboundEmailResult result = emailProcess.handleInboundEmail(email, env);
        
        //Assert results
		
        //Test for various cases like what happens if there is no email body,no case Id in the subject etc
    }
}

 
This was selected as the best answer
Sharon PragerSharon Prager
Thank you so much! I will let you know!
Sharon PragerSharon Prager
It worked!!!! I was getting close, but not close enough on my own. The incoming emails are from another ticket service, and should always have the data, but I will work on null fields. I think what I have created selects a certain line in the email. I am wondering what happens if a text field ends up more than one line? Is there a way to parse by characters at the end of the line (or end of the text) such as ";"? Happy Holidays, and thanks again!!!! I barely got through parsing the ID. I think what I am asking is a bit harder. 
SKolakanSKolakan
One way to make parsing email body easy is to set rules and conform to it on calling side and parsing side. For example, for "Case Status", you can say it must be like
casestatus:XXXX:casestatus
in this case you just look for that pattern in the entire email body and parse anything in between casestatus: and :casestatus (We can take a cue from salesforce acitivity association using ref: :ref). That will eliminate many issues. Ofcourse, if you don't have control over the ticketing system that sends the email then it won't work.