Reading business data from a process instance

The following sample shows how to use the web API to read business data from a process instance.

See the "completing a task" sample to learn how to access data for an instance of an activity (task).

For this sample to work, you must import CustomerService.zip into Process Designer and then start a few instances of the Customer Service Process BPD. You can find the CustomerService.zip file in the following directory: install_root/BPM/Lombardi/web-api/samples.

package teamworks.samples.scenarios;

import com.lombardisoftware.webapi.BusinessData;
import com.lombardisoftware.webapi.ProcessInstanceStatus;
import com.lombardisoftware.webapi.Search;
import com.lombardisoftware.webapi.SearchColumn;
import com.lombardisoftware.webapi.SearchCondition;
import com.lombardisoftware.webapi.SearchOperator;
import com.lombardisoftware.webapi.SearchResults;
import com.lombardisoftware.webapi.Variable;
import com.lombardisoftware.webapi.WebAPI;

/**
 * This scenario illustrates how to read business data from a process instance.
 * See the Checkin scenario for an example of accessing data for an activity
 * instance (task).   This scenario requires importing the CustomerService.zip 
 * sample process and creating a few instances of that process.
 */
public class ReadProcessInstanceBusinessData extends Scenario {
    
    /**
     * Scenario 1: Retrieve a flattened complex type from the 
     * top-level process context of a process instance
     */
    public void testScenario1() throws Exception {
        // Create a new WebAPI client stub
        WebAPI webAPI = getWebAPIFactory().newWebAPI();
        
        // Find an active process instance that is visible to the user
        SearchResults results = webAPI.executeSearch(buildCustomerServiceInstancesSearch(), 1, null);
        if (results.getRows().length > 0) {
            long processInstanceId = (Long) results.getRows()[0].getValues()[0];
            
            // Find the customer object defined in the top level process
            BusinessData[] contexts = webAPI.getProcessInstancesBusinessData(new long[]{processInstanceId});
            
            System.out.println("Found " + contexts.length + " matching contexts");            
            for(BusinessData context : contexts) {         
                for(Variable variable : context.getVariables()) {
                    System.out.println(variable.getName() + "=" + variable.getValue());
                }
            }
        }
        else {
            System.out.println("No matching process instance found");
        }
    }
    
    private Search buildCustomerServiceInstancesSearch() {
        Search search = new Search();
        search.setOrganizedByType("ProcessInstance");
        search.setColumns(new SearchColumn[] {
                new SearchColumn("ProcessInstance", "Id", null)
        });
        search.setConditions(new SearchCondition[] {
                new SearchCondition(
                        new SearchColumn("Process", "Name", null),
                        SearchOperator._EQUALS,
                        "Customer Service Process"
                        ),                
                new SearchCondition(
                        new SearchColumn("ProcessInstance", "Status", null),
                        SearchOperator._EQUALS,
                        ProcessInstanceStatus._Active
                        )                
        });        
        return search;
    }
}