You need to sign in to do that
Don't have an account?
Asking for help for Test Class
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
Hi,
Actually I figured out the reason the Select query does not return any record.
the problem was a blank space at the end of the string:
the instruction
string case_number = emailBody[2];
was returning the following value : case_number = '50690 ' end not '50690' and that was the reason no record was returned by the SELECT.
case_number = case_number.trim();
did solve the issue.
regards,
Alex
All Answers
It looks like the issue is here :
Right.
So the error was: "System.QueryException: List has no rows for assignment to SObject"
The list, in this case, is the result of the select.
You're doing a number of things here:
- making a query
- returning only one result from that query
- binding that to a variable.
The problem is (I think) the query is returning no results. So there is no case with whatever case number you're supplying.
So it can't create one result from the query
So it can't assign anything to the variable.
If you want o be careful you could so something like
Then check the length of lcs. If it's 0/null then obviously the case doesn't exist.
Hi,
Actually I figured out the reason the Select query does not return any record.
the problem was a blank space at the end of the string:
the instruction
string case_number = emailBody[2];
was returning the following value : case_number = '50690 ' end not '50690' and that was the reason no record was returned by the SELECT.
case_number = case_number.trim();
did solve the issue.
regards,
Alex
Cool. Ever since learning BASIC on a Sinclair Spectrum, I've always outputted values like this:
Instead of what you did, which was this:
It's enabled me to catch many more bugs, much faster :-) Spaces don't go unnoticed that way :-)
Anyway, glad all is good.