+ Start a Discussion
Faizan Ali 24Faizan Ali 24 

List has no rows for assignment to sObject error?

I have browsed forums for this issue and sadly it isn't helping much due to my basic knowledge of Apex.

I keep getting: List has no rows for assignment to SObject
 
public class SendEmailButtonController {
    
    private ApexPages.StandardController standardController;
    
    public SendEmailButtonController(ApexPages.StandardController acon){
        this.standardController = standardController;
    }
    
    public static void sendEmail(){
        
        List<String> toAddresses = new List<String>();
        
        Class_Register__c cont = [SELECT Id, Contact__r.Email, Contact__r.Id, Class__r.Name, Class__r.Event_Sub_Type__c FROM Class_Register__c];

        Class__c contClass     = [SELECT Id, Name, Contact__c, Contact__r.Id, Event_Sub_Type__c FROM Class__c];

        EmailTemplate[] et     = [SELECT Id, DeveloperName, FolderId, Body, IsActive FROM EmailTemplate WHERE FolderId = '00D3L0000008l4sUAA' LIMIT 1];

        Contact contacts       = [SELECT Id, FirstName, LastName, Name FROM Contact WHERE Id=: contClass.Id];
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        toAddresses.add(cont.Contact__r.Email);
                
        mail.setUseSignature(false);
        mail.setSaveAsActivity(false);
        
        String subType = contClass.Event_Sub_Type__c;
        
        switch on subType {
            when 'Webinar - Tools & Techniques' {
                mail.setTemplateId('00X3L000000MPbIUAW');
                mail.setTargetObjectId(contacts.Id);
                mail.setWhatId(cont.Id);
            }
            when else {
                System.debug('no value match');
            }
        }
        mail.toAddresses = new String[]{cont.Contact__r.Email};
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});        
    }
}

 
Best Answer chosen by Faizan Ali 24
Vishwajeet kumarVishwajeet kumar
Hello,
Ideally, a object should be queried with LIMIT 1 if we expect only one value from queried data and In Case where we are not sure we should use List and use index to get values.
List can be always preferred to avoid null data errors.

Below is updated Code, try out : 

public static void sendEmail(){
List<String> toAddresses = new List<String>();
List<Class_Register__c> cont = [SELECT Id, Contact__r.Email, Contact__r.Id, Class__r.Name, Class__r.Event_Sub_Type__c FROM Class_Register__c];

List<Class__c> contClass = [SELECT Id, Name, Contact__c, Contact__r.Id, Event_Sub_Type__c FROM Class__c];

EmailTemplate[] et = [SELECT Id, DeveloperName, FolderId, Body, IsActive FROM EmailTemplate WHERE FolderId = '00D3L0000008l4sUAA' LIMIT 1];

if(contClass != Null && et != Null && cont  != Null && contClass .size() > 0 && et.size()  > 0 && cont.size() > 0){
    Contact contacts = [SELECT Id, FirstName, LastName, Name FROM Contact WHERE Id=: contClass[0].Id];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); toAddresses.add(cont[0].Contact__r.Email); mail.setUseSignature(false);
mail.setSaveAsActivity(false);
String subType = contClass.Event_Sub_Type__c;
switch on subType {
when 'Webinar - Tools & Techniques' {
mail.setTemplateId('00X3L000000MPbIUAW');
mail.setTargetObjectId(contacts.Id);
mail.setWhatId(cont[0].Id);
} when else { System.debug('no value match'); } }

mail.toAddresses = new String[]{cont[0].Contact__r.Email};
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
}

Thanks.

All Answers

Vishwajeet kumarVishwajeet kumar
Hello,
Ideally, a object should be queried with LIMIT 1 if we expect only one value from queried data and In Case where we are not sure we should use List and use index to get values.
List can be always preferred to avoid null data errors.

Below is updated Code, try out : 

public static void sendEmail(){
List<String> toAddresses = new List<String>();
List<Class_Register__c> cont = [SELECT Id, Contact__r.Email, Contact__r.Id, Class__r.Name, Class__r.Event_Sub_Type__c FROM Class_Register__c];

List<Class__c> contClass = [SELECT Id, Name, Contact__c, Contact__r.Id, Event_Sub_Type__c FROM Class__c];

EmailTemplate[] et = [SELECT Id, DeveloperName, FolderId, Body, IsActive FROM EmailTemplate WHERE FolderId = '00D3L0000008l4sUAA' LIMIT 1];

if(contClass != Null && et != Null && cont  != Null && contClass .size() > 0 && et.size()  > 0 && cont.size() > 0){
    Contact contacts = [SELECT Id, FirstName, LastName, Name FROM Contact WHERE Id=: contClass[0].Id];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); toAddresses.add(cont[0].Contact__r.Email); mail.setUseSignature(false);
mail.setSaveAsActivity(false);
String subType = contClass.Event_Sub_Type__c;
switch on subType {
when 'Webinar - Tools & Techniques' {
mail.setTemplateId('00X3L000000MPbIUAW');
mail.setTargetObjectId(contacts.Id);
mail.setWhatId(cont[0].Id);
} when else { System.debug('no value match'); } }

mail.toAddresses = new String[]{cont[0].Contact__r.Email};
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
}

Thanks.
This was selected as the best answer
Faizan Ali 24Faizan Ali 24
Hi Vishwajeet, thank you very much for this. I have tried to update code but I keep getting the error that 'Variable does not exist: Event_Syb_Type__c'

This is on the line - String subType = contClass.Event_Sub_Type__c;
 
Why is this the case?
Vishwajeet kumarVishwajeet kumar
Hello,
Yes, that line should use variable index too.

try : 
String subType = contClass[0].Event_Sub_Type__c;


Thanks.