You need to sign in to do that
Don't have an account?
Bernd Nawrath
Getting a test class to work: System.QueryException!
Hello dear community, I'm having a very difficult time getting this test class working, whenever I try executing it no matter what I did 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 :)
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 :)
EmailTemplate et=[Select id from EmailTemplate where name='Neuer Kunde'];
I also suggest adding a limit 1 to this query like this:
EmailTemplate et=[Select id from EmailTemplate where name='Neuer Kunde' limit 1];
If you assign a List to the results of a SOQL query and no values are found you will get an empty List not an error. So the line:
List<Account> sendMail = [select id from account where (Name='Test Test') and id=:acc.id];
will always return an instance of a List, but it will be size() == 0 if no Account records are found.