SAP ABAP - Sample Program To Upload Material Master Sales / Warehousing View Using BAPI - BAPI_MATERIAL_SAVEDATA.

This is a sample ABAP program to upload material master sales view using BAPI - BAPI_MATERIAL_SAVEDATA.

SAMPLE PROGRAM:

REPORT ztest_bapi_mm01_upload.

TYPE-POOLS: truxs.

TYPES : BEGIN OF ty_upload,
matnr TYPE marc-matnr,
werks TYPE marc-werks,
lgnum TYPE mlgn-lgnum,
ltkza TYPE mlgn-ltkza,
ltkze TYPE mlgn-ltkze,
lgbkz TYPE mlgn-lgbkz,
bsskz TYPE mlgn-bsskz,
lhmg1 TYPE mlgn-lhmg1,
lhme1 TYPE mlgn-lhme1,
lety1 TYPE mlgn-lety1,
lhmg2 TYPE mlgn-lhmg2,
lhme2 TYPE mlgn-lhme2,
lety2 TYPE mlgn-lety2,
lhmg3 TYPE mlgn-lhmg3,
lhme3 TYPE mlgn-lhme3,
lety3 TYPE mlgn-lety3,
error(80),
END OF ty_upload.

DATA : BEGIN OF ty_fieldnames,
fieldnames(50).
DATA : END OF ty_fieldnames.

DATA : wa_fieldnames LIKE ty_fieldnames,
it_fieldnames LIKE TABLE OF ty_fieldnames.

DATA : wa_headdata TYPE bapimathead,
wa_plantdata TYPE bapi_marc,
wa_plantdatax TYPE bapi_marcx,
wa_warehousenumberdata TYPE bapi_mlgn,
wa_warehousenumberdatax TYPE bapi_mlgnx,
rettab TYPE bapiret2 OCCURS 0 WITH HEADER LINE.

DATA : wa_upload TYPE ty_upload,
it_upload TYPE TABLE OF ty_upload.

SELECTION-SCREEN : BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS: p_upload LIKE rlgrap-filename. " Uploading File
PARAMETERS: p_error LIKE rlgrap-filename. " Downloading Error File
SELECTION-SCREEN : END OF BLOCK b1.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_upload.

PERFORM search USING p_upload.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_error.

PERFORM search USING p_error.


START-OF-SELECTION.

PERFORM upload_process USING p_upload.
PERFORM upload.
PERFORM field_header.
PERFORM download USING p_error.

*&---------------------------------------------------------------------*
*& Form SEARCH
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_P_UPLOAD text
*----------------------------------------------------------------------*
FORM search USING pfname.

CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
EXPORTING
static = 'X'
CHANGING
file_name = pfname.

ENDFORM. " SEARCH
*&---------------------------------------------------------------------*
*& Form UPLOAD_PROCESS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_PFNAME text
*----------------------------------------------------------------------*
FORM upload_process USING pfname.

DATA : l_file TYPE string,
it_raw TYPE truxs_t_text_data.

l_file = pfname.

CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
i_field_seperator = 'X'
i_line_header = 'X'
i_tab_raw_data = it_raw
i_filename = pfname
TABLES
i_tab_converted_data = it_upload[]
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.

IF sy-subrc NE 0.
MESSAGE 'Error in uploading file' TYPE 'E'.
ENDIF.

ENDFORM. " UPLOAD_PROCESS


*&---------------------------------------------------------------------*
*& Form upload
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM upload.

DATA : l_tabix TYPE sy-tabix.

LOOP AT it_upload INTO wa_upload
l_tabix = sy-tabix.
PERFORM conv_routine USING wa_upload-matnr
CHANGING wa_upload-matnr.

wa_headdata-material = wa_upload-matnr.
wa_headdata-warehouse_view = 'X'.

wa_plantdata-plant = wa_upload-werks.
wa_plantdatax-plant = wa_upload-werks.

wa_warehousenumberdata-whse_no = wa_upload-lgnum.
wa_warehousenumberdata-withdrawal = wa_upload-ltkza.
wa_warehousenumberdata-placement = wa_upload-ltkze.
wa_warehousenumberdata-stgesector = wa_upload-lgbkz.
wa_warehousenumberdata-spec_mvmt = wa_upload-bsskz.

wa_warehousenumberdata-l_equip_1 = wa_upload-lhmg1.
wa_warehousenumberdata-l_equip_2 = wa_upload-lhmg2.
wa_warehousenumberdata-l_equip_3 = wa_upload-lhmg3.

wa_warehousenumberdata-leq_unit_1 = wa_upload-lhme1.
wa_warehousenumberdata-leq_unit_2 = wa_upload-lhme2.
wa_warehousenumberdata-leq_unit_3 = wa_upload-lhme3.

wa_warehousenumberdata-unittype_1 = wa_upload-lety1.
wa_warehousenumberdata-unittype_2 = wa_upload-lety2.
wa_warehousenumberdata-unittype_3 = wa_upload-lety3.

wa_warehousenumberdatax-whse_no = wa_upload-lgnum.
wa_warehousenumberdatax-withdrawal = 'X'.
wa_warehousenumberdatax-placement = 'X'.
wa_warehousenumberdatax-stgesector = 'X'.
wa_warehousenumberdatax-spec_mvmt = 'X'.

wa_warehousenumberdatax-l_equip_1 = 'X'.
wa_warehousenumberdatax-l_equip_2 = 'X'.
wa_warehousenumberdatax-l_equip_3 = 'X'.

wa_warehousenumberdatax-leq_unit_1 = 'X'.
wa_warehousenumberdatax-leq_unit_2 = 'X'.
wa_warehousenumberdatax-leq_unit_3 = 'X'.

wa_warehousenumberdatax-unittype_1 = 'X'.
wa_warehousenumberdatax-unittype_2 = 'X'.
wa_warehousenumberdatax-unittype_3 = 'X'.

* BAPI To Upload Material Master Warehousing View.

CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
EXPORTING
headdata = wa_headdata
plantdata = wa_plantdata
plantdatax = wa_plantdatax
warehousenumberdata = wa_warehousenumberdata
warehousenumberdatax = wa_warehousenumberdatax
IMPORTING
return = rettab.

APPEND rettab.

READ TABLE rettab WITH KEY type = 'S'.
IF sy-subrc = 0.
MOVE rettab-message TO wa_upload-error.
REFRESH rettab.

ELSE.
MOVE rettab-message TO wa_upload-error.
REFRESH rettab.
ENDIF.

MODIFY it_upload FROM wa_upload INDEX l_tabix TRANSPORTING error.
CLEAR wa_upload.
ENDLOOP.


ENDFORM. "modification

*&---------------------------------------------------------------------*
*& Form CONV_ROUTINE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_INPUT text
* -->P_OUTPUT text
*----------------------------------------------------------------------*
FORM conv_routine USING p_input
CHANGING p_output.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = p_input
IMPORTING
output = p_output.

ENDFORM. "CONV_ROUTINE

*&---------------------------------------------------------------------*
*& Form download
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_ERROR text
*----------------------------------------------------------------------*
FORM download USING p_error.

DATA : l_fname TYPE string.
l_fname = p_error.

CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = l_fname
filetype = 'DAT'
write_field_separator = 'x'
TABLES
data_tab = it_upload[]
fieldnames = it_fieldnames[]
.
IF sy-subrc = 0.
MESSAGE 'FILE DOWNLOADED SUCCESSFULLY' TYPE 'I'.
ENDIF.

ENDFORM. "download

*&---------------------------------------------------------------------*
*& Form FIELDNAMES
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_FIELDNAME text
*----------------------------------------------------------------------*
FORM fieldnames USING p_fieldname.

wa_fieldnames-fieldnames = p_fieldname.
APPEND wa_fieldnames TO it_fieldnames.
CLEAR wa_fieldnames.

ENDFORM. "FIELDNAMES

*&---------------------------------------------------------------------*
*& Form FIELD_HEADER
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM field_header.

PERFORM fieldnames USING 'Material Number'.
PERFORM fieldnames USING 'Plant'.
PERFORM fieldnames USING 'Warehouse Number'.
PERFORM fieldnames USING 'Storg type Ind for stock Removal'.
PERFORM fieldnames USING 'Storg type Ind for stock Placement'.
PERFORM fieldnames USING 'Storg type Indicators'.
PERFORM fieldnames USING 'Spl movement ind for warehouse management'.
PERFORM fieldnames USING 'Loading equipment quantity 1'.
PERFORM fieldnames USING 'Unit of measure for loading equipment quantity 1'.
PERFORM fieldnames USING '1st storage unit type'.
PERFORM fieldnames USING 'Loading equipment quantity 2'.
PERFORM fieldnames USING 'Unit of measure for loading equipment quantity 2'.
PERFORM fieldnames USING '2nd storage unit type'.
PERFORM fieldnames USING 'Loading equipment quantity 3'.
PERFORM fieldnames USING 'Unit of measure for loading equipment quantity 3'.
PERFORM fieldnames USING '3rd storage unit type'.
PERFORM fieldnames USING 'log Message'.

ENDFORM. "FIELD_HEADER


Your suggestions and comments are welcome in this section.


Please mail all your contributions to administrator@abapmadeeasy.com We request you to mention your Name, Designation, Experience & Organization you are working for. Your posts will be verified and posted in this site with your name.

No comments:

Post a Comment