+ Start a Discussion
Bernd NawrathBernd Nawrath 

System.QueryException with test class

Hello dear community, I'm getting this exception for a query that I have in my class as well as in my test class when I'm trying to run the test class, how can I fix it? : 
System.QueryException: List has no rows for assignment to SObject
<span unselectable="on" "="" style="font-weight: normal; display: block; padding: 3px 4px; overflow: hidden; margin-left: 0px;">Class.HelperContactTrigger.sendEmail: line 6, column 1 
Class.HelperContactTriggerTestneu.myTestMethod: line 17, column 1

the classes:

public with sharing class HelperContactTrigger {
    //static method
    public static List<Account> sendEmail(List<Account> accounts) {

        //query on template object, is this correct,what might have been the mistake when I'm refering to EmailTemplate?
        EmailTemplate et=[Select id from EmailTemplate where name= 'Sales: New Customer Email' limit 1];

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

        for(Account con : accounts){
        //send mail

        return accounts;

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

        system.debug('### NewAccountTest ###');
        Account acc = new Account(Name = 'Test Test');
        insert acc;
                        // Here I'm refering to the test account that I have in my accounts, still this does not seem to work, how is that possible?
        List<Account> sendMail = [select id from account where (Name='Test Test') and id=:acc.id];

        System.assert(acc !=null);

thanks in advance! :)
Guy KeshetGuy Keshet
so the obvious answer is that your Soql is not returning any data.
I'd suspect that either your insert is failing (perhaps there's a validation rule preventing the record insertion?) or your query has an error

debug your test class and check if the insert statment suceeded. IF so - syste,.debug the id to insure you have it to hand.
if the nisert is failing - create a record manually and see what is missing, or run your insert as anonymous apex

last but not least- it could be a sharing issue - check if you have any sharing restrictions on the account object 

generally speaking, it's always good practise to check a list populated from soql is not empty to avoid lsit out of bounds errors
after inserting the Account, place a debug statement and comment that soql. like this you can debug it.