+ Start a Discussion
Bernd NawrathBernd Nawrath 

make a test class work

good evening dear community, when I'm trying to execute a test class it wont do it because it's telling me: 
System.QueryException: List has no rows for assignment to SObject

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

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

        List<Account> sendMail = [select id from account where id=:acc.id];


it belongs to this class :

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=:'Sales: New Customer Email'];

        //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;

I created these to automatically send an email when a new account has been inserted.
Any advices how to make it work at all? because otherwise i cant deploy my test class, thanks in advance!
Try using

The following will not return id unless you use SeeAllData
Select id from EmailTemplate where name=:'Sales: New Customer Email
Balaji Chowdary GarapatiBalaji Chowdary Garapati
@Bernd Nawrath, Good Evening,

  This might be the probable cause: In the actual class, you were querying the Email template which you havent inserted as test data in test class and also the testclass has no visiblity to the curent org data as you havent used (seeAllData=True). So try inserting an email template record from test class and re-run it.

James LoghryJames Loghry

No no no.  Do not use SeeAllData=True.  This is a horrible practice.  It looks like you're inserting the account properly.  However, is the error coming from your SOQL to fetch the EmailTemplate?  If so, double check that the EmailTemplate name is correct.  I also noticed a typo in your SOQL query:

[Select id from EmailTemplate where name=:'Sales: New Customer Email']

You have a colon before your string, which means it will try to bind the variable 'Sales: New Customer Email' instead of compare Name with the correct string.  Try removing the colon from your EmailTemplate SOQL query, and verify that your Name is correct, and see if your test passes then.
I agree with James.  SeeAllData=True should only be used when it is aboslutely need by objects that require it.  The EmailTemplate object is not one of these.