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
kavya mareedukavya mareedu 

I want to write a program to send documents taking email template into consideration

Here is my program:

public class Outbound_Example_3 {
    public void invoke(){
        Messaging.SingleEmailMessage msg =new Messaging.SingleEmailMessage();
        Contact con=[select id ,AccountId from Contact where createdDate=TODAY];
        msg.setTargetObjectId(con.Id);
        msg.setWhatId(con.AccountId);
        EmailTemplate et =[select id from EmailTemplate where name='Case Response'];
        msg.setTemplateId(et.id);
        
        Document doc =[select id,Name,Body,ContentType ,Type from Document where name='Email Messaging'];
        Messaging.EmailFileAttachment eft1 =new Messaging.EmailFileAttachment();
        eft1.setFileName(doc.Name+doc.type);
        eft1.setBody(doc.body);
        eft1.setContentType(doc.ContentType);
        
        PageReference p=Page.EmailSerc;
        Blob body=p.getContentAsPDF();
        Messaging.EmailFileAttachment eft2 =new Messaging.EmailFileAttachment();
        eft2.setFileName('EmailSerc');
        eft2.setBody(body);
        List<Messaging.EmailFileAttachment> files =new List<Messaging.EmailFileAttachment>{eft1,eft2};
        msg.setFileAttachments(files);
        Messaging.Email[] emails =new Messaging.Email[]{msg};
        Messaging.sendEmail(emails);    
        
    }
}


Outbound_Example_3 ou=new Outbound_Example_3();
ou.invoke();


Please let me know where am I going wrong.
Thanks!
abhishek singh 497abhishek singh 497
Hello ,
Are you getting error while executing above class??

Thanks & Regards,
Abhishek Singh
kavya mareedukavya mareedu
Yes I am getting while debugging it. Error is: "List has no rows for assignment to sObject"!!                                                                                                      "
abhishek singh 497abhishek singh 497
This error generally comes when either of your queries is not returning any records.

Thanks & Regards,
Abhishek Singh.
kavya mareedukavya mareedu
May I know the reason for not returning any records?? I have created few as well. I did the preview as well. Everything looks fine. I don't know why things are not working in the code.