IBM BPM, V8.0.1, All platforms > Programming IBM BPM > Developing using the web service API > Samples
Completing a task
The following sample shows how to use the web API to enable a process participant to complete a task by checking it in.
For this sample to work, you must import the ExpenseApproval.twx file into Process Designer, start an instance of the Expense Approval Process BPD, and then complete the Create Expense Report task using a coach. The ExpenseApproval.twx file is located in the following directory: [BPM_install_directory]\BPM\Lombardi\web-api\samples\imports.
Sample
package teamworks.samples.scenarios; import teamworks.samples.client.ClientUtilities; import com.lombardisoftware.webapi.ComplexValue; import com.lombardisoftware.webapi.ExternalActivity; import com.lombardisoftware.webapi.ExternalActivityData; import com.lombardisoftware.webapi.Parameter; import com.lombardisoftware.webapi.SavedSearch; import com.lombardisoftware.webapi.Task; import com.lombardisoftware.webapi.Variable; import com.lombardisoftware.webapi.WebAPI; /** * This scenario illustrate how a task can be completed (checked in). In order * for this scenario to work, you must import the "Expense Approval" sample, * create (start) an instance, and complete the "Create Expense Report" task * using a Teamworks coach. * */ public class Checkin extends Scenario { public void testScenario() throws Exception { // Create a new WebAPI client stub WebAPI webAPI = getWebAPIFactory().newWebAPI(); // Retrieve the saved searches visible to this user SavedSearch[] savedSearches = webAPI.getSavedSearches(); // Find the Inbox saved search SavedSearch savedSearch = ClientUtilities.findByName( savedSearches, "Inbox"); // Get the tasks for the saved search Task[] tasks = webAPI.getTasksForSavedSearch(savedSearch.getId()); // Find a task for the "Approve or Reject Expense Report" activity // which is implemented using an external activity Task task = ClientUtilities.findByProperty( tasks, "activityName", "Approve or Reject Expense Report"); if (task == null) { System.out.println("No matching task found. Start a Expense Approval Process instance,"); System.out.println("and complete the Create Expense Report task before running this scenario."); return; } // Get the external activity definition. // This can be used to ensure that the inputs and outputs expected by // the client are the same as those provided by the external activity // used in the process. ExternalActivity externalActivity = task.getAttachedExternalActivity().getExternalActivity(); // Find the ExpenseReportIn input parameter Parameter inputParameter = ClientUtilities.findByName( externalActivity.getInputParameters(), "ExpenseReportIn"); System.out.println("Found ExpenseReportIn input parameter: " + inputParameter); // Find the DecisionOut output parameter Parameter outputParameter = ClientUtilities.findByName( externalActivity.getOutputParameters(), "DecisionOut"); System.out.println("Found DecisionOut output parameter: " + outputParameter); // TODO: Illustrate using custom properties // Retrieve the external activity data - this contains the actual variable values ExternalActivityData data = task.getAttachedExternalActivity().getData(); // Find the input variable for the expense report Variable inputVariable = ClientUtilities.findByName( data.getVariables(), "ExpenseReportIn"); // Get its value. Since this is a complex type, it must be // unwrapped and converted into a DOM element for use by the client String inputXml = ClientUtilities.toXMLString((ComplexValue) inputVariable.getValue()); System.out.println("Approve or Reject Expense Report:"); System.out.println(inputXml); // Normally, the user would perform some work in the UI to enter // their decision. Here we will do it directly. String outputXml = "<Decision xmlns=\"urn:teamworks.samples.expenseapproval\">" + "<Decision>Approved</Decision>" + "<Reason>This expense was within limits set by company policy</Reason>" + "</Decision>"; System.out.println(); System.out.println("Decision:"); System.out.println(outputXml); Variable outputVariable = new Variable("DecisionOut", ClientUtilities.toComplexValue(outputXml)); // Checkin (complete) the task webAPI.completeTask(task.getId(), new Variable[] {outputVariable}); } }