You need to sign in to do that
Don't have an account?
Eric Delgado
Salesforce Apex unit testing error
I am trying to create a Salesforce unit test for a new trigger I created.
System.QueryException: List has more than 1 row for assignment to SObject
I am new to Apex and I have no idea how to fix this. Can someone give me some idea how to fix this? Thanks!
trigger SOSCreateCaseCustom on SOSSession (before insert) { List<Event> aplist = new List<Event>(); List<SOSSession> sosSess = Trigger.new; for (SOSSession s : sosSess) { try { Case caseToAdd = new Case(); caseToAdd.Subject = 'SOS Video Chat'; if (s.ContactId != null) { caseToAdd.ContactId = s.ContactId; } else { List<Contact> contactInfo = [SELECT Id from Contact WHERE Email = :s.AppVersion]; if (!contactInfo.isEmpty()) { caseToAdd.ContactId = contactInfo[0].Id; s.ContactId = contactInfo[0].Id; } } insert caseToAdd; s.CaseId = caseToAdd.Id; }catch(Exception e){} } }Here is my unit test:
@isTest private class SOSCreateCaseCustomTest { static testMethod void validateSOSCreateCase() { String caseSubject = 'SOS Video Chat'; // set up case to add SOSSession s = new SOSSession(); insert s; Case caseToAdd = new Case(Subject='SOS Video Chat'); caseToAdd.ContactId = s.ContactId; insert caseToAdd; Case ca = [SELECT Subject, ContactId from Case where Subject =: caseSubject]; // Test that escaltion trigger correctly escalate the question to a case System.assertEquals(s.ContactId, ca.ContactId); } }I keep getting this error:
System.QueryException: List has more than 1 row for assignment to SObject
I am new to Apex and I have no idea how to fix this. Can someone give me some idea how to fix this? Thanks!
Regards,
Mahesh
All Answers
Regards,
Mahesh
Here I am not sure onething is AppVersion, other than that everything looks good from my above code.
Regards,
Mahesh
You are awesome! Removing the insert Case did fix the problem. However, when I go through the deployment, the validation fails again with the same error.
Code Coverage Failure
Your organization's code coverage is 4%. You need at least 75% coverage to complete this deployment. Also, the following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
SOSCreateCaseCustom
It doesn't make sense because my triggers should definitely have more than 0% code coverage. Am I missing something?
As you are getting the Test Class error, it is not covering any of your Trigger code.
You Org Code coverage is only 4% which is stopping you.
Please provide all your deployment components along with code coverage from sandbox so that I can tell you why it is showing the 4%.
Regards,
Mahesh
I created this trigger by following this guide:
https://developer.salesforce.com/docs/atlas.en-us.196.0.sos.meta/sos/service_cloud_guides-auto_case_pop.htm
When I turn on the Code Coverage in force.com IDE, I see the test did cover the trigger as some of the lines in the code are highlighted. I believe that means it is covered.
Try this:
Regards,
Mahesh
I tried replacing my unit test with yours. But it throws exception:
System.AssertException: Assertion Failed: Expected: null, Actual: 0034B000005mTdVQAU
Class.SOSCreateCaseCustomTest.validateSOSCreateCase: line 18, column 1
By the way, I don't think its because the unit test is not correct. I believe there might be something that doesn't pick up the unit tests while deploying. I see the same 4% code coverage before adding this unit test, and there is no changes to the math after I added this new unit test.
And regarding the 4% code coverage, can you paste the components which you are deploying.
Regards,
Mahesh
This code works, and it bumps up the code coverage to 22% now. But I am still struggling with the deployment. This is the only code I added to the sandbox, but I wonder how to get my code coverage up to 75%. This is really a hedious process for deployment. Any tips on what is the right way to deploy sandbox to production? Preferably without the concern of code coverage unit test? Thanks again!
Code Coverage Failure
Your organization's code coverage is 21%. You need at least 75% coverage to complete this deployment. Also, the following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
SOSCreateCaseCustom
SOS Session Layout, SOS SessionPage Layout, SOSSession-SOS Session Layout
SOSCreateCaseCustom, Apex Trigger, SOSCreateCaseCustom
SOS_Open_Case_Custom, Visualforce Page, SOS_Open_Case_Custom
Regards,
Mahesh
Please create a new Outbound change set and try to add it.
Still if you are not able to see then, please paste the images to see.
Regards,
Mahesh
I did what you say and it is finally working. I also need to do it by choosing specific unit test to run in order to get it to work.