How to use SQL in Groovy script Order Management Extensions (validations) Fusion Cloud
Elshad Karimov
Founder of AppMillers | ERP Expert | Teaching more than 200k people How to Code.
Oracle Order Management Extensions is a robust tool that enables adding custom business logic to predefined events, such as “Save” or “Submit,” using the Groovy programming language.
The official documentation provides methods and examples for accessing database data through public view objects and extension methods. You can access the documentation here: Oracle Documentation.
In certain cases, it may be simpler to execute a direct SQL query against the database. For instance, you might copy notes or attachments from a customer to an order on save.
Public view objects used within extension Groovy code are built on the Oracle ADF framework, enabling direct access to the underlying database connection via the getDBTransaction() method.
Below is an example of executing an SQL statement during the order save event:
import oracle.apps.scm.doo.common.extensions.ValidationException;
import oracle.apps.scm.doo.common.extensions.Message;
def headerId = header.getAttribute("HeaderId").toString();
def vo = context.getViewObject("oracle.apps.scm.doo.publicView.analytics.HeaderPVO");
def rows = vo.findByKey(headerId, 1);
if (rows.size() == 0) return;
def originalHeader = rows[0];
def qvo = "select 5 V_NUM from dual";
def qvoAttr = originalHeader.getDBTransaction().createViewObjectFromQueryStmt(qvo);
qvoAttr.executeQuery();
def attrRec = qvoAttr.first();
if (attrRec.V_NUM == 5 ) {
throw new ValidationException( new Message(Message.MessageType.ERROR, "The number is: ${attrRec.V_NUM}") );
}
Explanation
Import Statements
import oracle.apps.scm.doo.common.extensions.ValidationException;
import oracle.apps.scm.doo.common.extensions.Message;
These imports bring in specific Oracle classes needed for handling custom exceptions and messages in the Groovy script.
Main Code
1. Retrieving HeaderId Attribute
def headerId = header.getAttribute("HeaderId").toString();
2. Accessing a Public View Object
def vo = context.getViewObject("oracle.apps.scm.doo.publicView.analytics.HeaderPVO");
3. Finding a Row by Key
def rows = vo.findByKey(headerId, 1);
4. Checking if Row Exists
if (rows.size() == 0) return;
领英推荐
5. Accessing the Original Header Row
def originalHeader = rows[0];
6. Defining a SQL Query for a Site-Level Attribute
def qvo = "select 5 V_NUM from dual";
7. Creating and Executing the Query View Object
def qvoAttr = originalHeader.getDBTransaction().createViewObjectFromQueryStmt(qvo);
qvoAttr.executeQuery();
8. Accessing the First Record of the Result Set
def attrRec = qvoAttr.first();
9. Checking the Attribute Value and Throwing an Exception
if (attrRec.V_NUM == 5 ) {
throw new ValidationException( new Message(Message.MessageType.ERROR, "The number is: ${attrRec.V_NUM}") );
}
This code essentially checks for a specific condition (in this case, if a number equals 5) and throws an exception to inform the user when the condition is met.
At Inlab Ltd, we provide comprehensive Oracle Fusion Cloud implementation services across all modules, including
Our expertise also extends to:
For more information or inquiries, please get in touch with us via:
WhatsApp: +994 51 689 16 04