Declare trigger and OnChange in pega

Most rules in Pega run only when something calls them. Declarative rules are different: Pega runs them for you when a value changes or a record is saved. Declare OnChange and Declare Trigger are the two that run an activity automatically, and they are easy to confuse.

Declare OnChange

A Declare OnChange rule watches one or more properties. When the value of a watched property changes, Pega runs the activity you named. The check happens at activity step boundaries, so you do not need to call anything. This is a simple form of forward chaining.

  • Use it to react to a business value changing, for example a status or a risk score.
  • Keep the activity light. It runs inside the transaction that changed the property, so long work or database commits do not belong in it.
declare onChange

Declare Trigger

A Declare Trigger rule fires when an instance of a class is saved, deleted or committed. It is about database events, not property values. The triggered activity runs during the commit, so it cannot itself commit a database transaction.

  • Use it to write an audit record, send a notification, or copy a value to another table whenever a record is updated.
declare_trigger

Side by side

Declare OnChangeDeclare Trigger
Fires whenA watched property changes valueAn instance is saved, deleted or committed
CheckedAt activity step boundariesDuring the database operation
Best forReacting to business data changesAudit and side effects on persisted records
RestrictionKeep the activity lightThe activity must not commit

A worked example

A bank wants to flag a customer for review whenever the account balance falls below 500.

  1. Create a Declare OnChange rule on the Account class that watches .Balance.
  2. Point it to an activity FlagLowBalance that sets .ReviewFlag to true when the new balance is under 500.
  3. Now any activity, data transform or screen that changes the balance will set the flag automatically, and you never have to remember to call the check.

If the same bank also needs a history of every save to the Account table, use a Declare Trigger on the Save event that writes a row to an audit class.

Cautions

  • Declarative rules are hard to spot when debugging. Use the Tracer and the rule's Where referenced list to find them.
  • Do not let one OnChange rule change a property that another OnChange rule watches unless you have thought through the loop.
  • If a simple data transform on the form would do, prefer that, because it is easier for the next developer to read.

Interview tip

Say: "OnChange reacts to a property value changing, Trigger reacts to a database save, delete or commit." Then give the low balance example. More in the Activity label.

No comments:

Post a Comment