Migrating Material Master Data Through BAPI
-
In the previous part, we discussed how we can transfer data to SAP using BAPI.
-
In this part we will see the implementation part step by step using BAPI.
-
We will migrate Material Master data through BAPI.
-
Suppose, I have this above material data and I want to transfer it to my SAP and store it in the database using BAPI.
Solution :-
-
Step 1 :- Create a executable program in ABAP Editor ( SE38 ).
-
Step 2 :- Create a type for the above file and a corresponding internal table and work area for the file.
-
Step 3 :- Create a parameter for file and also create a local variable of type string in which we will store our file.
-
Step 4 :- In At Selection Screen on value request, use F4_FILENAME to navigate to local system and select the file.
-
Step 5 :- In Start Of Selection, we will use GUI_UPLOAD function module to upload data into our SAP system.
-
Now, Our data from our local file is stored into LT_MATERIAL internal table. So, we will pass our each record one at a time into the BAPI_MATERIAL_SAVEDATA.
- We will pass the data into the parameters of this BAPI.
- then we will mark them as true that we have passed them.
- At last BAPI will return a message which will be of type BAPIRET2, we will stored it.
-
step 6 :- Now we can display the return table in form of ALV using CL_SALV_TABLE class.
Code :-
*&---------------------------------------------------------------------*
*& Report ZAR_MATERIAL_BAPI
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zar_material_bapi.
TYPES : BEGIN OF ty_material,
matnr TYPE matnr,
mbrsh TYPE mbrsh,
mtart TYPE mtart,
maktx TYPE maktx,
meins TYPE meins,
END OF ty_material.
DATA : lt_material type table of ty_material,
ls_material type ty_material.
DATA : ls_headdata type BAPIMATHEAD.
DATA : ls_clientdata type BAPI_MARA.
DATA : ls_clientdatax type BAPI_MARAX.
DATA : lt_desc type table of BAPI_MAKT.
DATA : ls_Desc type BAPI_MAKT.
DATA : ls_return type BAPIRET2,
lt_return type table of BAPIRET2.
DATA : lv_file type string.
PARAMETERS : p_file type localfile.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
PROGRAM_NAME = SYST-CPROG
DYNPRO_NUMBER = SYST-DYNNR
FIELD_NAME = ' '
IMPORTING
FILE_NAME = p_file
.
lv_file = p_file.
START-OF-SELECTION.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = lv_file
* FILETYPE = 'ASC'
HAS_FIELD_SEPARATOR = 'X'
tables
data_tab = lt_material
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
.
loop at lt_material into ls_material.
ls_headdata-material = ls_material-matnr.
ls_headdata-ind_sector = ls_material-mbrsh.
ls_headdata-matl_type = ls_material-mtart.
ls_headdata-basic_view = 'X'.
ls_clientdata-base_uom = ls_material-meins.
ls_clientdatax-base_uom = 'X'.
ls_desc-langu = sy-langu.
ls_desc-matl_desc = ls_material-maktx.
append ls_desc to lt_desc.
clear ls_desc.
CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
EXPORTING
headdata = ls_headdata
CLIENTDATA = ls_clientdata
CLIENTDATAX = ls_clientdatax
* PLANTDATA =
* PLANTDATAX =
* FORECASTPARAMETERS =
* FORECASTPARAMETERSX =
* PLANNINGDATA =
* PLANNINGDATAX =
* STORAGELOCATIONDATA =
* STORAGELOCATIONDATAX =
* VALUATIONDATA =
* VALUATIONDATAX =
* WAREHOUSENUMBERDATA =
* WAREHOUSENUMBERDATAX =
* SALESDATA =
* SALESDATAX =
* STORAGETYPEDATA =
* STORAGETYPEDATAX =
* FLAG_ONLINE = ' '
* FLAG_CAD_CALL = ' '
* NO_DEQUEUE = ' '
* NO_ROLLBACK_WORK = ' '
* CLIENTDATACWM =
* CLIENTDATACWMX =
* VALUATIONDATACWM =
* VALUATIONDATACWMX =
IMPORTING
RETURN = ls_return
TABLES
MATERIALDESCRIPTION = lt_desc
* UNITSOFMEASURE =
* UNITSOFMEASUREX =
* INTERNATIONALARTNOS =
* MATERIALLONGTEXT =
* TAXCLASSIFICATIONS =
* RETURNMESSAGES =
* PRTDATA =
* PRTDATAX =
* EXTENSIONIN =
* EXTENSIONINX =
* UNITSOFMEASURECWM =
* UNITSOFMEASURECWMX =
* SEGMRPGENERALDATA =
* SEGMRPGENERALDATAX =
* SEGMRPQUANTITYDATA =
* SEGMRPQUANTITYDATAX =
* SEGVALUATIONTYPE =
* SEGVALUATIONTYPEX =
* SEGSALESSTATUS =
* SEGSALESSTATUSX =
* SEGWEIGHTVOLUME =
* SEGWEIGHTVOLUMEX =
* NFMCHARGEWEIGHTS =
* NFMCHARGEWEIGHTSX =
* NFMSTRUCTURALWEIGHTS =
* NFMSTRUCTURALWEIGHTSX =
.
append ls_return to lt_Return.
clear : ls_return, ls_headdata, ls_clientdata, ls_clientdatax.
refresh lt_desc.
endloop.
TRY.
CALL METHOD cl_salv_table=>factory
EXPORTING
list_display = IF_SALV_C_BOOL_SAP=>FALSE
* r_container =
* container_name =
IMPORTING
r_salv_table = data(lo_alv)
CHANGING
t_table = lt_return
.
CATCH cx_salv_msg.
ENDTRY.
lo_alv->display( ).
Execute the Code :-
-
Select the file from your desktop.
-
Press F8 and click on allow.
-
Now, we can go to MARA table to see the contents for these material numbers.
This comment has been removed by the author.
ReplyDelete