0

I have a visualforce page with a custom controller. The custom controller fills in some information in the header of my datatable:

timesheetOwner = [select service_staff__r.name from service_documentation__c 
                    where service_staff__c = 'a23M0000001A6FjIAK' limit 1].service_staff__r.name;

In the anon window, the following code also works:

timesheetOwner = [select service_staff__r.name from service_documentation__c 
                   where service_staff__c = 'a23M0000001A6FjIAK' limit 1].service_staff__r.name;
system.debug('timesheetowner = '+timesheetowner);

This works fine on my VF page, but when I try to test and deploy to production, I get the error:

"List has no rows for assignment to Sobject"

In placing various debug lines in the code, sure enough, the variable shows as null.... however, when I take the SOQL select and place it in the query editor, it works... Very confusing...

Originally, the implicit service_staff__c filter used a variable and gave the same error. I then tried the select with the literal content and got the same error.

So in a nutshell:

  • I have a line of code that for all practical purposes... works
  • The line doesn't work when testing
  • The code won't deploy for the same reason

Any ideas?

Thanks, Marty

  • Is there any specific reason why you do not wish to use list of Service_documentation__c in your code? – IvanR Nov 10 '14 at 16:00
  • As for the reason you might not be getting the error in Anonymous context - depending on how your test classes are written, your tests might not have access to Org data or the target Org might not have the right data in the first place. – IvanR Nov 10 '14 at 16:02
  • 2
    Is the error occuring only when running tests? my guess would be that you have not specified SeeAllData=true in your tests and therefore do not have access to the Service_Documentation__c specified by the ID 'a23M0000001A6FjIAK' – Michael Fleming Nov 10 '14 at 16:04
  • 5
    Have you created the requisite test records? You should not be hardcoding ids in your class. That is not the best practice. As @MichaelFleming says SeeAllData= true might help if you have existing record with that id, In your test classes unless you dont specify SeeAllData=true, existing records are not taken into consideration. Its always best to create your test records in your test classes. For some reason you end up deleteing that record then the test class would fail next time you move any new code into prodn as all the test classes would be run before code is moved to prodn – Prady Nov 10 '14 at 16:36
  • Welcome to the world of SFDC. You have made a mistake of hardcoding ids in your test class. Something we have all done or have contemplated doing when we began with SFDC :) – Richard N Dec 15 '14 at 21:33
2

You should not hard code the ID value. When test cases are run, you should create test data. Since you do not have any while condition, all you have to do while creating test data is put values in the mandatory fields. Insert the record via code and use the ID from there itself. The query in Test class should refer to this variable that hold the ID and not a hardcoded value of ID. Alternatively, you could mention SeeAllData=true but it is not usually advisable.

Hope this helps!

1

OMG, hardcoded ids!!! My eyes!!! :-)

Don't do that.

Create a record before using it's id. Make variable static. Create it. Use it in test method.

  • To avoid this error I always... no... ALWAYS use lists, and then check it. – Andrii Muzychuk Nov 10 '14 at 18:13
  • 1
    Might be helpful to as well explain what he should be doing instead. – dphil Nov 10 '14 at 19:10
  • He mentions he originally used a variable but went to a ID presumably for debug. – Scott Nov 11 '14 at 4:25

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.