Shared business objects

A shared business object's values are accessible to other instances at run time, which can be helpful when an application needs to use current data.

Because a shared business object is a different type of business object, you should note its special considerations.

  • Shared business objects only apply to a complex structure type.
  • The data within a shared business object is shared between business processes and tasks.

    You can send data from one process to a second process using a Message Event or by loading the data into the second process using the unique key of the shared business object. To load the data, get the unique identifier key and then load the instance using the key. For example in the following code, sharedBusinessObjectKey would be obtained by running tw.local.myVariable.metadata("key").

    tw.local.myVariable = new tw.object.mySharedBusinessObject(sharedBusinessObjectKey);
  • A shared business object uses database resources. The data within a shared object is persisted to the database when:
    • The shared object is created
    • The task is persisted to the database
    • The JavaScript method save() is performed on the shared business object.
  • Each shared object is logically connected to the business process instance that created it. When the business process instance is deleted (for example, when you delete a business process instance that was running in the Inspector), the shared object data in the database is also deleted.
  • If you clear the Shared Object check box at a later point in time, the business object will no longer be accessible to other instances at run time.
  • If the shared business object is created within a human service that can be started and that activity is not bound to a business process instance, the shared business object is connected to the activity's task instance. In such cases, the shared business object is deleted if the task instance is deleted.
  • If a shared business object is deleted from the database, the behavior of the tasks or processes that reference the shared business object is undefined.
  • Best practice for shared business objects: If you want to create shared business objects that have a long lifetime, you may want to design a business process that acts as a factory (that is, it is based on a factory method pattern). The result is your shared business objects will remain in the database until the factory business process is deleted.
  • An output shared business object for an external service, such as a web service, and an Advanced Integration Service will return a new and typically updated copy of the original input shared business object. Therefore, your application should not reference the input shared business object expecting an updated value if your application is working with an external service or an Advanced Integration Service. It should reference the new output shared business object.
  • Shared business objects allow concurrent modification. For example, two users can view and modify the same shared business object instance in a human service. When users trigger a boundary event, the data of the shared business object instance is saved. Only the fields that each user modifies are saved. Therefore, if the two users modify different fields, both changes are saved. If both users modify the same field, the last user’s update is saved. In addition to multiple users, you can have a situation with automated steps, for example, a server script that makes modifications to shared business objects.

You can create and retrieve the shared business objects you have created by using a key, as shown in the following example. In this case, tw.object.SharedObject is the shared business object.

// Create shared object
var sharedObject = new tw.object.SharedObject();
sharedObject.taskId = "init";
sharedObject.save();

// Create key for shared object
tw.local.sharedObjectKey = sharedObject.metadata("key");
log.info("sharedObjectkey: " + tw.local.sharedObjectKey);

// Retrieve shared object by key
var sharedObject2 = new tw.object.SharedObject(tw.local.sharedObjectKey);
log.info("sharedObject2.taskId: " + sharedObject2.taskId);