Wednesday, July 16, 2008

Add attachment to BPEL Human Task at creation

Consider the sample BPEL flow below, which contains a Human Task activity. It may be useful at times to have BPEL attach data to the task as an attachment file. How can this be done?




















We can do this by expanding the human task activity and providing some custom initialization code in the first, the AssignTaskAttributes, assignment section.




By adding four additional Copy directives to the existing assignment block, we can effectively cause the file attachment to be created:


<copy>
<from>
<attachment xmlns="http://xmlns.oracle.com/bpel/workflow/task">
<name/>
<mimeType/>
<content/>
</attachment>
</from>
<to variable="initiateTaskInput" part="payload"
query="/taskservice:initiateTask/task:task/task:attachment"/>
</copy>

<copy>
<from expression="string('text/plain')"/>
<to variable="initiateTaskInput" part="payload"
query="/taskservice:initiateTask/task:task/task:attachment[1]/task:mimeType"/>
</copy>

<copy>
<from expression="string('Test.txt')"/>
<to variable="initiateTaskInput" part="payload"
query="/taskservice:initiateTask/task:task/task:attachment[1]/task:name"/>
</copy>

<copy>
<from expression="string('Some string of text content here...')"/>
<to variable="initiateTaskInput" part="payload"
query="/taskservice:initiateTask/task:task/task:attachment[1]/task:content"/>
</copy>


The first copy block, creates a new attachment element and adds to the task. The next three copies initialize the properties of that attachment including the mime type, file name, and content.

In the above example we will have attached a simple text document with the name of Test.txt to the human task. Mime type can be any type desired, one of the more useful being application/octet-stream. Using that type I will show in my next article how to send a file attachment from a JSP/Servlet to the BPEL process and have it attached to the Human Task.

4 comments:

Klo Stein said...

Thanks your sample is useful, however when i execute my bpel with the code i get this fault:


java.lang.ArrayIndexOutOfBoundsException: 27

If you have any idea, please help me.

Thanks again.

Charles Piazza said...

Make sure you do it in the correct order...remember the first block is creating the attachment element.

Unknown said...

Nice sample. Thanks.

Klo:

Problem is in content.
Use:

from expression="ora:readFile('file:C:\Test.txt')"/

to variable="initiateTaskInput" part="payload" query="/taskservice:initiateTask/task:task/task:attachment[1]/task:content"/

Watch for file path!
This way you upload test file.

Rajesh A said...

Dear Charles,

Am I able to bring pdf attachment from BPEL human task activity ?? How can I achieve this, Please suggest me.

Thanks.