Declare OnChange in Pega
This is more like a continuation blog article on Declarative processing. I would recommend you to visit my another blog article on Declare expression rules.
I already explained some basics about declarative processing and RAG networks.
We also saw there are different declarative rules available and Declare OnChange is one of those.
In this blog article, we will see in detail about Declare OnChange rule.
Most of the screenshots were used from Pega Infinity '24 version. Also reused a few screenshots from older versions to explain the core concepts.
Just like other declarative rules, Declare OnChange get invoked automatically and they don’t need any manual invocation.
The primary difference between Declare Trigger and Declare OnChange are Declare triggers listen to persistence events like (Save/Delete), while Declare On change listens to any property update in a clipboard, but both can execute an activity.
Declare On Change:
“Imagine the shopping cart in a website. Whenever we add some special products, the business policy may involve complex calculation to determine the subtotal amount and may involve adding a gift. This complex calculation may involve some java steps and functions and can be achieved by using an activity”.
To solve this requirement, you can create a Declare on change to monitor the properties and execute an activity when the property changes.
Fires at change in a property
What is a Declare OnChange?
– Declare OnChange comes under decision category and implements forward chaining.
– When a value of a specific property changes, then it can execute an activity.
– Generally, it is used to do complex mathematical calculation. It can also track a status of the work item.
How do we configure a Declare Onchange rule?
Create a new declare rule from decision category.
Similar to Declare trigger, here also we have two main tabs:
- OnChange properties
- Pages & Classes
OnChange Properties tab
Properties To Watch
You can list as many properties as you want. The properties can be in top level page or embedded page.
Conditions
When – Specify a ‘when’ rule.
Choose action – You have two options:
- Call activity – You can call an activity of type Onchange.
- Suspend work Object.
What is suspend workobject?
Imagine a scenario, when an unexpected business exception scenario occurs.
For the Onboarding application, in the Validation stage we need to validate the customer profile with external third-party validation. If the third-party validation is not completed, then the case should be suspended at that stage.
We can have a Declare onchange rule to check the External Party Verified property changes.
Specify the ‘when’ rule to check, if the External Party Verified is ‘No’ . If yes, then you can suspend workobject.
– What actually means is that we are suspending all the assignments for the workitem. The assignment owner will be notified by an email. No one can work on the item till the reviewer approves.
– The status of the workitem gets changed to ‘Pending – PolicyOverride’ and a new assignment is created in ‘Assign-Suspend’ class.
– The flow identified in the rule moves the assignment to the reviewer. The Reviewer can either allow or deny the process.
Pega provides sample rules which you can customize in your own application.
How to configure suspend workobject?
Step 1 : Create a Declare onchange rule. Specify the target property as External Party Verified.
Step 2: Create a ‘when’ rule, that checks if External Party Verified property value is ‘No’.
And then specify the when rule in the Declare OnChange rule.
Step 3: Choose the action as Suspend work object and accept the default policy override suspend action configurations.
Step 4: Use the field External Party Verified in the UI and test the case status.
Step 5: Start your tracer and click on submit..
You can see the case is now Pending-PolicyOverride.
– The default routing activity in policy override flow routes this assignment ‘Assign-Suspend’ to admin@org.com operator ID. He/she can review it and decide either to allow or deny the exception.
– On denying the workitem is resolved.
You can customize the router activity ‘ToPolicyOverrideOperator’ to suit your requirement.
From the tracer, you can clearly see the declare onchange executions
Okay now let’s come back to the rule configuration.
When you choose the action as Call activity, you get two options block.
If the ‘when’ evaluates to true you can run one activity and if it evaluates to false you can run different activity.
Pages & Classes tab
We have the same configuration like declare trigger rule
How to test a Declare Onchange rule with Call activity configuration?
Scenario – We are in a Purchase Request case, where we capture the Product line items in the cart. We are going to calculate the total cost (Subtotal) of the purchase and track the total cost Subtotal) property. Whenever the total purchase is above 10,000, we are going to add a gift watch in the cart.
In the above scenario (Subtotal > 10,000), we should automatically add another item to the shopping cart as Gift watch!
Let’s see how we can achieve this 😊
Step 1: Create a new declare Onchange rule that monitors Subtotal property.
Step 2: Create a ‘when’ rule – SubTotalAboveTenK
Step 3: Create a new activity of type Onchange and add it in rule with, when evaluates to true.
The activity simply appends a new page to the LineItem with the Gift watch.
Step 4: Save the rule form and check it in user portal. Remember to include refresh strategy in the grid.
After updating, enter the subtotal greater than 10, 000.
You see the Declare OnChange successfully triggered and executed the OnChange activity.
You also see from the user portal, a new lineitem is automatically added.
We have successfully implemented both the scenarios for Declare OnChange.