与流程或服务流中的案例和活动交互
Draft comment:
This topic was viewed 12 times since its publication
您可以使用包含 JavaScript 方法来调用案例 API 的脚本,与流程或服务流中的案例和案例活动进行交互。 这使您能够使用案例属性的值,例如多值属性或业务对象。This topic was viewed 12 times since its publication
本主题提供了以下示例:
与案例和活动属性值交互的脚本示例
此示例脚本用于实现案例活动的流程。 该脚本通过流程变量访问内容对象属性,如以下代码片段中所示:
tw.local.caseProperties.Payout.value -= tw.local.activityProperties.Deductible.value;
以下示例显示了脚本的等效先前版本如何包含调用 parentCase JavaScript API 以执行以下操作的方法:
- 从父活动获取活动属性的值。
- 获取案例属性的值。
- 根据检索到的活动属性的值计算案例属性的新值。
- 为案例属性设置新值。
- 为父活动的活动属性设置新值。
- 从指定活动获取活动属性的值。
- 为指定活动的活动属性设置新值。
// 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);
用于更新多个属性的脚本示例
以下是使用内容对象属性更新的字符串和多值字符串属性的示例。
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";
限制: 请勿在同一脚本中同时使用 JavaScript API 方法 (setCasePropertyValues()) 和流程变量来设置两个单独属性的值,如以下示例中所示:
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);用于创建新案例并初始化其值的脚本示例
可以在任何流程中使用这些示例脚本。 这些脚本将创建一个新案例,并使用下列各项的值对其进行初始化:
- 多值属性。
- 包含多值属性和某些单值属性的业务对象。
请参阅以下代码片段中的示例脚本:
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);创建案例的替代方法是使用 tw.system.currentProcessInstance.createCase JavaScript 方法。 此方法不使用具有属性名称和值的字符串参数列表,而是接受系统工具箱中可用的
Record 业务对象。 通过传递 Record 对象,您可以在其他案例解决方案项目中初始化新案例的属性,而无需将所有属性值转换为字符串: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);使用 tw.system.createCase 从客户端人工服务创建案例的示例
另一种创建案例的方法是使用 tw.system.createCase JavaScript 方法。 该方法仅在从客户端人类服务调用的服务调用范围内可用。 从作为进程一部分的服务器端脚本调用时,它不起作用。 传递给 createCase 方法的属性可以在两种不同的作用域中构建:
- 在根据案例解决方案创建的流程活动中
在这种情况下,进程可以访问作为变量的
caseProperties内容对象。 可以使用类似下面的脚本对它们进行初始化:// 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";使用
tw.system.createCase,可将初始化后的caseProperties内容对象作为创建新案例的输入。该内容对象在传递给用户任务时,不能直接传递给任务内的服务流。 取而代之的是,在服务流程中定义一个字符串输入变量(例如
jsonStringValue),并通过以下方式传递序列化的内容对象:JSON.stringify(tw.local.caseProperties)然后,在服务流程脚本中
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);这种方法实现了两个目标:- 初始化 caseProperties 内容对象。
- 将其字符串表示作为 tw.system.createCase 的记录输入。
- 在独立的客户方人类服务机构内
在这种情况下,没有内容对象可用。 取而代之的是,定义案件属性的 JSON 表示法,并在
tw.system.createCase方法中直接使用。// 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);
用于搜索案例活动的脚本示例
这些脚本使用提供的属性过滤条件来搜索指定案例中的活动。 请参阅以下代码片段中的示例脚本:
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"
This topic is shared by BAW, CP4BA, CP4BASaaS. Last updated on 2025-01-20 10:38