• elpaso751
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies

Hi guys,

I'm sorry to bother you again, but still finding myself is a sticky patch with Test classes.

 

We have an escalation policy for Cases which includes an integration with an external system. We kept the integration pretty easy, via email.

 

now, I've created a Class to handle the 'return' message from the external system which updates a field in the CASE:

 

/**
 * Email services are automated processes that use Apex classes
 * to process the contents, headers, and attachments of inbound
 * emails.
 */ 
    
global class processSIACASE implements Messaging.InboundEmailHandler {

CASE[] updateCase = new Case[0];

  // Create variables to handle incoming email  
    
 global Messaging.InboundEmailResult handleInboundEmail(

  Messaging.InboundEmail email, 
  Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = 
        new Messaging.InboundEmailresult();
   
 
  // Retrieves content from the email.  
  // Splits each line by the simbol # character  
      
string subject = email.subject;    

String[] emailBody = email.plainTextBody.split('#', 0);
String CRNumber = emailBody[1];
String case_number = emailBody[2];

   // RUN an SQL to find the ID of the case to update.

system.debug('value of CRNumber'+CRNumber);
system.debug('value of case_number'+case_number);

CASE cs;
cs = [select ID from CASE where CaseNumber =:case_number limit 1];
    
  // update the case from the information     
  // retrieved from the inbound email  

   try
    {
      updateCase.add(new CASE(ID = cs.id,
      Service_Provider_CR_number__c = CRNumber,
      SIA_Resolution_Description__c = subject));
 
      update updateCase;
   }
    
   catch (System.DmlException e)
   {
System.debug('ERROR: Not able to update CASE: ' + e);
   }
 
return result;
    }   
}

I've tested it sending emails from the external system and this works, and does the job as requested. 

 

Now I've created the Test class in order to deploy it in production:

 

@isTest
private class TestProcessSIACASE {

    static testMethod void myUnitTest() {
     
        Account acct= New Account(Name='Testing Account');
        Insert acct;
        
        Case cs=new Case(Subject='Test Case', 
         AccountId=acct.Id,
         origin='Project',
         Type='Latency', 
         Description='This is a test'); 
       insert cs;
       system.debug('Case entered has Number: '+cs.CaseNumber);
        
       //Test.StartTest(); 
        
           // Create a new email, envelope object and Attachment
	   Messaging.InboundEmail email = new Messaging.InboundEmail();
	   Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
	 
	   //email.subject = 'MTS: Call Request # 632561 # 50676 # opened';
	   email.plainTextBody = 'MTS: Call Request # 632561 # ' + cs.caseNumber +' # opened';
	   env.fromAddress = 'elpaso750@gmail.com';
	 
	   // call the class and test it with the data in the testMethod
	   ProcessSIACASE TestObj = new ProcessSIACASE();
	   TestObj.handleInboundEmail(email, env );                     
	  //Test.StopTest();
    }
}

 but I'm getting the following error :

 

System.QueryException: List has no rows for assignment to SObject

Class.processSIACASE.handleInboundEmail: line 38, column 6 Class.TestProcessSIACASE.myUnitTest: line 32, column 5 External entry point.

 

Now I put some system.debug to try and troubleshoot what was going wrong. I also did try and force the CASENUMBER to a CASE NUMBER I knew was there in the DB

 

email.plainTextBody = 'MTS: Call Request # 632561 # 50676 # opened';

 

but still get the same error :

 

23:47:49.970|SOQL_EXECUTE_BEGIN|[38]|Aggregations:0|select ID from CASE where CaseNumber =:case_number limit 1
23:47:49.977|SOQL_EXECUTE_END|[38]|Rows:0
23:47:49.977|EXCEPTION_THROWN|[38]|System.QueryException: List has no rows for assignment to SObject
23:47:49.977|METHOD_EXIT|[32]|processSIACASE.handleInboundEmail(Messaging.InboundEmail, Messaging.InboundEnvelope)
23:47:49.977|FATAL_ERROR|System.QueryException: List has no rows for assignment to SObject

 

It appears the Select is not returning any value.

 

 

 

Any suggestion would be highly appreciated.

 

Bye now,

Alex