You are on page 1of 8

Page 1 of 8

Online interface between a Java application and SAP for real-


time budget control & account postings
Ram Honnavalli and Shantha Hanumantha Shastry
Hewlett-Packard Global Delivery India Center
Introduction
Procurement & Material Management System (MATS) is a custom solution based on J2EE
framework implemented for a leading automotive manufacturing company for at all its plants to
improve the efficiency of its purchasing and material management processes. A rich GUI combined
with easy to use functionalities are provided for its users from Production, Warehouse, Accounting
and Purchasing departments (see below Figure).
MATS system supports Warehouse with functionalities grouped under modules such as
Forecast, Procurement, Goods Receipt, Goods Issue, Invoice Verification, Physical Count and
Master Data and for Purchasing it supports with Sourcing module and for Accounting/Cost control
department, it supports with consolidated Reports on all the transactions. The accounting
department creates annual budgets by cost center in SAP and wants to track & control expenses in a
real-time manner. As a result, customer wanted a real-time interface between MATS and SAP to
support below key business requirements leading to an efficient control of expenses
Support real-time budget control : Provide a real-time budget control functionality from
MATS application with the budget information residing in SAP and budget being shared by
multiple systems. Support real-time budget control activities such as Check budget, Reserve
budget, Release budget reservation, Update budget and Consume budget.
Support real-time accounting postings : Provide ability for MATS to do General
Ledger(GL) or Accounts Payable(AP) posting on specific expense account codes for all
transactions in MATS such as Goods Receipt (GR), Goods Issue (GI), Invoice Confirmation
(INV), Credit Note (CN), Debit Note (DN), Retroactive CN(RCN), Retroactive DN(RDN),
Cancel Operation for all above objects, Physical stock adjustment
Support Physical count reconciliation: Provide volume update of records from MATS to
SAP for materials that have difference in stock count during annual cycle count process
Page 2 of 8
Solution Overview
Basically there were 3 ways of meeting the core requirement of real-time budget control in
SAP from an external system
1. Activate funds management function and use User-exits to access directly from MATS into
SAP to reserve, release, consume & update budgets
2. Use FI direct posting to consume the budget
3. Activate funds management and use Standard PR, PO objects to enable budget reserve,
Release, Consume & Update functions
First approach enables J2EE system to directly access business functions inside SAP and
commit budget consumption but in this case the J2EE system is expected to store the unique
document number given by SAP for each transaction and use it as a reference for future transactions
that refer the committed transactions. The major disadvantage with this approach was that the
standard tight integration within SAP between MM and FI/CO modules is not leveraged and it
involves lot of ABAP programming to achieve the real-time postings. The second approach supports
only budget consumption but gives no option for budget reservation and delayed consumption
leaving us with only third approach. The third approach, although results in duplication of
transaction objects, is a preferred option because it is leveraging the internal links between SAP-MM
and SAP-FI/CO modules for real-time GL/AP postings and also supports reservation of the budget
amount with delayed consumption.
Regarding the middleware needed to integrate J2EE and SAP, customer selected Websphere
Business Integrator as it provided a seamless integration with Websphere 5.0 & SAP and also
supported guaranteed message delivery. On the other hand, SAP Business Connector was fulfilling
the need to convert XML messages to SAP IDOC and back but did not support guaranteed message
delivery and a seamless integration to Websphere 5.0 development environment.
Solution details
The communication between MATS and SAP can be grouped into two major modes of
communication i.e. Synchronous and Asynchronous. Both modes of communication utilize similar
configurations in MQ.

Fig 1. MATS SAP interface using Websphere MQ
Page 3 of 8
Above figure (Fig 1) gives a brief overview of MQ technology and its components to better
understand their use in the integration between MATS and SAP. Some of the key design criteria
considered during the solution design were around handling of real-time requests for budget
available from over 1000+ production users working in 5 plants at various geographical locations,
complete elimination of manual key-in of transactions into SAP for accounting personnel and huge
volumes of transactions that need to be sent to SAP in short durations such as with regular cycle
count process. Below section gives a overview of each of the component involved in the integration
between MATS and SAP.
Java Trigger Program
In case of synchronous communication, requests are raised by application modules for
budget check. An XML message with data necessary to perform budget check is constructed by
application and placed in MQ queue. In case of Asynchronous communication, a java program
Send (Message Driven Bean) is triggered by invoking a JMS queue, at fixed time intervals. This
Send Program Is a java program that fetches objects from MATS database to be sent to SAP,
converts them into necessary XML format and puts them in the pre-configured MQ queue (Fig 2)
MQ Integrator
MQ Integrator picks messages from the source queues, convert messages to SAP XML (the
XML with SAP terminologies) format and routes into target pre-configured queues. On the reverse
flow from SAP to MATS, MQ Integrator picks the SAP XML message and converts into simple
XML message
Fig 2. Pre-configured queues in MQ Series
WBI adaptor for SAP
WBI adaptor picks the converted message from the Integrator queue and converts SAP XML
message into a SAP IDOC format. WBI adaptor further invokes an ALE Inbound Program which
will check for the MATS Document Number and in turn invokes the appropriate BAPI based on the
Message Type of the incoming IDOC.
Receive Program
In case of Asynchronous Communication, MQ Integrator puts the converted XML into
Initiation Queue which initiates the Trigger Monitor. A trigger Monitor is configured on MQ which
Page 4 of 8
reads a named initiation queue and then starts the Receive program on MATS associated with each
trigger message. The receive program does Two Phase Commit between the Queue Manager and
Database, which ensures that there is no loss of response messages In case of Successful result from
SAP, SAPDOCNUMBER is updated into MATS database. In case of Error the error messages are
logged into the database with the corresponding Object name
In case of Synchronous Communication for Budget Check, a java program Receive on the
MATS application will be listening to a pre-configured reply queue for a message that can match
original message id (created by Send program). The response for the request matching the message
id is picked within the time set in the property file. If there is no response, an appropriate message is
shown to the user on the screen. The Budget results from the XML are shown to the user on the
screen.
Budget consumption in SAP
Inside SAP, upon invocation of the appropriate BAPI, an unique document number gets
generated with successful creation of a corresponding object in SAP. Necessary posting takes place
in FI-GL and/or FI-AP. Budget reservation/consumption/release happens in CO-Funds management.
An outbound IDOC is created with SAP doc number, MATS Document No, SAP Message Type,
Error Message and SAP Status Code and placed in the WBI queue. WBI adaptor picks the IDOC
message from WBI queue and converts into SAP XML message
Below diagram illustrates how recording works and how the actual and budget values are
compared with one another in Funds Management. If you post to a particular G/L account, the
posting is simultaneously recorded in the commitment item stored in it. The control parameters
determine how and where the values are recorded in Funds Management.

Fig 3. Budget consumption in SAP
Page 5 of 8
Exception handling
Logging of the objects on hard disk has been used extensively to trace the messages in this
complex real-time MATS-WBI-SAP interface
All the error messages and exceptions in the application has been logged in database.
Reports are available to check for MATS objects that have been sent to SAP but have not
received a SAP document number
In case SAP has planned downtime, functionality is provided on MATS side to flag that
SAP is down. In such cases budget check will be done against a limit set in MATS
application.
WBI supports guaranteed message delivery so if SAP has an unplanned downtime, the
messages are queued and will be sent to SAP when it comes back up
Flow of data from MATS to SAP for a sample business object
Below section focuses on the technical aspect of the interface and explains the flow of data
objects from MATS to SAP via Websphere MQ. A sample transaction object - Goods Issue(GI) is
chosen to demonstrate the code involved in translating the MATS GI object into an XML message
and further convert into an IDOC to be used by respective BAPI in SAP.
Programfor placing a XML message into the MQ queue
This method will setup the Queues for operations and it should be called before the putMessage()
call. Additionally, MQ Setup parameters are read from a properties file.
public int setupMQS(int qID) throws MQException {
//Init some of the MQS parameters
int ret = -1;
MQEnvironment.hostname = hostname;
MQEnvironment.channel = channel;
MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,
MQC.TRANSPORT_MQSERIES);
MQEnvironment.port = this.tcpPort;
MQEnvironment.CCSID = ccsid;
qMgr = new MQQueueManager(qManager);
openOptions = MQC.MQOO_OUTPUT;
q = qMgr.accessQueue(qName,openOptions , null, null, null);
pmo = new MQPutMessageOptions();
return ret;
}//end of setupMQS
/** This will put message to the Q. It will return void */
public final void putMessage(String xml) throws java.io.IOException,MQException {
msg = new MQMessage();
msg.format = MQC.MQFMT_STRING;
msg.writeString(xml);
q.put(msg,pmo);
return ;
}
/*** This method will close the connection to the queue and queue manager */
public final void closeMQS() throws MQException {
if (q != null) { q.close(); }
if (qMgr != null) { qMgr.disconnect(); }
}
Page 6 of 8
MQ Integrator
Below tables depict the mapping between XML message structure and SAP IDOC message
structures and its core components for the sample transaction object Goods Issue. These XML
messages are converted into SAP compatible XML messages by custom logic based on ESQL
language. These converted SAP compatible XML messages are routed into the WBI queue for
IDOC conversion.
SAP IDOC control segment
Data Header segment
Data Item segment
Page 7 of 8
WBI adaptor
After converting the XML message into an IDOC message structure, the WBI adaptor calls
an ALE Inbound Program in SAP which invokes appropriate BAPIs (as shown below) based on the
Message Type of the incoming IDOC.
Material creation BAPI_MATERIAL_SAVEDATA
Purchase Request creation BAPI_REQUISITION_CREATE
Purchase Request cancellation BAPI_REQUISITION_DELETE
Purchase Order creation BAPI_PO_CREATE1
Goods Receipt for a PO MBGMCR02
Goods Issue MBGMCA02
Invoice Creation INCOMINGINVOICE_CREATE01
Physical Inventory count MATPHYINVCR02
Receive Program
This method gets the message from the queue where the sap has put the reply message Setup
parameters are read from a properties file .This method returns the message extracted from the
queue and also writes the message to a file.
public static String getMessageFromQueue() throws Exception
{
MQQueueManager qMgr = null;;
String Data= null;
String Temp;
String NewFile = "";
MQQueue q = null;
try
{
MQEnvironment.hostname = hostName;
MQEnvironment.channel = channel;
MQEnvironment.CCSID = iccsid;
MQEnvironment.port = tcpPort;
qMgr = new MQQueueManager(qManager);
int openOptions = MQC.MQOO_INPUT_SHARED +
MQC.MQOO_INQUIRE;
q = qMgr.accessQueue(qName,openOptions , null, null, null);
int depth = q.getCurrentDepth();
for(int i=0;i<depth;i++) {
MQMessage mes = new MQMessage();
MQGetMessageOptions gmo=new MQGetMessageOptions();
gmo.options=MQC.MQGMO_SYNCPOINT;
//Pick the message from the queue
q.get(mes,gmo);
byte[] strData = new byte[mes.getTotalMessageLength()];
mes.readFully(strData);
//read the data of the message
Data=new String(strData);
Temp=new String(mes.messageId);
String MesId= Temp.trim();
createFile(qMgr,Data,path);
String sMessage = path;
parseMessage(qMgr,Data);
Page 8 of 8
}//end of for-loop
}// end of try
catch(MQException ex)
{
ex.printStackTrace();
}
catch (java.io.IOException ex)
{
ex.printStackTrace();
}
finally {
try {
if( q != null )
q.close();
if( qMgr != null ) {
qMgr.disconnect(); qMgr.close();
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
return path;
}
Conclusion
The solution described above clearly demonstrated an effective way of integration between
SAP ERP and a J2EE based custom system using Websphere MQ series to meet business needs that
require real-time data availability with high reliability. The solution is also scalable for environments
with huge transaction volumes and large user base.
References
1. IBM Websphere Business Integration Adapters - Adapter for mySAP.com user guide
2. A B2B Solution using Websphere Business Integration v4.1 and Websphere Business
Connection v1.1 (ISBN:0738453358)