Tuesday, August 7, 2012

Difference Between ADF BC Groovy Old Value and Posted Attribute

To keep it short - ADF BC Groovy keyword "oldValue" and ADF BC API method getPostedAttribute(index) are not the same thing. I spoke with ADF developer today, who have spent around 5 days debugging and hitting validation bug related to incorrect usage of "oldValue" by Groovy. Both approached - "oldValue" and getPostedAttribute(index) are valid for its own use cases. But you should clearly understand the difference, otherwise may hit some painful bugs in validation rules.

How it works in ADF 11g R2:

1. "oldValue" Groovy keyword returns last valid value
2. getPostedAttribute(index) returns value from database

In most of the cases, we need to compare user input against original value from the database. This makes Groovy "oldValue" keyword pretty useless in such use cases where we want to validate data against original one.

Download sample application where both cases are demonstrated - OldNewValueValidationApp.zip.

Sample application defines one validation rule for CommissionPct attribute:


This rule is using Groovy oldValue and newValue keywords:


Second validation rule is defined for Salary attribute, its based on Java method. We are going to call getPostedAttribute(index) method from there:


Here is the code sample for getPostedAttribute(index) invocation:


1. Test for Groovy oldValue keyword

Initial value for CommissionPct is 0.1:


Change it to negative -0.1, this will trigger validation rule:


Old value is reported to be 0.1, this is correct. Now enter positive valid value 0.2:


Type -0.2 now, old value is reported as 0.2 - this is last valid value, but not the original value from database (we didn't commited yet our change to database):


2. Test for getPostedAttribute(index) method

Initial value for Salary is 16000:


Change it to 300, validation error is shown and you can check that old value is reported as 1600 (correct):


Change to new valid value 17000 now:


Again change back to invalid 400 - old value is reported correctly, the one original from database 16000:


4 comments:

Anonymous said...

Hi Andrejus,

Thanks for the nice article.
I have a following question:
How to specify the error message
"{0} Monthly Salary must be above{1}"
Where as {0} will be adf.newValue
and {1} should be the compareValue
So the question is how to refer to the compareValue in Failure Handling tab?

Thanks
SPG

Andrejus Baranovskis said...

Check 'Token Message Expressions', it is described in this post.

Andrejus

Mahmoud Yacine said...

thank you for the article but I have one question.
you printed using System.out, but how can I show it on the error message. it is java method not groovy expression, so how can I do that.

please

Andrejus Baranovskis said...

You can throw JboException or ValidationException.

Andrejus