This is a three part series. It is about using Apex for integration work. It was presented at Oracle User Group Norway (OUGN) on the 15.05.25. The first part focuses on creating your own REST service. This service retrieves data from a third party and stores it in Apex. Later, we will relay it to Oracle ERP.

The Second part will detail how to use Oracle ERP integration service to upload data. In this example we will upload currency data to ERP. The third part will be about how to use Oracle Workflow to orchestrate the integration.

ERP Integration Service is a great service that can do almost any integration against ERP Cloud. If there is a REST or SOAP service that can do the job, of course I prefer that. But sometimes the REST and SOAP services in Oracle ERP can have limitations, that ERP Integration service don’t have.

When creating customers and suppliers with a common Party, it is not possible to connect the supplier to an existing party or customer. This is because the Registry ID is read only in the REST API for suppliers. The FBDI for suppliers allow for setting the Registry ID.

Creating the REST API

In SQL Workshop, Select RESTful Services
Click Configure and set Schema Alias and Authorization like in image below. En result should be 3 green check marks like above.
Enable “RESTful Access”. Set Schema alias to something meaningful to protect the actual schema name. Enable authorization for metadata access.
For this integration we will create two REST services. The dailyRate service is for third party to send it’s payload with currencies. The erpCallBack is the endpoint which Oracle ERP will call back to Apex to tell how the job went. Dailyrate is protected by privilege and will need authentication to be called. The callback service is open, but will be secured with a json web token (jwt).
On the callback Service, you can set up Origins Allowed. This setting protects it against calls from places other than the ERP. This setup increases security.
Here I have created a privilege and protected the dailyRate service with it. By moving that service to the right side, it needs authentication to be called. ERP callback is still on the left side, and will be unprotected.
A Module can have many resource templates.
Underneath a URI template it can only be one of each http method (get, post, put, delete). In this example my uri template have a bad name. Should only have been “batch”. Then it would have been post batch, get batch, put batch and delete batch as methods. Since I only have post, in this example, i put it in the uri.

In the code above some of the code for the rest service can be viewed. Some of the SQL bind variables, like :body_text and :status_code, are implicit variables. They can be assigned and referenced without any declaration. List of implicit parameters below.

We will use the :body_text, :status_code and the declared parameter :errmsg in this integration.

Start with assigning the :body_text to the v_data variable. A package call to the “rest_store_currency_data_json” procedure with the payload, placholder for batchid, status_code and error message.

When returning from the procedure call we check the status code. If it’s 201 we return the batchid for the callers reference. If any exceptions occur inside the procedure we return 400 and the error message.

The callback service

In the above window we can see the parameter token with bind variable X01 defined as a IN parameter. The URL used in the callback contains a parameter named X01. This parameter contains the JWT token we sent to Oracle when calling the ERP-service. We assign the token to v_token by reading the X01 parameter. The response body is assigned to v_data. The procedure store_erp_callback will evaluate the token and throw a 401 Access Denied, if the token is not valid. Else 201 created will be returned to Oracle ERP

The above service is open, but protected with the JSON token. A random caller trying to call this service will struggle a lot. They will find it difficult to understand how to call this service. Generating a valid token is also challenging. I think it’s possible to set up SAML authentication also. I have not figured out how to do that yet. Will for sure blog about it, if I find out.

Creating an Oauth client for the dailyRate service.

One benefit of REST enabling the schema is access to the SQL Developer Web menu item.
We will go here to create the Oauth client to authenticate against the dailyRate service.
In the hamburger menu, choose REST, then Security | Oauth Clients. Press “Create Oauth Client”
Give the client a name, a description and support email. On the privileges tab choose the privilege used when creating the REST services. When you press create, copy the “Client secret”. It will not show again. This is new in 24.2. Earlier it was accessible all the time.
After the client is created it’s possible to copy the client ID. Also possible to rotate the client secret and edit the client.
Here I’m testing the client using Bruno. For the body choose “Form URL Encoded”. This is the same if you use Postman or other clients. Key=grant_type and value = client_credentials
Under Auth the Client id and Client Secret is input under username and password. When running the payload the access token of type bearer, valid for one hour, will be returned.

Receiving the currency payload via our REST api

The payload will be of type JSON.

{
    "operationName": "uploadCurrencyData",
    "sourceRef": "241e89df-073f-48b1-88ac-88546370e4aa",
    "rates": [
        {
            "fromCurrency": "GBP",
            "toCurrency": "NOK",
            "fromConversionDate": "2024/03/19",
            "toConversionDate": "2024/03/28",
            "userConversionType": "Spot",
            "conversionRate": 13.550361599999999999999999999,
            "inverseConversionRate": 0.0737987685878434417573033623
        },
        {
            "fromCurrency": "SEK",
            "toCurrency": "NOK",
            "fromConversionDate": "2024/03/19",
            "toConversionDate": "2024/03/28",
            "userConversionType": "Spot",
            "conversionRate": 1.00,
            "inverseConversionRate": 1.00
        },
        {
            "fromCurrency": "CAD",
            "toCurrency": "NOK",
            "fromConversionDate": "2024/03/19",
            "toConversionDate": "2024/03/28",
            "userConversionType": "Spot",
            "conversionRate": 7.8543095185430951854309518543,
            "inverseConversionRate": 0.12731863923100030039050766
        },
        {
            "fromCurrency": "CHF",
            "toCurrency": "NOK",
            "fromConversionDate": "2024/03/19",
            "toConversionDate": "2024/03/28",
            "userConversionType": "Spot",
            "conversionRate": 11.997747494087171978826444419,
            "inverseConversionRate": 0.0833489786722739561429858816
        }
    ]
}
Payload will be added to the cur_batch_rate and cur_daily_rate_interface tables. Rate codes are downloaded from Oracle ERP. A foreign key constraint makes sure no illegal rates can be submitted. There are also som unique constraints to handle unique dates for a currency within a payload.
Code for parsing the payload, part I. Code is stripped for some checks for validity of the json payload and also for code to start workflow. The workflow will be covered in part III
Code for parsing the payload part II. Code is stripped for some checks for validity of the json payload and also for code to start workflow. The workflow will be covered in part III

When payload is parsed, inserted and committed the p_status_code will be set to 201-created. On commit a trigger will evaluate a lot of currency rules. If an exceptions occurs, insert will roll back and p_status_code will be set to 400-error.

Oracle ERP enforces these rules for a currency payload:
Code  Description
VF01  The Rate Type is invalid.
VF02  Date range between the From Date and To Date should not exceed 366 days.
VF03  The Conversion Rate must be a positive number.
VF04  The Inverse Conversion Rate must be a positive number.
VF05  The From Currency is invalid.
VF06  The From Currency is disabled.
VF07  The From Currency can not be STAT (statistical).
VF08  The From Currency is out-of-date.
VF09  The From Currency can not be an EMU currency.
VF10  The To Currency is invalid.
VF11  The To Currency is disabled.
VF12  The To Currency can not be STAT.
VF13  The To Currency is out-of-date.
VF14  The To Currency can not be an EMU currency.
VF15  Duplicate Row (an overlapping date range was found).
VF18  Self Rate Row not allowed.

All of these rules are enforced in foreign key constraint, unique constraints and trigger code. When third party call out service they will get immediate feedback on the payload. Our service is synchronous service while the Oracle ERP service is asynchronous.

Trigger part I: Check for overlapping currencies and dates within the same batch.
Trigger part II: Checking that the currency is active in Oracle ERP. Checking that it has a Currency flag in Oracle ERP. Both test for the from currency
Trigger part III: Checking that the currency is active in Oracle ERP. Checking that it has a Currency flag in Oracle ERP. Both test for the to currency
Trigger part IV: Checking that rate type is Spot, that date range is not more than 366 days. Checking that conversion rate and inverse rate is positive. Checking that to and from currency are different.

That was the end of part I. We have created two REST services. Made a Oauth client and tested it. We have implemented the daily rate service with tables, package routine for inserting and a trigger for validating the currencies.

When testing the service it returns a msgId on successful insert.
When violating a currency rule we immediately get an error back.

Leave a Reply


Oppdag mer fra Standal.blog

Abonner for å få de siste innleggene sendt til din e-post.

Håvard Standal avatar

Published by

Discover more from Standal.blog

Subscribe now to keep reading and get access to the full archive.

Continue reading