Creating a new process instance

The following sample shows how to use the Web API to create a new process instance.

For this sample to work, you must import ExpenseApproval.zip into Process Designer and then enable the 'Expose to start' option for the Expense Approval Process BPD and select the participant group whose members can start instances of this process. The ExpenseApproval.zip file is located in install_root/BPM/Lombardi/web-api/samples..
package teamworks.samples.scenarios;

import teamworks.samples.client.ClientUtilities;

import com.lombardisoftware.webapi.Favorite;
import com.lombardisoftware.webapi.Process;
import com.lombardisoftware.webapi.ProcessInstance;
import com.lombardisoftware.webapi.Task;
import com.lombardisoftware.webapi.WebAPI;

/**
 * This scenario illustrates creating a new process instance.  In order for this
 * scenario to work, you must import the ExpenseApproval.zip sample and create
 * a favorite for the "Expense Approval Process" process via the Teamworks administration
 * console.
 *
 */
public class CreateNewProcessInstance extends Scenario {

    /**
     * Scenario 1: Create a new process instance using a favorite
     */
    public void testScenario1() throws Exception {
        // Create a new WebAPI client stub
        WebAPI webAPI = getWebAPIFactory().newWebAPI();

        // Favorites are created using the Teamworks administration console
        // are used to give permissions to groups of users to start a process
        Favorite[] favorites = webAPI.getFavorites();
        
        // Find the favorite for Expense Approval
        Favorite favorite = ClientUtilities.findByName(favorites, "Expense Approval Process");
        
        if (favorite == null) {
            System.out.println("This scenario requires that you create a favorite for the " +
                    "Expense Approval Process using the Lombardi administration console.");
            return;
        }
  
        // Run the favorite.  This creates the process instance
        // and can return a task -- but only if the task activity is configured
        // to send the task to "Last User in Lane"
        Task task = webAPI.runFavorite(favorite.getId());
        
        // The Expense Report process meets these criteria, so task will be non-null
        ProcessInstance processInstance = task.getProcessInstance();
        
        System.out.println("Created new process instance using RunFavorite with id=" + processInstance.getId());
    }

    /**
     * Scenario 2: Create a new process instance using the system id of the process
     */
    public void testScenario2() throws Exception {
        // Create a new WebAPI client stub
        WebAPI webAPI = getWebAPIFactory().newWebAPI();

        // Retrieve the process by using its system identifier, which is visible in the 
        // Teamworks authoring environment for library items as the System ID property
        // when the Teamworks Advanced Features -> Public API capability is enabled.
        Process process = webAPI.getProcessBySystemId("Change Me", "guid:5347919854665fa8:-1e39686f:112e7f4c825:-7f51");
        
        // Create (start) this process instance which requires no inputs 
        ProcessInstance processInstance = webAPI.startProcess(process, null);
        
        System.out.println("Created new process instance using StartProcess with id=" + processInstance.getId());
    }
}