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 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? : 
myTestMethod
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>();

        //loop
        for(Account con : accounts){
        .............
        //send mail
        Messaging.sendEmail(emails);

        return accounts;
    }
}

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

        HelperContactTrigger.sendEmail(sendMail);
        test.stopTest();
        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
sadasiva07sadasiva07
after inserting the Account, place a debug statement and comment that soql. like this you can debug it.