+ Start a Discussion
Krishna KatveKrishna Katve 

Sending email from rest service(method annotated with HTTPPost)

Hi,

I am trying to send email from rest resource with attachments from records notes and attachments.
But i am receiveing email with strange behaviour.

1. I cant open one of the email attachments
2. I can open one of the pdf's but it has content of previous one
3. FIle contents are gettinf swapped between files.

***** I tried by executing same piece of code in developer console. it worked well

Below is sample code with hard coded values
@RestResource(urlMapping='/sendPDFEmail2/*')
Global class TESTSENDEMAIL{
    @HttpPost
     global static void sendEmail(String pobjQuoteId, String emailTemplate) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        string [] toaddress = new List<String>{'krishna.katve@gmail.com'}; 
        mail.setToAddresses(toaddress);  
        mail.setTargetObjectId('005c0000001hn1i');
        mail.setWhatId('a35c000000042bA');
        mail.setTemplateId('00Xc0000000MdL2');
        mail.setSaveAsActivity(false);
             
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
          
        List<Attachment> aList  = [select Name, Body from Attachment where ParentId = :parentId limit 10];
        
        for(Attachment a : aList){    
            Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
            efa.setFileName(a.Name);
            efa.setBody(a.Body);
            fileAttachments.add(efa);                   
        }
            
        if(!fileAttachments.isEmpty()){   
            mail.setFileAttachments(fileAttachments);
        }
        Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
     }
}

Please advise.

Thanks
Krishna
Nagaveena A 5Nagaveena A 5
Hi,

In the line: List<Attachment> aList  = [select Name, Body from Attachment where ParentId = :parentId limit 10];
parentId value it's missing, please assign the id value directly or by querying - it works fine.

Thanks
Veena