Saturday, December 12, 2015

IBM BPM - Send an Uploaded File as an Attachment in Email

In this post, let us look at how to write a file attached in IBM BPM (to an ECM ) on to the file system and send that file as an attachment in the email Notification.


 Below are the assumptions or Prerequisites for this illustration:
  • BPM uploads the document attachments to an ECM document store.
  • A folder already created and folder id is generated (Of course using the services available in Content management Toolkit !! ) in the ECM for a given application.
Below are the steps involved:
On a high level, this solution can be divided in three parts such that: a) Retrieve the file from content store (ECM), b)Write file to a file system, c) send the email by providing the file location to email service.

Let us look at this solution in details.



Step 1 : Identify the Document Id of the uploaded document from the IBM BPM document Store / External ECM server integrated with IBM BPM etc., which is being used for the content management. There are multiple way using which we can find the document id (which is out of scope for this article). Lets say we have identified the document id using one of the methods and it is "idd_D0B0DE74-D3AC-430E-80FF-6345EF2F715B".

Step 2: 

Using the above document id, we can get the content of the document using the outbox content integration component feature "get document content",  by passing the server name and document id. This service return an object of type ContentSream.

Get document Content





Inputs for Content integration


Sample output of the content Integration
 The variable tw.local.contentStream.content holds the content of the file which will be in a format  of Base64 String and encoded in UTF-8 format as described in this knowledge center link.

Step 3 :

Once we get the content from the above step, we need to convert this content String back to usable file Content. Below is code helps in doing the same.

Below code converts the file content string in to byte[] (byte array).
var byteValue = Packages.java.lang.String(tw.local.contentStream.content).getBytes();

 Below code, decodes the above Base64 byte array in to use format of byte[] (byte array)
var content64 = Packages.org.apache.commons.codec.binary.Base64.decodeBase64(byteValue);

Step 4: 
Once the file content is decoded and byte array retrieved, we can use the below script (bpm specific java code) to write the file on to a temp location of the file system.

            var fileName="/tmp/sample document.docx";
            var file = new Packages.java.io.File(fileName);
           

             // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            var fop = new Packages.java.io.FileOutputStream(file);
            fop.write(content64);


Below image shows the consolidated view of step 3 and step 4 in the server script.

Script from step 3 and step 4

Step 5:
once the file is created on the file system (either on the server on which bpm installed or some shared file location), we can use the out of mail service from System Data tool kit and provide the location of the file on the file system in order to send the email with the attachment as shown below.

Send Email Service


Below is email sample with the file attachment send from IBM BPM.

Sample email

* Please note that you need to delete the file written on the file system location once email is sent. This is important in order to avoid disk space issues due to piling up of unwanted files. 


Hope you find this useful !!



4 comments:

  1. Need the twx for email tolkit
    ksidkumar@gmail.com

    ReplyDelete
  2. Hi , I actually don't have twx file for this. But these are the exact steps on how you do that.

    ReplyDelete