Determining what data changed in a bundle

In rule set code, you sometimes need to identify data that recently changed. You might need to compare the most recent entity data from the database with the latest version of an entity in a locally modified bundle. In the most common case, rule sets contain conditions and rule actions that must detect which if any properties just changed. For example, if a certain object property changed, you might want to validate properties, recalculate related properties, log changes, or send messages to external systems.

To detect data changes, call the object’s isFieldChanged method. If it changed, call the entity’s getOriginalValue method to get the original value loaded from the database. You must cast the original value to the expected type with the syntax “as TYPENAME”.

Depending on the type of value stored in that property, the isFieldChanged method behaves differently. The following table describes the behavior based on the property type.

Type of property

Behavior of getOriginalValue

Behavior of isFieldChanged

Scalar value

Returns the original simple value, such as a String value like "John Smith", a number like 234, or any other non-entity and non-array type.

Returns true if and only if the actual property value changed to a different actual value. If the value changed to a different value and then back to the original value, isFieldChanged would return false.

Entity

Guidewire applications represent links to entity subobjects as a foreign key called the internal ID. This foreign key is the entity.Id property on the destination entity. Note that the Id property is different than the PublicID property. If you call getOriginalValue with a foreign key field, you get an internal ID as a Key object. A Key object encapsulates the entity type and the unique ID value for that object.

To get a reference to the entity instance with the original ID use the bundle method loadBean.

For example:

var k = e.getOriginalValue("Address1")

var bundle = Transaction.getCurrent()

var addy = bundle.loadBean(k) as Address

Remember to call the getOriginalValue method on the correct entity instance.

Returns true if and only if the foreign key Id value for the subobject is the same in the original entity. Remember to call the isFieldChanged on the correct entity instance.

Keep in mind that checks for a changed entity could return false even if there are changes on properties of subobjects.

Be aware that the foreign key is a link to the Id property of the target object. The PublicID property of the target object is not examined.

Array

Returns an array containing the set of array elements that were originally persisted.

If any properties changed on those elements, the bundle contains the new values, not the original values. If you need the original properties on those array elements, you must examine each combination of element and properties by calling getOriginalValue on each array element for each property.

Returns true if any elements were added or removed to the array.

Additionally, if the array is defined in the data model configuration as an owned array, if any properties on an array element changed, isFieldChanged returns true. This does not apply to non-owned arrays.

Note: Because entity array order is not meaningful, the order is not checked by isFieldChanged. In this case, isFieldChanged returns the same value as the array method isArrayElementAddedOrRemoved.

Scalar value property example

For a scalar value example, the Address entity contains a String value property called City. If the address.City value is different from the original entity, address.isFieldChanged("City") returns true and address.getOriginalValue("City") returns the original value for this property of type String.

Entity property example 1

For an entity property example, the ClaimCenter application includes a Claim entity with a LossLocation property containing an Address entity.

If that property points to an entirely different entity instance than the original entity loaded from the database, it is considered changed:

  • The expression claim.isFieldChanged("LossLocation") returns true
  • The expression claim.getOriginalValue("LossLocation") returns the ID of the original entity instance as a Key. Do not assume that the original entity instance is unchanged. It may be loaded and already changed.

For example:

if (claim.isFieldChanged("LossLocation")) { 
  // Get original entity
  var id = claim.getOriginalValue("LossLocation") as Key 
  var originalAddress = claim.Bundle.loadBean(id)

  // Get original property on original entity
  var origAddLine1 = originalAddress.getOriginalValue("AddressLine1")
}

In contrast, if the claim.isFieldChanged("LossLocation") returns false, then the entity foreign key is unchanged but that does not mean necessarily that data in a subobject is unchanged. To check properties on subobjects, you must test specific properties on the subobject using the isFieldChanged method. For example:

if (not claim.isFieldChanged("LossLocation")) {
    if (claim.LossLocation.isFieldChanged("City")) {
      var origAddLine1 = claim.LossLocation.getOriginalValue("AddressLine1")
  }
}

Entity property example 2

The following example gets the original value for an entity property and gets a property on that original object

var originalPolicyID = account.getOriginalValue("Policy") as Key
var originalPolicy = account.Bundle.loadBean(originalPolicyID)
var originalPolicyNumber = originalPolicy.getOriginalValue("PolicyNumber") as String

See also