TransWikia.com

UNKNOWN_EXCEPTION, List has no rows

Salesforce Asked on November 30, 2021

I have the following error when trying to send an email.

System.EmailException: SendEmail failed. First exception on row 0; first >error: UNKNOWN_EXCEPTION, List has no rows for assignment to SObject: []

    List<User> users = [SELECT Name, Email, Id FROM User WHERE Recipient__c = TRUE and isActive = TRUE];
    EmailTemplate et = [SELECT id FROM EmailTemplate WHERE DeveloperName='Advisor'];

    for(User u : users) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

        mail.setTargetObjectId(u.Id);
        mail.setSenderDisplayName('Salesforce');
        mail.setUseSignature(false);
        mail.setBccSender(false);
        mail.setSaveAsActivity(false);
        mail.setTemplateId(et.id);

        Messaging.SendEmailResult[] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
    }

The list users contains at least one record and the Emailtemplate is not null.

This is the result of the mail system debug:

(Messaging.SingleEmailMessage[
getBccAddresses=null;
getCcAddresses=null;
getCharset=null;
getEntityAttachments=null;
getFileAttachments=null;
getHtmlBody=null;
getInReplyTo=null;
getOptOutPolicy=SEND;
getOrgWideEmailAddressId=null;
getPlainTextBody=null;
getReferences=null;
getTargetObjectId=005D0XXXXXXXXXuIAE;
getTemplateId=00XD0XXXXXXXXcbMAA;
getToAddresses=null;
getWhatId=null;
isTreatBodiesAsTemplate=false;
isTreatTargetObjectAsRecipient=true;
isUserMail=true; ])

2 Answers

It is a best practice to get the query results in a list. If a query does not return any rows we get List has no rows for assignment to sobject error. What I think is your code is failing because EmailTemplate query is not returning any rows. You can update code as below :

public static String ADVISOR = 'Advisor';
List<User> users = [
        SELECT Id
             , Email
             , Name 
          FROM User
         WHERE Recipient__c = TRUE
           AND isActive = TRUE
];

List<EmailTemplate> etList = [
        SELECT Id
          FROM EmailTemplate
         WHERE DeveloperName = :ADVISOR
];   

//Check if emailTemplate records exists, if so then only proceed with the implementation
if (!etList.isEmpty()) {
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    for (User u : users) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();    
        mail.setTargetObjectId(u.Id);
        mail.setSenderDisplayName('Salesforce');
        mail.setUseSignature(false);
        mail.setBccSender(false);
        mail.setSaveAsActivity(false);
        mail.setTemplateId(etList[0].id);//Make sure you have one record for email template where developer name is advisor or you would have to make this more dynamic    
        mails.add(mail);           
    }

    //As @Adrian Larson mentioned you only get 10 sendEmail invocations in a transaction,
    //it is a best practice to add mails in a list and invoke it once
    if (!mails.isEmpty()) {
        Messaging.SendEmailResult[] r = Messaging.sendEmail(mails);
    }
}

Answered by soniya raut on November 30, 2021

You code is passing empty list.

This is the correct code.

Main change is at this line:Messaging.SendEmailResult[] r = Messaging.sendEmail(mails);

And kept that out of for loop.

For more information, refer sendEmail()

List<User> users = [SELECT Name, Email, Id FROM User WHERE Recipient__c = TRUE and isActive = TRUE];
EmailTemplate et = [SELECT id FROM EmailTemplate WHERE DeveloperName='Advisor'];

List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();

    for(User u : users) 
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

        mail.setTargetObjectId(u.Id);
        mail.setSenderDisplayName('IAM Salesforce');
        mail.setUseSignature(false);
        mail.setBccSender(false);
        mail.setSaveAsActivity(false);
        mail.setTemplateId(et.id);
        mails.add(mail);
    }

    Messaging.SendEmailResult[] r = Messaging.sendEmail(mails);

Answered by Santanu Boral on November 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP