ApexSalesforce

Simple Project – Post 3 – Email Attachment

This is the first of short set of posts that will build in to a working example. The overall example will allow the user to select a document from a static resource and then send the static resource as an attachment on an email. This will demonstrate the following:

  • How to list the documents in a static resource
  • How to present a datatable to the user with a select to move the next screen of the flow
  • How to attach a specific static resource to an email and send

Post 1 covered getting the list of documents from the static resource and Post 2 showed how the user can select the document from the list.

We will setup a new apex class with an invocable action, receive in the document and then send an email with the document as an attachment.

We will need a few helper functions. So here is the getEmail:

private static Messaging.SingleEmailMessage getEmail() {
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

    try {
      Messaging.reserveSingleEmailCapacity(1);
    } catch (Exception e) {
      system.debug('----No reservation for sending of emails----');
      //handle your exception gracefully
    }

    String[] toAddresses = new List<String>{ 'email@gmail.com' };
    mail.setToAddresses(toAddresses);
    mail.setSubject('Email Title');
    mail.setUseSignature(false);
    mail.setPlainTextBody('emailPlainBody');
    mail.setHtmlBody('<h1>Body of Email</h1>');

    return mail;
  }

We now need the code to get an attachment in the right format. We are passing in the document and so we get the zipped file from the static resource as we saw in Part 1. The getFile comment on the Zippex object get the blob of the file inside the static resource. Then we can create a EmailFileAttachment object and set the name and body by passing the blob into the setBody method :

 private static Messaging.EmailFileAttachment getAttachment(string document) {
    List<StaticResource> staticResource = [
      SELECT id, name, body
      FROM StaticResource
      WHERE name = 'Attached_Documents'
    ];

    if (staticResource == null)
      return null;

    Blob staticBody = staticResource[0].body;

    //https://github.com/pdalcol/Zippex
    Zippex staticZip = new Zippex(staticBody);


    Blob fileData = staticZip.getFile(document);

    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
    efa.setFileName(document);
    efa.setBody(fileData);
    efa.setContentType('application/pdf');

    return efa;
  }

Our invocable method is fairly straightforward:

 @InvocableMethod
  public static List<string> doStaticSend(List<Requests> requests) {
    if (requests == null)
      return new List<String>{ 'Error' };

    String document = requests[0].sendDocument;

    Messaging.SingleEmailMessage mail = getEmail();
    Messaging.EmailFileAttachment attachment = getAttachment(document);

    mail.setFileAttachments(
      new List<Messaging.EmailFileAttachment>{ attachment }
    );

    List<Messaging.SendEmailResult> sendResults = Messaging.sendEmail(
      new List<Messaging.SingleEmailMessage>{ mail }
    );


    return new List<String>{ 'Success' };
  }

Leave a Reply

Your email address will not be published. Required fields are marked *