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?
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 business variable and use it in gateway to route the process execution.
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.
About your issue with the variable arriving empty, maybe you can create another question and share your diagram + BDM as a .bos file so I can take a look?
Hi Antoine,
Your solution works well when a form is used. However, if the contract is instantiated through a webservice call, then we should use a constraint.
Would you have any other workaround?
Thank,s
FX
If you don’t use a form I would suggest to handle the validation as part of the process logic: store received 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 business variable and use it in gateway to route the process execution.
Bonitasoft empowers development teams with Bonita, the open-source and extensible platform to solve the most demanding process automation use cases. The Bonita platform accelerates delivery of complex applications with clear separation between capabilities for visual programming and for coding. Bonita integrates with existing solutions, orchestrates heterogeneous systems, and provides deep visibility into processes across the organization.