Interacting with cases and activities from processes or service flows

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.

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

This example script is used in a process that implements a case activity. The script accesses content object properties through process variables, as shown in the following code fragment:

tw.local.caseProperties.Payout.value -= tw.local.activityProperties.Deductible.value;
The following example shows how an equivalent previous version of the script includes methods that call the parentCase JavaScript API to perform the following operations:
  1. Obtain a value of an activity property from the parent activity.
  2. Obtain a value for a case property.
  3. Calculate a new value of a case property based on the value of the retrieved activity property.
  4. Set a new value for a case property.
  5. Set a new value for an activity property of a parent activity.
  6. Obtain a value of an activity property from a specified activity.
  7. Set a new value for an activity property of a specified activity.
// Obtain the deductible amount from a 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);

//Set a new value for an activity property of a parent activity:
var interestingActivityPropertyNames = new tw.object.listOf.toolkit.TWSYS.String();
interestingActivityPropertyNames[0] = "S1_Deductible"; //The list (String) of property names for which a value is to be set
var ActivityPropertyValuesToUpdate = new tw.object.listOf.toolkit.TWSYS.String(); 
ActivityPropertyValuesToUpdate[0]=payout.toString(); //The list (String) of property values to set
tw.system.currentProcessInstance.parentCase.setParentActivityPropertyValues(interestingActivityPropertyNames, ActivityPropertyValuesToUpdate);

// Obtain the deductible amount of an activity property from a specified activity:
var interestingActivityID = "A01E758B-0000-C4DF-9D9D-B8320585E550" ;// The unique identifier or GUID of the case activity
interestingActivityPropertyNames[0] = "S1_Deductible"; //The list (String) of property names whose value is to be retrieved 
var interestingActivityPropertyValues = new tw.object.listOf.toolkit.TWSYS.String(); 
interestingActivityPropertyValues = tw.system.currentProcessInstance.parentCase.getActivityPropertyValue(interestingActivityID , interestingActivityPropertyNames); //returns a list of values for a given list of activity property names
var deductible = parseInt(interestingActivityPropertyValues[0]);

//Set a new value for an activity property of a specified activity:
var interestingActivityID = "A01E758B-0000-C4DF-9D9D-B8320585E550"; // The unique identifier or GUID of the case activity
interestingActivityPropertyNames[0] = "S1_Deductible"; //The list (String) of property names for which a new value is to be set
var interestingActivityPropertyValues = new tw.object.listOf.toolkit.TWSYS.String(); 
interestingActivityPropertyValues[0] = "8.9"; // The list (String) of new property values
tw.system.currentProcessInstance.parentCase.setActivityPropertyValues(interestingActivityID , interestingActivityPropertyNames, interestingActivityPropertyValues);

Examples of scripts for updating multiple properties

The following is an example of string and multi-value string properties that are updated by using Content objects properties.

tw.local.caseProperties.Name.value= "Andrew Smith";
tw.local.caseProperties.Address.value[0]= "Street No 1";
tw.local.caseProperties.Address.value[1]= "Road No 2";
Restriction: Do not set the values of two separate properties by using both the JavaScript API method (setCasePropertyValues()) and process variables in the same script, as shown in the following example:
tw.local.caseProperties.Name.value= "Andrew Smith";

var casePropertyNames = new tw.object.listOf.String();
var casePropertyValues = new tw.object.listOf.String();
casePropertyNames[0] = "LO_Address";
casePropertyValues[0] = "{'Street No 1 ','Road No 2'}";
tw.system.currentProcessInstance.parentCase.setCasePropertyValues(casePropertyNames, casePropertyValues, true);

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

The example scripts can be used in any process. The scripts create a new case and initialize it with values for:
  • A multi-value property.
  • A business object that contains a multi-value property and some single-value properties.
See the example scripts in the following code snippets:
var propertyNames = new tw.object.listOf.String(); // The list of property names to set during the case creation.
var propertyValues = new tw.object.listOf.String();// The list of property values to set during the 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 the 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);

Example of using tw.system.createCase to create a case from a client-side human service

Another alternative way of creating a case is by using the tw.system.createCase JavaScript method. This method is available only within the scope of a service call that is called from a client-side human service. It does not work when called from a server-side script that is part of a process. The properties passed to the createCase method can be constructed in two different scopes:

  1. Within a process activity created from the Case Solution

    In this scenario, the process has access to the caseProperties Content Object as variables. These can be initialized by using a script like the following:

    // Set Destination
    tw.local.caseProperties.Destination.value = "YYZ";
    
    // Format and set DepartureDate in ISO format
    var depDate = new Date(Date.now() + 24 * 60 * 60 * 1000);
    tw.local.caseProperties.DepartureDate.value = depDate.toISOString().replace(/\.\d+/, "");
    
    // Set Shippers as a multi-value string
    var shippers = new tw.object.listOf.String();
    shippers[0] = "ABC Logistic";
    shippers[1] = "XYZ Freight";
    tw.local.caseProperties.Shippers.value = shippers;
    
    // Set Closed to true
    tw.local.caseProperties.Closed.value = true;
    
    // Set descriptions as a multi-value string
    var descriptions = new tw.object.listOf.String();
    descriptions[0] = "Foo";
    descriptions[1] = "Bar";
    
    // Initialize Packages as a list of 'Package' content objects
    tw.local.caseProperties.Packages.value = new tw.object.listOf.contentObject.S1_Package();
    
    // First package
    tw.local.caseProperties.Packages.value[0] = new tw.object.contentObject.S1_Package();
    tw.local.caseProperties.Packages.value[0].TrackingNumber.value = "T100001";
    tw.local.caseProperties.Packages.value[0].MinimumTemperature.value = -40;
    tw.local.caseProperties.Packages.value[0].ItemDescription.value = descriptions;
    tw.local.caseProperties.Packages.value[0].Value.value = 150000.00;
    
    // Second package
    tw.local.caseProperties.Packages.value[1] = new tw.object.contentObject.S1_Package();
    tw.local.caseProperties.Packages.value[1].TrackingNumber.value = "T100002";
    

    This initialized caseProperties Content Object can be used as input to create a new case by using tw.system.createCase.

    This Content Object when passed to a user task, cannot be directly passed to a service flow within the task. Instead, define a string input variable (for example, jsonStringValue) in the service flow and pass the serialized Content Object by using:

    JSON.stringify(tw.local.caseProperties)
    

    Then, in the service flow script:

    var newCaseProperties = new tw.object.Record();
    var propname = "casePropertiesJSON";
    var propvalue = tw.local.jsonStringValue;
    newCaseProperties.setPropertyValue(propname, propvalue);
    
    // Create the case
    tw.local.caseId = tw.system.createCase("SC_CT1", "tos", newCaseProperties, null, false);
    
    This approach achieves two goals:
    • Initializes the caseProperties Content Object.
    • Uses its string representation as a record input for tw.system.createCase.
  2. Within a stand-alone Client-Side Human Service

    In this case, no Content Object is available. Instead, define a JSON representation of the case properties and use it directly in the tw.system.createCase method.

    // create a json that could have the properties such as 'Closed' 
    // which is again represented as a json, that has a 'displayName' and 'value'
    // example is given on how to set values for single, multi value and 
    // business object properties are set
    var caseProperties = {
       "Closed": {
          "displayName": "Closed",
          "value": true
       },
       "DepartureDate": {
          "displayName": "DepartureDate",
          "value": "2025-08-21T10:04:21.000Z"
       },
       "Destination": {
          "displayName": "Destination",
          "value": "YYZ"
       },
       "Packages": {
          "displayName": "Packages",
          "value": [
             {
                "ItemDescription": {
                   "displayName": "ItemDescription",
                   "value": [
                      "Foo",
                      "Bar"
                   ]
                },
                "MinimumTemperature": {
                   "displayName": "MinimumTemperature",
                   "value": -40
                },
                "TrackingNumber": {
                   "displayName": "TrackingNumber",
                   "value": "T100001"
                },
                "Value": {
                   "displayName": "Value",
                   "value": 150000
                }
             },
             {
                "ItemDescription": {
                   "displayName": "ItemDescription",
                   "value": [
                   ]
                },
                "MinimumTemperature": {
                   "displayName": "MinimumTemperature",
                   "value": 0
                },
                "TrackingNumber": {
                   "displayName": "TrackingNumber",
                   "value": "T100002"
                },
                "Value": {
                   "displayName": "Value",
                   "value": 0
                }
             }
          ]
       },
       "Shippers": {
          "displayName": "Shippers",
          "value": [
             "ABC Logistic",
             "XYZ Freight"
          ]
       }
    };
    // Create a local variable and initialize it to be a Record
    var newCaseProperties = new tw.object.Record();
    // set a local variable propname to any name for example casePropertiesJSON
    var propname = "casePropertiesJSON";
    // set a local variable propvalue set to the string value of the JSON 
    propvalue = JSON.stringify(caseProperties);
    
    
    // set the name/value into the record object
    newCaseProperties.setPropertyValue(propname, propvalue);
    // use the record object as input to the createCase method 
    // the first argument is the case type 
    // the second argument is the target object store 
    // the third argument is the record object 
    // the fourth argument is a case folder structure which could be set to null 
    // the fifth argument is a boolean value that is used to indicate to the method 
    // whether the case is to be created by an Admin user (if value is true) or 
    // by the user that is logged in (if value is false)tw.local.caseId =
    tw.local.caseId = tw.system.createCase("SC_CT1", "tos", newCaseProperties, null, false);
     

Example of a script for searching case activities

The scripts searches for activities in a specified case by using the provided property filter conditions. See the example script in the following code snippet:
var returnedActivityIdList = new tw.object.listOf.toolkit.TWSYS.String();
var interestingActivityNames = new tw.object.listOf.toolkit.TWSYS.String(); 
interestingactivityNames[0]= "S1_ActivityName1"; // A list of symbolic activity names
var propertyFilter = "[TaskState]=4 OR [TaskState]=5 OR [TaskState]=6" ;// The string must conform to the Content Platform Engine SQL syntax, for example: '([prop1] = 1 AND [prop2] <> 3)'
returnedActivityIdList = tw.system.currentProcessInstance.parentCase.searchActivities(interestingActivityNames, propertyFilter); //Returns a list of activity IDs that match the given conditions.
var activityID = parseString(returnedActivityIdList[0]);
//The sample result of variable  activityID will be  "A01E758B-0000-C4DF-9D9D-B8320585E550"