0

I am receiving an apex test error - the error message is:

System.QueryException: List has no rows for assignment to SObject Stack Trace: Class.BillingScheduleTest.TestBillingSchedule2: line 108, column 1

When I view the code in the developer console - line 108 code is the below:

Asset objAS = [Select Id,Contract__c,Name  From Asset Where Name= 'TestAsset' Limit 1];

The whole code can be found at: https://gist.github.com/JennSchnell/1fe9ab2fc2288ad5077baa15ae61ddfa

Still learning a lot of this stuff, but is the error telling me that I don't have an Asset with the name "TestAsset"?

3

is the error telling me that I don't have an Asset with the name "TestAsset"?

Precisely. The "List has no rows for assignment" means that you tried to assign the result of a query that returned no records to a variable.

If you get a "List has more than 1 row for assignment to SObject" error it would mean the opposite: you got more than one record in your query, and you are trying to assign it to a single instance in your code. In this case you need to either refine your query to get just one record or change your variable type to a list.

Edit:

So, after reading your code, some things come to my mind:

  1. On the first line you have seeAlldata=true in your class. This means that when the test is run, it will be able to read the data in your organization. No big deal, actually, but you might not need this, see below.
  2. Lines 3 to 81 are basically for setting up some test data (which would make the seeAllData=true useless.
  3. TestBillingSchedule1 and TestBillingSchedule2 are methods that will be using the test data created previously. If they are failing then you need to fix whatever is causing the error (in this case, the Asset is missing from the test data).
  4. If the AssetTriggerHandlerTest class is also failing, you might want to check it before, since it is testing a trigger. The trigger behaviour will likely affect other tests, like the first one you posted.
  5. See the introduction to test methods.
12
  • So if it is "List has no rows for assignment" - How do I fix the test class? I don't really need an asset in the database called TestAsset?
    – JennS
    Nov 10 '17 at 23:52
  • I don't know. It depends on what your test is testing. You can fix the class by making sure that the asset is created and inserted in the database. Nov 11 '17 at 0:16
  • Got it. So if it was created in the database (production) and an admin deleted the record, than that could result in this error?
    – JennS
    Nov 11 '17 at 0:29
  • 1
    @JennS records used in test methods should be created in test methods. Don’t use seealldata=true. Any records created during a test are rolled back. If you have not already visit the test method section of trailhead. Without seeing your test class it’s hard to be specific. Your test is most likely failing because you are relying on existing records rather than creating them in the tests
    – Eric
    Nov 11 '17 at 7:35
  • 1
    Here is the entire code: gist.github.com/JennSchnell/1fe9ab2fc2288ad5077baa15ae61ddfa
    – JennS
    Nov 14 '17 at 23:27
0

It’s best to create your own test data in test classes with code. That way your tests basically build your supplemental test context, test your functionality, and then the test ends and the system cleans up after you.

To fix your specific problem, make sure to create an asset named “TestAsset” in the test method or in a setup method in the test class.

Google Salesforce testing best practices and start from there to learn more about it

1
  • Rather than telling someone to google for a resource, you should link to it and cite a relevant snippet if one can concisely make your point. The whole point of this site is that people should be able to look here for a comprehensive answer. Also, search results change over time.
    – Adrian Larson
    Nov 15 '17 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.