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 |
Behavior of |
|---|---|---|
Scalar value |
Returns the original
simple value, such as a |
Returns |
Entity |
Guidewire applications
represent links to entity subobjects as a foreign key called the internal ID. This foreign key is the entity To get a reference to the entity instance with the original ID use the bundle method loadBean. For example:
Remember to call the getOriginalValue method on the correct entity instance. |
Returns
Keep in mind that checks for a changed entity
could return Be aware that the foreign key is a link to
the |
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 |
Returns Additionally, if the array is defined in the
data model configuration as an owned array, if any properties on an array
element changed, 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")returnstrue - The expression
claim.getOriginalValue("LossLocation")returns the ID of the original entity instance as aKey. 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
