Access DAO object in Constraint script
It seems it's not possible to access DAO objects in a Constraint validation script. For instance, when I try to validate using objectDAO.findByCode(input.myCode, 0, 1), I get the following error:
objectDAO is neither defined in the script nor in dependencies.
Is it so or am I doing something wrong? And if so, what alternative is feasible to validate input data against BDM (database) values?
Thank you in advance.
Actually for constraints definition you can only use contract inputs and you don't have access to external data such as business data.
What I can recommend is to do the validation as much as possible in the forms where you can actually call the API to get business data values.
Also if needed you might want to store receive data in process variable and then do some verification in a dedicated step in the process. You can then store the result of the validation as well in a process variable and use it in gateway to route the process execution.
Thank you for your promptly answer.
The problem in this case is we are trying to validate an item which is part of a complex object. We created a custom widget that would only accept correct values, but couldn't find a way to assign it to the specific value. For some reason, the designated variable arrived at the backend empty, although we were using the exact variable name that the form was generated with.
Then, we tried this back-end validation.
I'll explain a little more our scenario: We are entering an invoice with a header and detail lines. We want to make sure the detail lines refer to actual product codes (codes which are different from the product persistenceId).
In this scenario, how can we validate a detail line item against a BDM object?
I had to do this in my application, and had to cheat to get it to work.
What I did in the end was to add a function to a custom groovy script which went direct to the database, bye-passing bonita's persistence layer. I'm using MySQL, so I used the java.sql library to do it. It's safe to do for queries, but avoid doing any updates.
I hate having to do it that way, but until Bonita provide access to the API objects inside a constraint validator it's what you're stuck with.
It works - but it isn’t nice.
I'll do as you suggest.