Sending a message only if data model fields changed

To use a GX model in messaging code, edit the Event Fired rules to generate a message payload that uses the GX model.

After you pass a type to a GX model’s create method, check the $ShouldSend property. The $ShouldSend property returns true if at least one mapped data model field changed in the local database bundle. If you want to send your message only if data model fields changed, use the $ShouldSend property to determine whether data model fields changed.

if (MessageContext.EventName == "AddressChanged") {
  var xml = mycompany.messaging.addressmodel.Address.create(MessageContext.Root as Address)

  if (xml.$ShouldSend) {
    var strContent = xml.asUTFString()
    var msg = MessageContext.createMessage(strContent)

    print("Message payload for debugging:")
    print(msg);
  }
}

When Gosu checks for mapped data model properties that changed, Gosu checks both normal and key properties. Because a key property uniquely identifies an object, typically that value never changes.

An important exception to this behavior occurs if any ancestor of a changed property changed its value, including to null. In this case, the change might not trigger $ShouldSend to be true. If the original value of the non-data-model field indirectly referenced data model fields, those fields do not count as properties that trigger $ShouldSend to be true. If the property that changed has a path from the root type that includes non-data-model fields, Gosu’s algorithm for checking for changes does not determine that this field changed. This behavior also occurs if the property value changed to null. PolicyCenter can tell whether a property changed only if all its ancestors are actual data model fields rather than enhancement properties or other virtual properties.

For example, suppose that a user action causes a change to the mapped field A.B.C.D property and also A.B.C became null. If the B property and the C property are both data model fields, Gosu detects this bundle change and sets $ShouldSend to true. However, if the B.C property is implemented as a Gosu enhancement or an internal Java method, then $ShouldSend is false. This difference is because the bundle of entities does not contain the old value of A.B.C. Because Gosu cannot determine whether the property changed, Gosu does not mark the change as one that sets the property $ShouldSend to the value true.

This special behavior with non-data-model properties subgraphs is similar to how the Incremental property in GXOptions is effectively disabled in virtual property subgraphs.

Note that if a property contains an entity array and its contents change, $ShouldSend is true. In other words, if an entity is added or removed from the array, $ShouldSend is true.

The behavior of $ShouldSend does not change based on the value of the Verbose or Incremental options.