In this post, will show you how to add task attachment using BPM java api. For demonstration, i am taking simple string for input stream and same can be extended to any file.
public void addAttachment(String taskId, String fileName) { IWorkflowContext wfCtx = null; try { String fileContents = "Using the sample string for attachment and this can be a file along with correct mime type"; InputStream is = null; //creating the attachmenttype AttachmentType attachmentType = new ObjectFactory().createAttachmentType(); //setting the attributes attachmentType.setTaskId(taskId); attachmentType.setName(fileName); attachmentType.setDescription("From BPM Java API"); attachmentType.setMimeType("text/plain"); //setting the input stream is = new ByteArrayInputStream(fileContents.getBytes()); attachmentType.setInputStream(is); //get the admin workflow context wfCtx = getAdminWorkflowContext(); //Task qryTask = getWfServiceClient().getTaskQueryService().getTaskDetailsById(wfCtx, taskId); //getWfServiceClient().getTaskService().addAttachment(wfCtx, qryTask, attachmentType); getWfServiceClient().getTaskService().addAttachment(wfCtx, taskId, attachmentType); } catch (Exception ex) { System.out.println("in addAttachment exception"); ex.printStackTrace(); } }
The commented lines in the above code is another way of achieving the same. Pass root task id or any child task id for this method call. We can verify this attachment in WFATTACHMENT table as shown below.
select * from WFATTACHMENT where taskid = ‘c14791b7-7e43-475d-bb9a-003ab05aaf36’
Observations:
- When roottaskid is used to add the attachments, the task form will show this attachment for all child tasks.
- addAttachment API is inserting an entry into WFATTCHMENT table even when there is no task id passed.
- The above behavior can be used in a scenario when we want to add task attachment task creation. To make this works, set the correlation id in attachment type like below and use the same correlation id during task creation.
attachmentType.setCorrelationId(correlationId);
- By default Admin, Approvers, Assignees, Reviewers, Owner and Creator can add attachments to a task which is configured in Access tab of human task as shown below. We will get an exception when unauthorized user trying to add the attachment as shown below.
<Nov 14, 2017, 2:45:36,118 PM IST> <Error> <oracle.soa.services.workflow.persistency> <BEA-000000> <<.> exception.code:30327
exception.type: ERROR
exception.severity: 2
exception.name: Current user does not have the ADD privilege on task attribute: ATTACHMENTS.
exception.description: Current user siva does not have ADD privilege for ATTACHMENTSon task with taskI
d b4ce32d1-d2e9-4f73-abc0-18c1447facf4.
exception.fix: Grant the ADD access for the current user/role. If the error persists, contact Oracle Support Services.