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 

Debug statement for test class

Hello dear community, I was trying to create a class with a trigger so send mails autimatically when a new account is inserted. 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: 

myTestMethod
System.QueryException: List has no rows for assignment to SObject
Class.HelperContactTrigger.sendEmail: line 6, column 1 
Class.HelperContactTriggerTestneu.myTestMethod: line 17, column 1

I know it seems like there are no records found that should be returned. So how exactly is my debug statement supposed to look like to check if the account is really inserted?

These are 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
        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! :)
PratikPratik (Salesforce Developers) 
Hi Bernd,

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

system.debug(et);   //debug to check the template


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

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

        return accounts;
    }
}



Also I am not sure where you are fetching the records in this class, however you can add  system.debug(list.size() )  to check the list size i.e. if there are records into it or not.


Hope this will help you.

Thanks,
Pratik