Skip to Content
Author's profile photo Piyas Kumar Das

How to get Task Details of a BPM Process using BPM API as a rest full service.

This guide provides instructions on how to get Task Details of a SAP BPM Process using BPM API as a Restful Service.

Applies to:

This Document Holds good for all CE 7.3 SP05 onward. This service can be called from UI5 Screen as an Ajax call.

Please note that from 7.31 SP09 onward you can also use the standard BPM OData service to retrieve task details and complete the task:Custom UIs with the BPM OData Service.

Special Thanks to Christian Loos  for commenting and mentioning Custom UIs with the BPM OData Service & Andre Backofen for the allowing me mention his blog in the document.


Summary:

This guide describes step-by-step how to get Task Details of a BPM Process using BPM API as a rest full service.


About Me:

Piyas Kumar Das

As a Sr. Netweaver Consultant, I’ve been undertaking consulting assignments leveraging on my undermentioned NetWeaver skills.

  • Business Process Management (SAP NW BPM)
  • Restful Services using BPM api to be used in UI5 Screens.
  • SAP Web Dynpro Java (SAP WD4J)
  • SAP Business Rules Management (SAP BRMS)
  • SAP Composit Application Framework (SAP CAF)
  • Master Data Management (SAP NW MDM)
  • Enterprise Portal (SAP EP)
  • Services creation using NWDS (SAP EJB)
  • Enterprise SOA


Prerequisites:

You should have read through the document : How to Start a BPM Process using BPM API as a RESTful service.


We will be discussing following points in detail in this document –


  1. Adding Libraries.
  2. Setting up the foundation for using Libraries.
  3. Creating Deploy-able Object.
  4. Accessing the methods exposed.
  5. Testing your REST services

Adding Libraries:


Step 1 : Refer the document to add libraries.


Setting up the foundation for using Library:


Step 1 to Step 4 is same as mentioned in the document.


Step 5 : Create a restful service class file and write a code as shown below or if you are continuing from the document then just copy past the methods only.


import java.io.StringReader;
import java.net.URI;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import bo.Material;
import bo.MaterialCreation;
import bo.TaskHeader;
import bo.TaskHeaders;
import com.sap.bpm.api.BPMFactory;
import com.sap.bpm.exception.api.BPMException;
import com.sap.bpm.pm.api.ProcessDefinition;
import com.sap.bpm.pm.api.ProcessDefinitionManager;
import com.sap.bpm.pm.api.ProcessStartEvent;
import com.sap.bpm.pm.api.ProcessStartManager;
import com.sap.bpm.tm.api.Status;
import com.sap.bpm.tm.api.TaskAbstract;
import com.sap.bpm.tm.api.TaskDetail;
import com.sap.bpm.tm.api.TaskInstanceManager;
import com.sap.tc.logging.Location;
import commonj.sdo.DataObject;
import commonj.sdo.helper.XMLHelper;
@Path("/MaterialCreationService")
@Produces({MediaType.APPLICATION_XML})
public class MaterialCreationToRestService {
  private static final Location location = Location.getLocation(MaterialCreationToRestService.class);
  private final String PRE_TASK_URI = "bpm://bpm.sap.com/task-instance/";
  @Path("/getTaskDetails/{task-id}")
  @GET
  @Consumes({MediaType.APPLICATION_JSON})
  @Produces({MediaType.APPLICATION_JSON})
  public Material getTaskDetails(@PathParam("task-id") String taskInstanceId) throws Exception
  {
  System.err.println("MaterialMaintenanceService -> getTaskDetails Task ID: "+taskInstanceId);
  taskInstanceId = PRE_TASK_URI + taskInstanceId;
  Material material = null;
  try
  {
  //Retrieve the TaskInstanceManager
  TaskInstanceManager taskInstanceManager = BPMFactory.getTaskInstanceManager();
  URI taskInstId = new URI(taskInstanceId);
  TaskDetail taskDetails = taskInstanceManager.getTaskDetail(taskInstId);
  String inputData = XMLHelper.INSTANCE.save(taskDetails.getInputDataObject(), "", "InputData");
  String outputData = XMLHelper.INSTANCE.save(taskDetails.getOutputDataObject(), "", "OutputData");
  System.err.println("InputData : "+inputData);
  System.err.println("OutputData : "+outputData);
  material = convertDocumentToMaterial(inputData);
  }
  catch (Exception e)
  {
  System.err.println(e);
  throw e;
  }
  return material;
  }
  private static void printNodes(Node node)
  {
  for (int i = 0; i < node.getChildNodes().getLength(); i++)
  {
  Node childNode = node.getChildNodes().item(i);
   
        if(childNode.getNodeType() == Node.ELEMENT_NODE)
        {
        if(hasChildNodes(childNode))
        {
        printNodes(childNode);
        }
        else
        {
        System.err.println("Key" + childNode.getNodeName());
        System.err.println("Value" +childNode.getTextContent());
        }
        }
  }
  }
  private static boolean hasChildNodes(Node node)
  {
  int nodeCount = 0;
  for (int i = 0; i < node.getChildNodes().getLength(); i++)
  {
  Node childNode = node.getChildNodes().item(i);
  if(childNode.getNodeType() == Node.ELEMENT_NODE)
  {
  nodeCount++;
  }
  }
  if(nodeCount > 0)
  return true;
  else
  return false;
  }
  private Material convertDocumentToMaterial(String inputData)
  {
  Material material = new Material();
  try
  {
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  Document document = db.parse(new InputSource(new StringReader(inputData)));
  document.getDocumentElement().normalize();
  Node pNode = document.getChildNodes().item(0);
  Element pElement = (Element) pNode;
  //Set material data
  material.setMaterial(pElement.getElementsByTagName("ns1:Material").item(0).getTextContent());
  material.setIndustrySector(pElement.getElementsByTagName("ns1:IndustrySector").item(0).getTextContent());
  material.setMaterialType(pElement.getElementsByTagName("ns1:MaterialType").item(0).getTextContent());
  material.setDescription(pElement.getElementsByTagName("ns1:Description").item(0).getTextContent());
  }catch (Exception e)
  {
  System.err.println(e);
  }
  return material;
  }
}



  • The Following code is very important as the key value printed in the logs is the key to be used while writing into the “convertDocumentToMaterial” method./wp-content/uploads/2014/03/1_407568.jpg/wp-content/uploads/2014/03/1_407568.jpg

Step 6 & Step 7 is same as mentioned in the document. If you are continuing then you can ignore these steps.


Creating Deploy-able Object & Accessing the methods exposed.


Refer the document to “Creating Deploy-able Object” & “Accessing the methods exposed” as it is the same.


Testing your REST services

You can test the created REST services using the POSTMAN for Chrome or Advanced REST client for Chrome.

/wp-content/uploads/2014/03/1_407568.jpg

Assigned Tags

      11 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Kumar Saurav
      Kumar Saurav

      Hi Piyas,

      Nice Article.

      -Kumar Saurav.

      Author's profile photo Piyas Kumar Das
      Piyas Kumar Das
      Blog Post Author

      Hi Kumar,

      Thanks for the appreciation.

      Cheers

      Piyas

      Author's profile photo Former Member
      Former Member

      Hi Piyas,

      Nice document keep up the good work

      Regards

      Samudra

      Author's profile photo Christian Loos
      Christian Loos

      Hi Piyas,

      Thanks for the document

      Please note that from 7.31 SP09 onwards you can also use the standard BPM OData service to retrieve task details and complete the task: Custom UIs with the BPM OData Service

      Regards,

      Christian

      Author's profile photo Piyas Kumar Das
      Piyas Kumar Das
      Blog Post Author

      Hi Cristian,

      I had a look @ the blog its good.

      I think i should mention the blog in my document.

      But i need to take the confirmation of Ander Backofen before I can mention his blog in my document.

      Cheers

      Piyas

      Author's profile photo Andre Hofeditz
      Andre Hofeditz

      Hi Piyas,

      Confirmed. 🙂

      Good Document by the way. It's a good help if you cannot use the OData Service.

      Best Regards,

      Andre

      Author's profile photo Piyas Kumar Das
      Piyas Kumar Das
      Blog Post Author

      Hi Ander,

      Thanks for confirming i will put the link in my documents...

      Cheers 😉

      Piyas

      Author's profile photo Former Member
      Former Member

      Hi Piyas,

      Nice Blog I tried it on my BPM process and it worked great.

      I have a small query if I have a list type then how do I read it?

      Regards

      Kumar

      Author's profile photo Piyas Kumar Das
      Piyas Kumar Das
      Blog Post Author

      Hi Kumar,

      Thanks for the appreciation.

      If you wish to read a list then it can be done easily.

      Read it from the child node.

      Cheers

      Piyas

      Author's profile photo Former Member
      Former Member

      nice document

      Anu

      Author's profile photo Thomas Sun
      Thomas Sun

      Hi Kumar,

      I know it's been a long time for this document, but I face a problem which hopes you may help.

      Each day I call "taskInstanceManager.getTaskDetail(taskInstId)" at first time, it takes about 9 min.

      After first time, the rest of tasks would be fine and even no issues at all.

      Would you have any idea that I should config but I didn't?

       

      Thanks in advance

       

      Allen