Interacting with cases and activities from processes or service flows

 V20.0.0.2 
You can interact with cases and case activities from processes or service flows by using a script that contains JavaScript methods to call case APIs. This enables you to work with the values of case properties, such as multivalued properties or business objects.

This topic contains the following examples:

  • A script that interacts with case and activity property values.
  • Scripts that create a new case and initialize the values.

These examples are provided in the following sections.

Example of a script that interacts with case and activity property values

The example script is used in a process that implements a case activity. The script includes methods that call the parentCase JavaScript API to perform the following operations:

  1. Obtain a value of an activity property.
  2. Obtain a value for a case property.
  3. Calculate a new value for a case property based on the value of the retrieved activity property.
  4. Set a new value for the case property.

The example script is shown in the following code fragment:

// Obtain the deductible amount from the parent activity:
var interestingActivityPropertyNames = new tw.object.listOf.toolkit.TWSYS.String();
interestingActivityPropertyNames[0] = "S1_Deductible";
var interestingActivityPropertyValues = tw.system.currentProcessInstance.parentCase.getParentActivityPropertyValues(interestingActivityPropertyNames);
var deductible = parseInt(interestingActivityPropertyValues[0]);

// Obtain the payout amount from the parent case:
var interestingCasePropertyNames = new tw.object.listOf.toolkit.TWSYS.String();
interestingCasePropertyNames[0] = "S1_Payout";
var interestingCasePropertyValues = tw.system.currentProcessInstance.parentCase.getCasePropertyValues(interestingCasePropertyNames);
var payout = parseInt(interestingCasePropertyValues[0]);

// Calculate the updated payout amount:
payout = payout - deductible;

// Set the updated payout amount on the parent case:
var casePropertyValuesToUpdate = new tw.object.listOf.toolkit.TWSYS.String();
casePropertyValuesToUpdate[0]=payout.toString();
tw.system.currentProcessInstance.parentCase.setCasePropertyValues(interestingCasePropertyNames, casePropertyValuesToUpdate);

Note: This example is equivalent to the following script that accesses content object properties through process variables:

tw.local.caseProperties.Payout.value -= tw.local.activityProperties.Deductible.value;

Examples of scripts that create a new case and initialize the values

The example scripts can be used in any process. The scripts create a new case and initialize it with values for the following types:

  • A property that is multivalued.
  • A business object that contains a multivalued property and some single-valued properties.

The example scripts are shown in the following two code fragments:

var propertyNames = new tw.object.listOf.String(); // The list of property names to set during case creation.
var propertyValues = new tw.object.listOf.String();// The list of property values to set during case creation.

propertyNames[0] = "S1_Destination";
propertyValues[0] = "YYZ"; // Single String value.

propertyNames[1] = "S1_DepartureDate";
var depDate= new Date(Date.now() + 24 * 60 * 60 * 1000);
propertyValues[1] = depDate.toISOString().replace(/\.\d+/, ""); // DateTime in format: yyyy-MM-dd'T'HH:mm:ss'Z'

propertyNames[2] = "S1_Shippers";
propertyValues[2] = "{'ABC Logistic','XYZ Freight'}" // Multiple values.

propertyNames[3] = "S1_Closed";
propertyValues[3] = false.toString(); // Boolean value.

propertyNames[4] = "S1_Packages";
propertyValues[4] = "{\
    \"objects\": [{\
            \"properties\": [{\
                    \"name\": \"S1_TrackingNumber\",\
                    \"value\": \"T100001\"\
                }, {\
                    \"name\": \"S1_MinimumTemperature\",\
                    \"value\": \"-40\"\
                }, {\
                    \"name\": \"S1_ItemDescription\",\
                    \"value\": \"{'Foo','Bar'}\"\
                }, {\
                    \"name\": \"S1_Value\",\
                    \"value\": \"150,000.00\"\
                }\
            ]\
        }, {\
            \"properties\": [{\
                    \"name\": \"S1_TrackingNumber\",\
                    \"value\": \"T100002\"\
                }\
            ]\
        }\
    ],\
    \"requiredClass\": \"S1_Package\"\
}"; // Multiple business object values.

tw.system.currentProcessInstance.createCaseUsingSpecifiedCaseType("S1_Shipment", propertyNames, propertyValues);

An alternative way of creating a case is by using the tw.system.currentProcessInstance.createCase JavaScript method. Instead of using lists of string parameters with property names and values, this method accepts a Record business object that is available in the system toolkit. By passing a Record object, you can initialize properties of a new case in other case solution projects without needing to convert all property values to strings:

tw.local.newCaseProperties = new tw.object.Record();

tw.local.newCaseProperties.setPropertyValue("S1_Destination", "YYZ");

var depDate = new tw.object.Date();
depDate.setUTCTime(depDate.getUTCTime() + 24 * 60 * 60); // tomorrow
tw.local.newCaseProperties.setPropertyValue("S1_DepartureDate", depDate);

var shippers = new tw.object.listOf.String();
shippers[0]="ABC Logistic";
shippers[1]="XYZ Freight";
tw.local.newCaseProperties.setPropertyValue("S1_Shippers", shippers);

tw.local.newCaseProperties.setPropertyValue("S1_Closed", false);

var packages = new tw.object.listOf.Record();
packages[0] = new tw.object.Record();
packages[0].setPropertyValue("S1_TrackingNumber", "T100001");
packages[0].setPropertyValue("S1_MinimumTemperature", -40);
var descriptions = new tw.object.listOf.String();
descriptions[0]="Foo";
descriptions[1]="Bar";
packages[0].setPropertyValue("S1_ItemDescription", descriptions);
packages[0].setPropertyValue("S1_Value", 150000.00);
packages[1] = new tw.object.Record();
packages[1].setPropertyValue("S1_TrackingNumber", "T100002");
tw.local.newCaseProperties.setPropertyValue("S1_Packages", packages);

tw.system.currentProcessInstance.createCase("S1_Shipment", tw.local.newCaseProperties);