0
@AuraEnabled
public static void sendEmail(Id recordId) {
    System.debug('sendEmail is called');
    // get the record data
    Invoice__c invoice = [Select Id,Name,Customer__r.Email__c,Invoice_Amount__c from Invoice__c where Id =: recordId];
    System.debug('invoice Email---- '+invoice);
    
    // set up the email
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    email.setToAddresses(new List<String>{ invoice.Customer__r.Email__c });
    email.setSubject('Invoice ' + invoice.Name);
    email.setPlainTextBody('Dear Customer,\n\nPlease find attached your invoice for $' + invoice.Invoice_Amount__c + '.\n\nThank you for your business.\n\nSincerely,\nMySat Pty Ltd');
    
    // attach the PDF to the email
    PageReference pdfPage = new PageReference('/apex/InvoicePDF?id=' + invoice.Id);
    pdfPage.getParameters().put('isEmail', 'true');
    Blob pdfBlob = pdfPage.getContentAsPDF();
    Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
    attachment.setFileName(invoice.Name + '.pdf');
    attachment.setBody(pdfBlob);
    email.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment });
    
    // send the email
    Messaging.SendEmailResult[] results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
    
    // check the results
    for (Messaging.SendEmailResult result : results) {
        if (!result.isSuccess()) {
            // handle error
            System.debug('Error sending email: ' + result.getErrors()[0].getMessage());
        }
    }
}

I wrote a test class for this class

@isTest
static void sendEmail(){
    Account acc = new Account();
    acc.Name = 'test account';
    acc.Email__c = '[email protected]';
    insert acc;

    Subscription__c sub = new Subscription__c();
    sub.Customer__c = acc.id;
    insert sub;

    Invoice__c inv = new Invoice__c();
    inv.Customer__c = acc.id;
    inv.Subscription__c = sub.id;
    insert inv;


    Test.startTest();
    // Set up a mock PDF body for testing
    Blob mockPdfBlob = Blob.valueOf('Mock PDF content');
    // Set up mock callout response
    Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator(mockPdfBlob));
    Test.setCurrentPageReference(new PageReference('/apex/InvoicePDF?id=' +inv.Id));
    Invoice__c invoice = [Select Id,Name,Customer__r.Email__c,Invoice_Amount__c from Invoice__c where Id =: inv.Id];
    // call the method
    try{
        invoicepdf.sendEmail(invoice.Id);
    }catch(Exception ex){
         
    }


    Test.stopTest();

}


private class MockHttpResponseGenerator implements HttpCalloutMock {
    private Blob responseBody;
    
    public MockHttpResponseGenerator(Blob responseBody) {
        this.responseBody = responseBody;
    }
    
    public HTTPResponse respond(HTTPRequest request) {
        HTTPResponse response = new HTTPResponse();
        response.setBodyAsBlob(responseBody);
        response.setStatusCode(200);
        return response;
    }
}

but it does not cover the email attachment part how to cover full method

0

You must log in to answer this question.

Browse other questions tagged .