0

I'm building a test class for a controller that consists of a method for sending email. I need to provide test data for that email method. The test code is-

    List<String> listEmail = new List<String>();
    listEmail.add('[email protected]');
    listEmail.add('[email protected]');

    List<Messaging.EmailFileAttachment> efa = new List<Messaging.EmailFileAttachment>();
    efa.fileName='Unit Test Attachment'; //error is here
    efa.Body=Blob.valueOf('Unit Test Attachment Body'); //error is here

    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    email.ToAddresses = listEmail;
    email.PlainTextBody = 'Testing Email';
    email.FileAttachments = efa; //error is here

    Messaging.SendEmailResult[] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

When I assign efa to email.FileAttachments, I receive this error- Variable does not exist: fileName

And if i comment fileName assignment then this error- Variable does not exist: Body

Please guide me as to what are the correct attributes?

Thanks in advance!

1 Answer 1

2

you are trying to set the properties 'fileName' and 'body' to a List of EmailFileAttachment. You shoud instead create a single EmailFileAttachment variable and add it later to the List:

  List<Messaging.EmailFileAttachment> efa = new 
    List<Messaging.EmailFileAttachment>();  
    Messaging.EmailFileAttachment singleEfa = new Messaging.EmailFileAttachment();
    singleEfa.fileName='Unit Test Attachment';
    singleEfa.Body=Blob.valueOf('Unit Test Attachment Body');
    efa.add(singleEfa);

You could also put a loop and create multiple attachment in the same way. Hope this helped.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .