Sometimes we need to validate attribute based on other entity’s attribute. In this post I’m using HR schema, there are master detail relationship between Jobs and Employees table. So when you will create Entity objects using these tables, association will create automatically using foreign key relationship or you can create it yourself.
As you can see in following image relationship defined between jobs and employees table using Association. And Accessor name are used to get attribute value in other entity. So you can get Jobs table attribute in Employees table using accessor name Jobs.
Jobs table contain MinSalary and MaxSalary attribute for each Job.
So here we are going to validate Salary attribute of Employees table. Salary must be less than or equal to Max salary as defined in Jobs table for each job type.
Groovy Expression allows to access attribute of other entity using accessor name. Following are steps to do the same-
- Add validation rule in Salary Attribute on employees table using Green Plus icon.
- On dialog window select
- Compare With=Expression
- And write expression MaxSalary where Jobs is accessor name and MaxSalary is attribute name of Jobs table.
- Then Go to Failure Handling tab and set error message. You can also use Accessor to give custom message to user as below image(source.Jobs.MaxSalary)