function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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

@isTest
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];
        test.startTest();

        HelperContactTrigger.sendEmail(sendMail);
        test.stopTest();
        
        }
        }
        }

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>();

        //loop
        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
                singleMail.setTargetObjectId(con.Id);

                //set template Id
                singleMail.setTemplateId(et.Id);

                //flag to false to stop inserting activity history
                singleMail.setSaveAsActivity(false);

                //add mail
                emails.add(singleMail);
            }
        }

        //send mail
        Messaging.sendEmail(emails);

        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!
BalajiRanganathanBalajiRanganathan
Try using
@isTest(SeeAllData=true)

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.

Thanks,
balaji
 
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.
pconpcon
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.