0

I have below code to send multiple email attachments in a single email which is throwing below error. Can some one please tell me what's wrong here?

Code Snippet

Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
       Messaging.EmailFileAttachment efa1 = new Messaging.EmailFileAttachment();
        if(getSelectedSize() > 0)
        { if(recordtypes.contains('ABS_Compliance_Incidents_abv'))
            {
            PageReference pdf = Page.CompIncidentTransactionsAttachment;
            pdf.getParameters().put('Ids',Ids ); //
            pdf.setRedirect(true);//false

            Blob b = pdf.getContent();


            efa.setFileName('ComplianceIncidentReport.pdf');
            efa.setBody(b); 
            }                  
            //email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});        

        if(recordtypes.contains('Sample_Compliance_Incidents_abv'))  
           {
           PageReference pdf1 = Page.CompIncidentTransactionsAttachmentSample;
            pdf1.getParameters().put('Ids',Ids ); //
            pdf1.setRedirect(true);//false

            Blob b1 = pdf1.getContent();
           // Messaging.EmailFileAttachment efa1 = new Messaging.EmailFileAttachment();

            efa1.setFileName('SampleIncidentReport.pdf');
            efa1.setBody(b1);  
           }    
           Integer interSample=[select count() from Compliance_Incident_abv__c where Id =:Ids1 and Recordtype.developername='Sample_Compliance_Incidents_abv'];
           Integer interABS=[select count() from Compliance_Incident_abv__c where Id =:Ids1 and Recordtype.developername='ABS_Compliance_Incidents_abv'];
            if(interSample==0)
             {email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});}         
           else if(interABS==0)
               {email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa1});}*/

           else {
           email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa,efa1}); 
            }

        }

        // Sets the paramaters of the email
        email.setSubject(Subject);

        email.setToAddresses( toAddresses );
        if(ccAddresses.size() > 0 && ccAddresses[0] != null && ccAddresses[0] != '') 
        {
            //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, toAddresses + ' ' + ccAddresses + ccAddresses.size() + toAddresses.size()));
            email.setCCAddresses( ccAddresses );
        }

        email.setPlainTextBody( Body);

        Messaging.SendEmailResult [] r = 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
        sendEmail=true;//Control viz of Send btn.
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, 'Your Email has been successfully Sent'));

Error

System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, No body supplied for the file attachment.: [fileAttachments]

0
1

There can be one reason: 1. Record Types are not matching in if. Try below sample code.

   List<Messaging.EmailFileAttachment> efaList = new List<Messaging.EmailFileAttachment>();

    if(getSelectedSize() > 0)
    { 
       if(recordtypes.contains('ABS_Compliance_Incidents_abv'))
       {
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();

        PageReference pdf = Page.CompIncidentTransactionsAttachment;
        pdf.getParameters().put('Ids',Ids ); //
        pdf.setRedirect(true);//false

        Blob b = pdf.getContent();


        efa.setFileName('ComplianceIncidentReport.pdf');
        efa.setBody(b); 
        efaList.add(efa);
        Integer interABS=[select count() from Compliance_Incident_abv__c where Id =:Ids1 and Recordtype.developername='ABS_Compliance_Incidents_abv'];

        if(interABS==0)
        {
           email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
        }

       }                        

       if(recordtypes.contains('Sample_Compliance_Incidents_abv'))  
       {
        PageReference pdf1 = Page.CompIncidentTransactionsAttachmentSample;
        pdf1.getParameters().put('Ids',Ids ); //
        pdf1.setRedirect(true);//false

        Blob b1 = pdf1.getContent();

        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();

        efa.setFileName('SampleIncidentReport.pdf');
        efa.setBody(b1);
        Integer interSample=[select count() from Compliance_Incident_abv__c where Id =:Ids1 and Recordtype.developername='Sample_Compliance_Incidents_abv'];
        if(interSample==0)
       {
           email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

        }  
       }    
    }

    // Sets the paramaters of the email
    email.setSubject(Subject);

    email.setToAddresses( toAddresses );
    if(ccAddresses.size() > 0 && ccAddresses[0] != null && ccAddresses[0] != '') 
    {
        //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, toAddresses + ' ' + ccAddresses + ccAddresses.size() + toAddresses.size()));
        email.setCCAddresses( ccAddresses );
    }

    email.setPlainTextBody( Body);

    Messaging.SendEmailResult [] r = 
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
    sendEmail=true;//Control viz of Send btn.
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, 'Your Email has been successfully Sent'));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.