+ Start a Discussion
Bernd NawrathBernd Nawrath 

Getting a test class to work: System.QueryException!

Hello dear community, I'm having a very difficult time getting this test class working, whenever I try executing it no matter what I did it's giving me this error: System.QueryException: List has no rows for assignment to SObject. 
I already looked up in my account for the test name which is mentioned in the query, I found it but it still since I get this error it doesn't seem to return anything. When I look up in account, under accountname,am I supposed to pick the name for my test there and use it in my test code or what else do I need to put in exactly?
Please have a look on the classes:

private class HelperContactTriggerTestneu {
public static testMethod void myTestMethod() {

        Account acc = new Account(Name = 'Test Test');
        insert acc;

        List<Account> sendMail = [select id from account where (Name='Test Test') and id=:acc.id];

        System.assert(acc !=null);
public with sharing class HelperContactTrigger {
    //static method
    public static List<Account> sendEmail(List<Account> accounts) {

        //query on template object
        EmailTemplate et=[Select id from EmailTemplate where name='Neuer Kunde'];

        //list of emails
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();

        for(Account con : accounts){

            //check for Account
            if(con.PersonEmail == null && con.PersonEmail != null){

                //initiallize messaging method
                Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();

                //set object Id

                //set template Id

                //flag to false to stop inserting activity history

                //add mail
        //send mail

        return accounts;

Thanks in advance :)
I saw you have two queries (Account query and EmailTemplate query), so the "List has no rows for assignment to SObject" error is for Account query? Could you check the debug logs and see whether there is any error during inserting the Account? You can also print more debug logs and see what is going on.
Hi!  The error List has no rows for assignment to SObject will occur if you do a SOQL select into a variable but no records are found.  The error is likely occurring at this line:

 EmailTemplate et=[Select id from EmailTemplate where name='Neuer Kunde'];

I also suggest adding a limit 1 to this query like this:

 EmailTemplate et=[Select id from EmailTemplate where name='Neuer Kunde' limit 1];

If you assign a List to the results of a SOQL query and no values are found you will get an empty List not an error.  So the line:

List<Account> sendMail = [select id from account where (Name='Test Test') and id=:acc.id];

will always return an instance of a List, but it will be size() == 0 if no Account records are found.