I have setup a task custom object. When the user changes the status of task from pending to completed, an email alert needs to be sent to the person with the attachment that is available in the 'notes and attachment' section on task level. I am calling the following apex class/function from process builder when the task status changes to complete to send an email alert with attachment.However, the code does not detect the file attached in notes and attachments section on tasks and throws error "An Apex error occurred: System.QueryException: List has no rows for assignment to SObject"
public class sendAnEmailWithCompletedForm
{
@InvocableMethod(label='Send an email from apex class'
description='sends an email')
public static void sendEmailWithAttachment(List<Id>
Requestor_s_Checklist)
{
Id Taskid= Requestor_s_Checklist[0];
Attachment a;
Attachment[] attachments = [select Id, Name, Body, BodyLength from Attachment where ParentId =: Requestor_s_Checklist];
if (attachments.size() > 0)
a = attachments[0];
Messaging.SingleEmailMessage semail= new Messaging.SingleEmailMessage();
Messaging.EmailFileAttachment attach= new Messaging.EmailFileAttachment();
attach.setBody(a.Body);
attach.setFileName(a.Name);
semail.setSubject('Review the completed form');
String[] emailIds= new String[]{'[email protected]'};
semail.setToAddresses(emailIds);
semail.setPlainTextBody('Please find the attached completed FormName');
semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
}
}