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 

System.QueryException: List has no rows for assignment to SObject

Hello dear community, I'm having a very difficult time getting this test class working, whenever I try executing it no matter what I tried 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:

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

        HelperContactTrigger.sendEmail(sendMail);
        test.stopTest();
        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>();

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

Thanks in advance.
 
Balaji Chowdary GarapatiBalaji Chowdary Garapati
@Bernd Nawrath:

Try this:

@isTest
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];
   EmailTemplate testTemplate=new EmailTemplate(Name='Neuer Kunde',DeveloperName='Neuer_Kunde',Body=blob.value('test email template'),FolderId=Userinfo.getUserId());
insert testTemplate;

        HelperContactTrigger.sendEmail(sendMail);
        System.assert(acc !=null);
        
        }
        }


Hope this helps,

 if it is failing, please share the debug log, so that we can see if the account creation itsself is failing due to any validations!