Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Purpose of BAPI

Former Member
0 Kudos

Are BAPIs only used for transferring data from legacy to SAP ...or any other uses of BAPI??

Can I have the BAPI flow.. step by step instructions... ie.. how to start with..?

1 ACCEPTED SOLUTION

Former Member
0 Kudos

I expect the steps in the creation of a normal BAPI with example.. in your own words...

Please provide me an example and lemme know the steps in achieving it, in your words...( screenshots not needed).. thank u.

13 REPLIES 13

Former Member
0 Kudos

Hi,

SAP created the Business Framework to allow the technical integration and exchange of business data among SAP components and between SAP and non-SAP components. Important components of the Business Framework are the Business Application Programming Interfaces (BAPIs), which represent visible interfaces at the component boundaries and whose properties serve to integrate these components.

The integration can include both components within a local network and components that are connected with one another through the Internet.

BAPIs allow integration at the business level, not the technical level. This provides for greater stability in the link, and independence from the underlying communication technology.

You can get all the standard SAP BAPI functions with the transactions code 'BAPI'.

Purpose

When a customer implements a new SAP System, an important part of the migration process is copying objects from the legacy system. To transfer this mass data, each business object type in the SAP System currently has its own program for performing this initial data load with batch input, direct input, or other SAP developments (such as IS-B).

SAP developed the Data Transfer (DX) Workbench in Release 3.1 to give users a uniform startup screen for loading the different business object types, as well as simplified handling of standard files.

As of Release 4.6A, the DX Workbench allows BAPIs to be used for loading data into the SAP System. The use of BAPIs is increasingly important, because the previous techniques are only of limited or no use for data transfer from Release 4.6 onwards:

The batch input procedure cannot be used for the new Enjoy transactions because the batch input recorder does not support the controls used in these transactions. Until SAP implements a data transfer BAPI for these new transactions, SAP will continue to support the old transactions that do not yet use these controls. The disadvantage with this is that users have to deal with an additional transaction.

The administration transaction associated with the direct input method will no longer be supported from Release 5.0 onwards (at the latest). This means that existing direct input programs can be used, but the data transfer should be converted to BAPIs in the medium term.

Target Audience

The target audiences of this document are:

BAPI developers who want to implement BAPIs capable of supporting mass data

The document describes which steps are required to use these BAPIs with the DX Workbench. In addition, the programming models of all standardized BAPIs that are relevant for mass data transfer are discussed in detail.

Potential users of mass data-capable BAPIs who would like a conceptual overview of the process flow of the mass data transfer using BAPIs

Please note, however, that this document does not contain a description of any specific tool (for example the DX Workbench).

Implementation Considerations

To develop or use mass data-capable BAPIs, you need:

Basic knowledge of BAPIs, as described in General Introduction to BAPIs

Detailed knowledge of BAPI development, as described in the BAPI Programming Guide

Knowledge of the ABAP programming language and ABAP Workbench

General knowledge of the SAP System

See the following links.

http://www.sap-img.com/bapi.htm

http://help.sap.com/saphelp_nw04/helpdata/en/e0/9eb2370f9cbe68e10000009b38f8cf/frameset.htm

http://help.sap.com/saphelp_nw04/helpdata/en/4c/4c0e8a725311d396a80004ac96334b/frameset.htm

Regards,

Jagadish

Former Member
0 Kudos

Hi SRK,

Uses of BAPI

1)BAPI directly update the database instead through th screen flow.

2)BAPI support version upgrades...so need to worry about our previous uploads.

3)BAPI is a fast data trasnfer techinique.

4)Using BAPI we can acess data from non sap also

5.In case of bdc data transfer takes place from flat file into sap system ie the file existing in sap system to sap sytem

where is bapi's r remotly enabled function modules which are assigned to some business objects n used to transfer the data between different business partners who are using different systems other than sap.

Please check this link

Please check this link

Understanding BAPI:

http://articles.techrepublic.com.com/5100-10878_11-1051160.html

http://help.sap.com/saphelp_nw04/helpdata/en/4c/4c0e8a725311d396a80004ac96334b/frameset.htm

http://www.sapbapi.com/

http://help.sap.com/saphelp_nw04/helpdata/en/e0/9eb2370f9cbe68e10000009b38f8cf/frameset.htm

http://sap.ittoolbox.com/topics/t.asp?t=303&p=323&h1=303&h2=322&h3=323

\http://help.sap.com/saphelp_nw04/helpdata/en/4c/4c0e8a725311d396a80004ac96334b/content.htm

http://searchsap.techtarget.com/news/article/0,289142,sid21_gci948835,00.html

http://articles.techrepublic.com.com/5100-6329-1051160.html

Best regards,

raam

Former Member
0 Kudos

I expect the steps in the creation of a normal BAPI with example.. in your own words...

Please provide me an example and lemme know the steps in achieving it, in your words...( screenshots not needed).. thank u.

0 Kudos

0 Kudos

BAPI Step by step Guidance

BAPI stands for Business Application Programming Interface.

There are 5 different steps in BAPI.

- Create BAPI Structure

- Create BAPI Function Module or API Method.

- Create BAPI object

- Release BAPI Function Module.

- Release BAPI object.

Step1. Creating BAPI Structure:

- Go to <SE11>.

- Select Data Type & Enter a name.

- Click on Create.

- Note: Always BAPI should be in a development class with request number (Not Local Object).

- Select Structure & hit ENTER.

- Enter the fields from your database. Make sure that the first field is the Primary Key Field.

- Then SAVE & ACTIVATE.

Step 2. Creating BAPI module:

- Enter TR.CODE <SE37>.

- Before entering any thing, from the present screen that you are in, select the menu

Goto -> Function Groups -> Create Group.

Enter a name (Note: This name Must start with ZBAPI)

Let this screen be as it is and open another window and there, enter TR.CODE <SE80).

Click on the Third ICON that says Inactive Objects.

Select the group that you just created and click on Activate.

Notice that the group you created will disappear from the list of inactive objects.

- Go back to <SE37> screen and enter a name and hit <ENTER>. Then enter the group name that you just created and activated.

NOTE: When you release a function module the respective group will be attached to that particular application. It cannot be used for any other application. NEVER include an already existing group that is attached to another module.

Now click on the first Tab that says [ATTRIBUTES] and select the radio button that says remote-enabled module since we will be accessing this from any external system.

Then click on the second tab that says [IMPORT].

Enter a PARAMETER NAME, TYPE and the structure you created in the first step. Also select the check box u2018Pau2019. All remotely enabled functional modules MUST be Pa enabled, where Pa means u2018Passed by Valueu2019 and if you donu2019t select u2018Pau2019, then that means it will be passed by reference..

Then click on tab that says [EXPORT].

Enter the following as is in the first three fields

RETURN TYPE BAPIRETURN (These 3 field values are always same)

Here also select u2018Pau2019 meaning Pass by value.

Note: BAPIRETURN contains structure with message fields.

Then SAVE and ACTIVATE.

Step 3. Creating BAPI object:

- Enter Tr.Code <SWO1> (Note. It is letter u2018Ou2019 and not Zero).

- Enter a name and then click on create. Enter details.

NOTE: Make sure that that Object Type and Program name are SAME.

- Enter Application u2018Mu2019, if you are using standard table Mara. If you are using your own database then select u2018Zu2019 at the bottom.

- Then hit <ENTER>.

- Now we have to add u2018Methodsu2019. High light METHODS and then select the following from the menu:

Goto Utilities -> API Methods -> Add Methods.

- Enter function Module name and hit <ENTER>.

- Select the second FORWARD ARROW button (>)to go to next step.

- Check if every thing looks ok and again click on FORWARD ARROW button (>).

- Then select u2018YESu2019 and click on <SAVE>.

- Now on a different screen goto TR.CODE <SE37>. Enter Function Module name and select from the top menu Function Module -> Release -> Release.

- Goback to TR.CODE <SWO1>.

Here select the menu combination shown below in the same order.

- Edit -> Change Release Status -> Object Type Component -> To Implemented.

- Edit -> Change Release Status -> Object Type Component -> To Released.

- Edit -> Change Release Status -> Object Type -> To Implemented.

- Edit -> Change Release Status -> Object Type -> To Released.

- Then click on <SAVE>.

- Then click on Generate Button (4th button from left hand side looks like spinning wheel).

- Then Click on the button that says u2018PROGRAMu2019 to see the source code.

To check if this is present in work flow goto TR.CODE <BAPI>.

Here it shows business object repository.

- First click on the middle button and then select u201CALLu201D and hit ENTER.

- Goto tab [ALPHABETICAL] and look for the object that you created. This shows that the BAPI object has been created successfully.

0 Kudos

Hi Jagadish

U mentioned 5 steps to create a BAPI as follows:

· Create BAPI Structure

· Create BAPI Function Module or API Method.

· Create BAPI object

· Release BAPI Function Module.

· Release BAPI object.

If we use Standard BAPI then which r all the above steps that are required??

0 Kudos

Hi SRK,

· Create BAPI Structure

· Create BAPI Function Module or API Method.

· Create BAPI object

· Release BAPI Function Module.

· Release BAPI object.

These steps are for custom BAPI.

If u use standard bapi then no need of these steps. u can direclty use the standard bapi.

Best regards,

raam

0 Kudos

The separation of a BAPI definition from its actual implementation enables you to access a BAPI in two ways:

􀂾

You can call the BAPI in the BOR through object-oriented method calls

􀂾

You can make RFC calls to the function module on which the BAPI is based

GetList

Delivers a list of key fields objects that satisfies certain selection Criteria

GetDetail

Delivers detailed information of an object, whose complete key has been

Specified

CreateFromData

Generates new objects in R3 from key fields and returns information.

For more information click on the following link,

https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/200dd1cc-589e-2910-98a9-bb2c48b7...

http://help.sap.com/saphelp_nw04/helpdata/en/5a/ccb4bc808311d396b40004ac96334b/frameset.htm

Best Regards,

Jagadish

0 Kudos

Hi

thanks for the reply..

can u provide me with an example how to use standard BAPI.. with any scenario.. some thing like that...related to MM or SD..

what are all the pre-requisites to use standard BAPI??

0 Kudos

Hi,

Please check this like

&---------------------------------------------------------------------
*& Report YDM_PO_CREATE *
*& *
&---------------------------------------------------------------------
*& *
*& *
&---------------------------------------------------------------------
REPORT ydm_po_create.


Input File Declaration 
TYPES: BEGIN OF ty_input_file,
column1 TYPE char50,
column2 TYPE char50,
column3 TYPE char50,
column4 TYPE char50,
column5 TYPE char50,
column6 TYPE char50,
column7 TYPE char50,
column8 TYPE char50,
column9 TYPE char50,
column10 TYPE char50,
column11 TYPE char50,
column12 TYPE char50,
column13 TYPE char50,
column14 TYPE char50,
column15 TYPE char50,
column16 TYPE char50,
column17 TYPE char50,
column18 TYPE char50,
END OF ty_input_file.

DATA: i_input_file TYPE STANDARD TABLE OF ty_input_file,
wa_input_file TYPE ty_input_file.

CONSTANTS: c_path TYPE char20 VALUE 'C:\',
c_mask TYPE char9 VALUE ',.,..',
c_mode TYPE char1 VALUE 'O',
c_filetype TYPE char10 VALUE 'ASC',
c_x TYPE char01 VALUE 'X'.

PARAMETERS : p_fname LIKE rlgrap-filename.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.



Browse Presentation Server 
PERFORM f4_presentation_file.

START-OF-SELECTION..



Read presentation server file 
PERFORM f1003_upload_file.

IF NOT i_input_file[] IS INITIAL.
PERFORM split_data.
ENDIF.

*&---------------------------------------------------------------------
*& Form f4_presentation_file
*&---------------------------------------------------------------------
*& F4 Help for presentation server
*&---------------------------------------------------------------------

FORM f4_presentation_file .

CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
def_path = c_path
mask = c_mask
mode = c_mode
title = text-001
IMPORTING
filename = p_fname
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
OTHERS = 5.
IF sy-subrc 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

ENDFORM. " f4_presentation_file
*&---------------------------------------------------------------------
*& Form f1003_upload_file
*&---------------------------------------------------------------------
*& Upload File
*&---------------------------------------------------------------------
FORM f1003_upload_file .

DATA: lcl_filename TYPE string.

lcl_filename = p_fname.

CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = lcl_filename
filetype = c_filetype
has_field_separator = c_x
TABLES
data_tab = i_input_file
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
OTHERS = 17.
IF sy-subrc 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
EXIT.
ENDIF.

ENDFORM. " f1003_upload_file
&---------------------------------------------------------------------
*& Form split_data
&---------------------------------------------------------------------

Collect data for creating Purchase Order




--------------------------------------------------------------------------------
FORM split_data .

DATA: i_poitem TYPE STANDARD TABLE OF bapimepoitem,
i_poitemx TYPE STANDARD TABLE OF bapimepoitemx,
i_poitem_sch TYPE STANDARD TABLE OF bapimeposchedule,
i_poitem_schx TYPE STANDARD TABLE OF bapimeposchedulx,
i_acct_ass TYPE STANDARD TABLE OF bapimepoaccount,
i_acct_assx TYPE STANDARD TABLE OF bapimepoaccountx,
i_services TYPE STANDARD TABLE OF bapiesllc ,
i_srvacc TYPE STANDARD TABLE OF bapiesklc,
i_return TYPE STANDARD TABLE OF bapiret2,
wa_header TYPE bapimepoheader,
wa_headerx TYPE bapimepoheaderx,
wa_poitem TYPE bapimepoitem,
wa_poitemx TYPE bapimepoitemx,
wa_poitem_sch TYPE bapimeposchedule,
wa_poitem_schx TYPE bapimeposchedulx,
wa_acct_ass TYPE bapimepoaccount,
wa_acct_assx TYPE bapimepoaccountx,
wa_services TYPE bapiesllc,
wa_srvacc TYPE bapiesklc,
wa_return TYPE bapiret2,
ws_po TYPE bapimepoheader-po_number.

break gbpra8.

wa_services-pckg_no = 10.
wa_services-line_no = 1.
wa_services-outl_no = '0'.
wa_services-outl_ind = c_x.
wa_services-subpckg_no = 20.
APPEND wa_services TO i_services.

wa_srvacc-pckg_no = 10.
wa_srvacc-line_no = 1.
wa_srvacc-serno_line = 01.
wa_srvacc-serial_no = 01.
wa_srvacc-percentage = 100.
APPEND wa_srvacc TO i_srvacc.

LOOP AT i_input_file INTO wa_input_file.

IF wa_input_file-column2 EQ 'HD'.
wa_header-doc_type = wa_input_file-column3.
wa_header-creat_date = sy-datum.
wa_header-created_by = sy-uname.
wa_header-vendor = wa_input_file-column4.
PERFORM conversion_output USING wa_header-vendor
CHANGING wa_header-vendor.
wa_header-comp_code = 'DE03'.
wa_header-purch_org = 'DE03'.
wa_header-pur_group = 'DE1'.
wa_header-vper_start = wa_input_file-column9.
wa_header-vper_end = wa_input_file-column10.

wa_headerx-comp_code = c_x.
wa_headerx-doc_type = c_x.
wa_headerx-creat_date = c_x.
wa_headerx-created_by = c_x.
wa_headerx-vendor = c_x.
wa_headerx-purch_org = c_x.
wa_headerx-pur_group = c_x.
wa_headerx-vper_start = c_x.
wa_headerx-vper_end = c_x.
ENDIF.

IF wa_input_file-column2 EQ 'IT'.

wa_poitem-po_item = wa_input_file-column3.
wa_poitem-short_text = wa_input_file-column6.
wa_poitem-plant = wa_input_file-column8.
wa_poitem-quantity = '1'.
wa_poitem-tax_code = 'V0'.
wa_poitem-item_cat = 'D'.
wa_poitem-acctasscat = 'K'.
wa_poitem-matl_group = wa_input_file-column7.
wa_poitem-pckg_no = '10'.
APPEND wa_poitem TO i_poitem .

wa_poitemx-po_item = wa_input_file-column3.
wa_poitemx-po_itemx = c_x.
wa_poitemx-short_text = c_x.
wa_poitemx-plant = c_x.
wa_poitemx-quantity = c_x.
wa_poitemx-tax_code = c_x.
wa_poitemx-item_cat = c_x.
wa_poitemx-acctasscat = c_x.
wa_poitemx-matl_group = c_x.
wa_poitemx-pckg_no = c_x.
APPEND wa_poitemx TO i_poitemx.

wa_poitem_sch-po_item = wa_input_file-column3.
wa_poitem_sch-delivery_date = sy-datum.
APPEND wa_poitem_sch TO i_poitem_sch.

wa_poitem_schx-po_item = wa_input_file-column3.
wa_poitem_schx-po_itemx = c_x.
wa_poitem_schx-delivery_date = c_x.
APPEND wa_poitem_schx TO i_poitem_schx.

wa_acct_ass-po_item = 10.
wa_acct_ass-serial_no = 01.
wa_acct_ass-gl_account = '0006360100'.
wa_acct_ass-co_area = '1000'.
wa_acct_ass-costcenter = 'KC010000'.
APPEND wa_acct_ass TO i_acct_ass.

wa_acct_ass-po_item = 10.
wa_acct_ass-serial_no = 02.
wa_acct_ass-gl_account = '0006360100'.
wa_acct_ass-co_area = '1000'.
wa_acct_ass-costcenter = 'KC010000'.
APPEND wa_acct_ass TO i_acct_ass.

wa_acct_assx-po_item = 10.
wa_acct_assx-serial_no = 01.
wa_acct_assx-po_itemx = c_x.
wa_acct_assx-serial_nox = c_x.
wa_acct_assx-gl_account = c_x.
wa_acct_assx-co_area = c_x.
wa_acct_assx-costcenter = c_x.
APPEND wa_acct_assx TO i_acct_assx.

wa_acct_assx-po_item = 10.
wa_acct_assx-serial_no = 02.
wa_acct_assx-po_itemx = c_x.
wa_acct_assx-serial_nox = c_x.
wa_acct_assx-gl_account = c_x.
wa_acct_assx-co_area = c_x.
wa_acct_assx-costcenter = c_x.
APPEND wa_acct_assx TO i_acct_assx.

wa_services-pckg_no = 20.
wa_services-line_no = 2.
wa_services-service = wa_input_file-column9.
wa_services-quantity = '100'.
wa_services-gr_price = '100'.
wa_services-userf1_txt = wa_input_file-column13.
APPEND wa_services TO i_services.

wa_srvacc-pckg_no = 20.
wa_srvacc-line_no = 1.
wa_srvacc-serno_line = 02.
wa_srvacc-serial_no = 02.
wa_srvacc-percentage = 100.
APPEND wa_srvacc TO i_srvacc.
ENDIF.
ENDLOOP.

CALL FUNCTION 'BAPI_PO_CREATE1'
EXPORTING
poheader = wa_header
poheaderx = wa_headerx

POADDRVENDOR = 
TESTRUN = 
MEMORY_UNCOMPLETE = 
MEMORY_COMPLETE = 
POEXPIMPHEADER = 
POEXPIMPHEADERX = 
VERSIONS = 
NO_MESSAGING = 
NO_MESSAGE_REQ = 
NO_AUTHORITY = 
NO_PRICE_FROM_PO = 
IMPORTING
exppurchaseorder = ws_po

EXPHEADER = 
EXPPOEXPIMPHEADER = 
TABLES
return = i_return
poitem = i_poitem
poitemx = i_poitemx

POADDRDELIVERY = 
poschedule = i_poitem_sch
poschedulex = i_poitem_schx
poaccount = i_acct_ass

POACCOUNTPROFITSEGMENT = 
poaccountx = i_acct_assx

POCONDHEADER = 
POCONDHEADERX = 
POCOND = 
POCONDX = 
POLIMITS = 
POCONTRACTLIMITS = 
poservices = i_services
posrvaccessvalues = i_srvacc

POSERVICESTEXT = 
EXTENSIONIN = 
EXTENSIONOUT = 
POEXPIMPITEM = 
POEXPIMPITEMX = 
POTEXTHEADER = 
POTEXTITEM = 
ALLVERSIONS = 
POPARTNER = 
.

break gbpra8.

LOOP AT i_return INTO wa_return.

ENDLOOP.

ENDFORM. " split_data
&---------------------------------------------------------------------
*& Form conversion_output
&---------------------------------------------------------------------

Conversion exit input

--------------------------------------------------------------------------------
FORM conversion_output USING p_ip
CHANGING p_op.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = p_ip
IMPORTING
output = p_op.

ENDFORM. " conversion_output

best regards,

raam

0 Kudos

Sample Abap code on BAPI_PO_CHANGE

DELETING PURCHASE ORDERS

&----


*& Report ZMMR_DELETEPO *

&----


*& Author : Bikash Agarwal

*& Description : VTLS PO Change

*& Program Objective : Places a DELETION indicator for the PO

*& items given in the VTLS data

*& Remarks : NA

&----


REPORT ZMMR_DELETEPO NO STANDARD PAGE HEADING MESSAGE-ID zisb.

tables : zvtls_sap.

*C-- Types Declarations

TYPES : BEGIN OF tp_flatfile_vtls,

ebeln(10),

ebelp type ekpo-ebelp,

END OF tp_flatfile_vtls.

*=====================================================================

  • INTERNAL TABLES DECLARATION

*=====================================================================

DATA: t_flatfile_vtls TYPE tp_flatfile_vtls OCCURS 0 WITH HEADER LINE.

data : begin of t_sapdata occurs 0,

po like zvtls_sap-posap,

item like zvtls_sap-itemsap,

end of t_sapdata.

data : begin of t_flatfile_vtls1 occurs 0,

po(10),

item like zvtls_sap-itemsap,

end of t_flatfile_vtls1.

data : begin of t_update occurs 0,

mandt like zvtls_sap-mandt,

povtls like zvtls_sap-povtls,

itemvtls like zvtls_sap-itemvtls,

posap like zvtls_sap-posap,

itemsap like zvtls_sap-itemsap,

aedat like zvtls_sap-aedat,

paedt like zvtls_sap-paedt,

loekz like zvtls_sap-loekz,

end of t_update.

data : begin of t_poheader occurs 0,

po like zvtls_sap-posap,

end of t_poheader.

data : begin of t_poitem occurs 0,

po like zvtls_sap-posap,

item like zvtls_sap-itemsap,

end of t_poitem.

DATA : BEGIN OF T_MESSAGE OCCURS 0,

MSGTY,

MSGID(2),

MSGNO(3),

MSGTX(100),

PO like zvtls_sap-povtls,

item like zvtls_sap-itemvtls,

END OF T_MESSAGE.

DATA : BEGIN OF t_bapi_poheader OCCURS 0.

INCLUDE STRUCTURE bapimepoheader.

DATA : END OF t_bapi_poheader.

DATA : BEGIN OF t_bapi_poheaderx OCCURS 0.

INCLUDE STRUCTURE bapimepoheaderx.

DATA : END OF t_bapi_poheaderx.

DATA : BEGIN OF t_bapi_poitem OCCURS 0.

INCLUDE STRUCTURE bapimepoitem.

DATA : END OF t_bapi_poitem.

DATA : BEGIN OF t_bapi_poitemx OCCURS 0.

INCLUDE STRUCTURE bapimepoitemx.

DATA : END OF t_bapi_poitemx.

DATA : BEGIN OF t_bapireturn OCCURS 0.

INCLUDE STRUCTURE bapiret2.

DATA : END OF t_bapireturn.

*=====================================================================

  • V A R I A B L E S

*=====================================================================

DATA: w_success(6) TYPE n,

w_bklas like t023-bklas,

w_curryear(4),

w_begda like sy-datum,

w_endda like sy-datum,

w_begyr(4),

w_endyr(4),

w_currmon(2),

w_assetclass like ankt-anlkl,

w_price type p,

w_recordsap type i,

w_povtls(10),

w_count type i.

DATA: w_filepath TYPE rlgrap-filename,

w_rc TYPE sy-subrc,

w_sscrfields_ucomm1 TYPE sscrfields-ucomm,

w_file1 TYPE string,

w_file2 TYPE FILENAME-FILEINTERN.

*=====================================================================

  • C O N S T A N T S

*=====================================================================

CONSTANTS: c_x TYPE c VALUE 'X',

c_hyp TYPE c VALUE '-',

c_err TYPE bdc_mart VALUE 'E'.

CONSTANTS: c_slash(1) TYPE c VALUE '/',

c_hash(1) TYPE c VALUE '#',

c_pipe TYPE c VALUE '|',

c_1 TYPE i VALUE 1,

c_zero TYPE n VALUE '0',

c_rg1(3) TYPE c VALUE 'rg1',

c_gr3(3) TYPE c VALUE 'GR3',

c_gr2(3) TYPE c VALUE 'GR2',

c_e(1) TYPE c VALUE 'E',

c_filepath(8) TYPE c VALUE '/interf/',

c_filetype(10) TYPE c VALUE 'ASC'.

CONSTANTS : c_bapimepoheaderx TYPE x030l-tabname

VALUE 'bapimepoheaderx',

c_bapimepoitem TYPE x030l-tabname

VALUE 'bapimepoitem',

c_bapimepoaccount TYPE x030l-tabname

VALUE 'bapimepoaccount',

c_t_bapi_poheader(15) TYPE c

VALUE 't_bapi_poheader',

c_t_bapi_poitem(13) TYPE c

VALUE 't_bapi_poitem',

c_t_bapi_poitemx(14) TYPE c

VALUE 't_bapi_poitemx',

c_t_bapi_poheaderx(16) TYPE c

VALUE 't_bapi_poheaderx'.

CLASS cl_abap_char_utilities DEFINITION LOAD.

CONSTANTS:con_tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab.

*======================================================================

  • SELECTION SCREEN

*======================================================================

SELECTION-SCREEN BEGIN OF BLOCK inputpath WITH FRAME TITLE text-001.

SELECTION-SCREEN : BEGIN OF BLOCK blk2 WITH FRAME TITLE text-002.

PARAMETERS : p_fore RADIOBUTTON GROUP rg1

USER-COMMAND pc,

p_back RADIOBUTTON GROUP rg1 DEFAULT 'X'.

SELECTION-SCREEN : END OF BLOCK blk2.

SELECTION-SCREEN : BEGIN OF BLOCK blk1 WITH FRAME TITLE text-003.

PARAMETERS : p_file1 LIKE rlgrap-filename OBLIGATORY MODIF ID gr2.

PARAMETERS : p_afile1 LIKE rlgrap-filename OBLIGATORY MODIF ID gr3.

SELECTION-SCREEN : END OF BLOCK blk1.

SELECTION-SCREEN END OF BLOCK inputpath.

*C-- Initialization Event

INITIALIZATION.

CLEAR w_filepath.

CONCATENATE c_filepath sy-sysid c_slash sy-mandt c_slash INTO

w_filepath.

CONDENSE w_filepath NO-GAPS.

p_file1 = text-008.

p_afile1 = text-009.

*======================================================================

  • SELECTION SCREEN EVENTS

*======================================================================

*C-- Selection Screen Output

AT SELECTION-SCREEN OUTPUT.

IF p_fore = c_x.

w_sscrfields_ucomm1 = space.

ELSE.

w_sscrfields_ucomm1 = c_rg1.

ENDIF.

LOOP AT SCREEN.

*C--Modify selection screen if presentation

*C--or application server radio button is chosen

IF w_sscrfields_ucomm1 = space.

IF screen-group1 = c_gr3.

screen-active = c_zero.

ENDIF.

ELSE.

IF screen-group1 = c_gr2.

screen-active = c_zero.

ENDIF.

ENDIF.

if screen-name = 'P_AFILE1'.

screen-input = 0.

ENDIF.

MODIFY SCREEN.

ENDLOOP.

*C-- Selection Screen VALUE-REQUEST FOR File path

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file1.

IF p_fore EQ c_x.

CALL FUNCTION 'F4_FILENAME'

EXPORTING

program_name = syst-cprog

dynpro_number = syst-dynnr

IMPORTING

file_name = p_file1.

ENDIF.

*C-- At Start of the Selection Process

START-OF-SELECTION.

IF p_fore EQ c_x.

w_file1 = p_file1.

ELSE.

w_file2 = p_afile1.

ENDIF.

IF p_fore EQ c_x. " Presentaion Server

*C--Validations for the input files

PERFORM validate_pre_file USING p_file1.

*C-- Load the contents of the input file into the internal table

PERFORM upload_file TABLES t_flatfile_vtls

USING w_file1

CHANGING w_rc.

IF w_rc <> 0.

MESSAGE s006 DISPLAY LIKE c_e.

ENDIF.

ELSE. " Application Server

*C--Validations for the input files

PERFORM validate_app_file USING w_file2.

*C-- Load the contents of the input file into the internal table

PERFORM upload_file_app TABLES t_flatfile_vtls

USING w_file2

CHANGING w_rc.

ENDIF.

loop at t_flatfile_vtls.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

EXPORTING

input = t_flatfile_vtls-ebeln

IMPORTING

output = t_flatfile_vtls1-po.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

EXPORTING

input = t_flatfile_vtls-ebelp

IMPORTING

output = t_flatfile_vtls1-item.

append t_flatfile_vtls1.

clear t_flatfile_vtls1.

endloop.

perform get_podata.

loop at t_poheader.

perform move_to_bapi.

perform call_bapi.

endloop.

PERFORM STORE_MESSAGES TABLES T_MESSAGE.

*&----


*

*& Form validate_pre_file

*&----


*

  • Routine to validate presentation server file path.

*----


*

  • -->fp_name text

*----


*

FORM validate_pre_file USING fp_name TYPE rlgrap-filename.

DATA : l_result,

l_filename TYPE string.

l_filename = fp_name.

CLEAR l_result.

CALL METHOD cl_gui_frontend_services=>file_exist

EXPORTING

file = l_filename

RECEIVING

result = l_result

EXCEPTIONS

cntl_error = 1

error_no_gui = 2

wrong_parameter = 3

not_supported_by_gui = 4

OTHERS = 5.

IF sy-subrc <> 0.

MESSAGE s007 DISPLAY LIKE c_e.

LEAVE LIST-PROCESSING.

ELSEIF l_result IS INITIAL.

MESSAGE s008 DISPLAY LIKE c_e.

LEAVE LIST-PROCESSING.

ENDIF.

ENDFORM. " validate_pre_file_hdr

*&----


*

*& Form validate_app_file

*&----


*

  • text - Checks if the path entered and filename is correct

*----


*

FORM validate_app_file USING fp_file TYPE FILENAME-FILEINTERN.

data : l_fname(60).

CALL FUNCTION 'FILE_GET_NAME'

EXPORTING

LOGICAL_FILENAME = FP_FILE

OPERATING_SYSTEM = SY-OPSYS

IMPORTING

FILE_NAME = L_FNAME

EXCEPTIONS

FILE_NOT_FOUND = 1

OTHERS = 2.

IF SY-SUBRC = '0'.

OPEN DATASET L_FNAME FOR INPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc NE 0.

MESSAGE s007 DISPLAY LIKE c_e.

ELSE.

CLOSE DATASET l_fname.

ENDIF.

ENDIF.

ENDFORM. " validate_app_file

*&----


*

*& Form upload_file

*&----


*

  • Routine to upload data from file to tables.

*----


*

  • -->P_fp_flatfile

  • -->P_fp_file

  • <--P_fp_rc

*----


*

FORM upload_file TABLES fp_flatfile

USING fp_file TYPE string

CHANGING fp_rc TYPE sy-subrc.

IF fp_flatfile[] IS INITIAL.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = fp_file

filetype = c_filetype

has_field_separator = c_x

TABLES

data_tab = fp_flatfile

EXCEPTIONS

file_open_error = 1

file_read_error = 2

no_batch = 3

gui_refuse_filetransfer = 4

invalid_type = 5

no_authority = 6

unknown_error = 7

bad_data_format = 8

header_not_allowed = 9

separator_not_allowed = 10

header_too_long = 11

unknown_dp_error = 12

access_denied = 13

dp_out_of_memory = 14

disk_full = 15

dp_timeout = 16

OTHERS = 17.

MOVE sy-subrc TO fp_rc.

ENDIF.

ENDFORM. " upload_file

&----


*& Form upload_file_app

&----


  • text

----


  • -->FP_FLATFILEtext

  • -->FP_FILE text

  • -->FP_RC text

----


FORM upload_file_app TABLES fp_flatfile

USING fp_file TYPE FILENAME-FILEINTERN

CHANGING fp_rc TYPE sy-subrc.

DATA: l_string TYPE tedata-data.

DATA: wa_data_file TYPE tp_flatfile_vtls,

l_wllength TYPE i,

FNAME(60).

CALL FUNCTION 'FILE_GET_NAME'

EXPORTING

LOGICAL_FILENAME = FP_FILE

OPERATING_SYSTEM = SY-OPSYS

IMPORTING

FILE_NAME = FNAME

EXCEPTIONS

FILE_NOT_FOUND = 1

OTHERS = 2.

IF SY-SUBRC = 0.

OPEN DATASET FNAME FOR INPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc NE 0.

  • *C-- commented by Bikash

  • MESSAGE s107(yaero_ps) DISPLAY LIKE c_e.

message e008.

ELSE.

DO.

CLEAR: l_string.

READ DATASET FNAME INTO l_string LENGTH l_wllength.

IF sy-subrc NE 0.

EXIT.

ELSE.

SPLIT l_string AT con_tab INTO wa_data_file-ebeln

wa_data_file-ebelp.

APPEND wa_data_file TO fp_flatfile.

ENDIF.

ENDDO.

CLOSE DATASET FNAME.

ENDIF.

ENDIF.

ENDFORM. " upload_file_app

&----


*& Form get_podata

&----


  • text

----


form get_podata.

select *

into table t_update

from zvtls_sap

for all entries in t_flatfile_vtls1

where itemvtls = t_flatfile_vtls1-item

and povtls = t_flatfile_vtls1-po.

sort t_update by posap itemsap.

loop at t_update.

at new posap.

t_poheader-po = t_update-posap.

append t_poheader.

clear t_poheader.

endat.

t_poitem-po = t_update-posap.

t_poitem-item = t_update-itemsap.

append t_poitem.

clear t_poitem.

t_update-paedt = sy-datum.

t_update-loekz = 'X'.

modify t_update.

endloop.

modify zvtls_sap from table t_update.

endform. "get_podata

&----


*& Form move_to_bapi

&----


  • text

----


form move_to_bapi.

t_bapi_poheader-po_number = t_poheader-po.

CLEAR t_bapi_poheaderx.

PERFORM fill_check_structure USING c_bapimepoheaderx

c_t_bapi_poheader

c_t_bapi_poheaderx

c_x.

refresh : t_bapi_poitem,t_bapi_poitemx.

loop at t_poitem where po = t_poheader-po.

clear t_bapi_poitem.

t_bapi_poitem-po_item = t_poitem-item.

t_bapi_poitem-delete_ind = 'X'.

CLEAR t_bapi_poitemx.

PERFORM fill_check_structure USING c_bapimepoitem

c_t_bapi_poitem

c_t_bapi_poitemx

c_x.

t_bapi_poitemx-po_item = t_poitem-item.

t_bapi_poitemx-po_itemx = c_x.

APPEND t_bapi_poitem.

APPEND t_bapi_poitemx.

clear t_bapi_poitem.

clear t_bapi_poitemx.

endloop.

endform. "move_to_bapi

*&----


*

*& Form call_bapi

*&----


*

  • This form Routine is used to commit the data records

----


FORM call_bapi .

DATA : l_msgty TYPE c,

l_msgid(2) TYPE c,

l_msgno(3) TYPE c,

l_msgtx(100) TYPE c,

l_errflag TYPE c.

CLEAR: t_bapireturn.

REFRESH: t_bapireturn.

CALL FUNCTION 'BAPI_PO_CHANGE'

EXPORTING

PURCHASEORDER = T_POHEADER-PO

POHEADER = T_BAPI_POHEADER

POHEADERX = T_BAPI_POHEADERX

TABLES

RETURN = T_BAPIRETURN

POITEM = T_BAPI_POITEM

POITEMX = T_BAPI_POITEMX.

READ TABLE t_bapireturn WITH KEY type = c_err TRANSPORTING NO FIELDS.

IF sy-subrc NE 0.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

EXPORTING

wait = c_x.

ENDIF.

*C-- Write messages

WRITE: / 'PO Number', t_poheader-po.

clear : t_update,w_povtls.

read table t_update with key posap = t_poheader-po.

w_povtls = t_update-povtls.

CLEAR l_errflag.

LOOP AT t_bapireturn.

CLEAR: l_msgty, l_msgid, l_msgno, l_msgtx.

l_msgty = t_bapireturn-type.

l_msgid = t_bapireturn-id.

l_msgno = t_bapireturn-number.

l_msgtx = t_bapireturn-message.

WRITE: / l_msgty, l_msgid, l_msgno, l_msgtx.

if l_msgtx cs t_poheader-po.

w_count = w_count + 1.

loop at t_update.

if sy-tabix = w_count.

t_message-item = t_update-itemvtls.

endif.

endloop.

endif.

t_message-msgty = l_msgty.

t_message-msgid = l_msgid.

t_message-msgno = l_msgno.

t_message-msgtx = l_msgtx.

t_message-po = w_povtls.

append t_message.

clear t_message.

IF l_msgty EQ c_err.

l_errflag = c_x.

ENDIF. " l_msgty EQ 'E'

ENDLOOP.

ULINE.

IF l_errflag NE c_x.

w_success = w_success + 1.

ENDIF. " l_errflag NE C_X

endform. "call_bapi

*&----


*

*& Form fill_check_structure

*&----


*

  • This form Routine will check whether the specified structure

  • exist/active

*----


*

FORM fill_check_structure USING fp_tabname TYPE any

fp_orgtabname TYPE any

fp_chktabname TYPE any

fp_check TYPE c.

FIELD-SYMBOLS : <fs_chk>, <fs_org>.

DATA: l_char1(61) TYPE c,

l_char2(61) TYPE c.

DATA: BEGIN OF tl_nametab OCCURS 60.

INCLUDE STRUCTURE x031l.

DATA: END OF tl_nametab.

REFRESH tl_nametab.

CALL FUNCTION 'RFC_GET_NAMETAB'

EXPORTING

tabname = fp_tabname

TABLES

nametab = tl_nametab

EXCEPTIONS

table_not_active = 1

OTHERS = 2.

IF sy-subrc <> 0.

CLEAR tl_nametab.

ENDIF.

LOOP AT tl_nametab.

CLEAR: l_char1, l_char2.

CONCATENATE fp_chktabname c_hyp tl_nametab-fieldname INTO l_char1.

ASSIGN (l_char1) TO <fs_chk>.

CONCATENATE fp_orgtabname c_hyp tl_nametab-fieldname INTO l_char2.

ASSIGN (l_char2) TO <fs_org>.

IF <fs_org> IS NOT INITIAL.

<fs_chk> = fp_check.

ENDIF.

ENDLOOP.

ENDFORM. " fill_check_structure

&----


*& Form STORE_MESSAGES

&----


  • text

----


  • -->FP_MESSAGEStext

----


FORM STORE_MESSAGES TABLES FP_MESSAGES STRUCTURE T_MESSAGE.

DATA: wl_output_data LIKE t_MESSAGE.

DATA: l_catstr TYPE string.

DATA: l_fieldvalue TYPE string.

DATA: l_index TYPE i VALUE 1.

DATA: L_FNAME(60).

FIELD-SYMBOLS <fs>.

CLEAR l_catstr.

CALL FUNCTION 'FILE_GET_NAME'

EXPORTING

LOGICAL_FILENAME = '/USR/SAP/VTLS/POCHANGE/LOG'

OPERATING_SYSTEM = SY-OPSYS

IMPORTING

FILE_NAME = L_FNAME

EXCEPTIONS

FILE_NOT_FOUND = 1

OTHERS = 2.

IF SY-SUBRC = '0'.

IF fp_messages[] IS NOT INITIAL.

OPEN DATASET L_FNAME FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc NE 0.

LEAVE LIST-PROCESSING.

ELSE.

LOOP AT fp_messages INTO wl_output_data.

DO.

ASSIGN COMPONENT l_index OF STRUCTURE wl_output_data TO <fs>.

IF sy-subrc <> 0.

EXIT.

ENDIF.

MOVE <fs> TO l_fieldvalue.

IF l_catstr IS NOT INITIAL.

CONCATENATE l_catstr l_fieldvalue INTO l_catstr SEPARATED

BY con_tab.

ELSE.

MOVE l_fieldvalue TO l_catstr.

ENDIF.

l_index = l_index + c_1.

CLEAR l_fieldvalue.

CLEAR <fs>.

ENDDO.

l_index = c_1.

TRANSFER l_catstr TO L_FNAME .

CLEAR wl_output_data.

CLEAR l_catstr.

ENDLOOP.

CLOSE DATASET L_FNAME.

ENDIF.

ENDIF.

ENDIF.

ENDFORM. "STORE_MESSAGES

Former Member
0 Kudos

hi,

BAPI provides different FM for downloading & uploading data from any file on presentation server.

Its very fast technique for downloading data & uploading data from any file.

see this link

http://www.sap-img.com/abap/bapi-step-by-step-guidance.htm

reward points if useful

thx.

twinkal.

0 Kudos

Hi Patel

U gave me the same link... pls go thru the above discussion carefully..

thanks anyway...