Migrating Material Data Using Methods and 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.

  • In this part we will file_open_dialog and GUI_UPLOAD method of CL_GUI_FRONTEND_SERVICES

  • 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 4 :- In Start Of Selection we can use FILE_OPWN_DIALOG method of CL_GUI_FRONTEND_SERVICES to navigate to local system and select the file.



  • Step 5 :- In Start Of Selection, we will use GUI_UPLOAD method of CL_GUI_FRONTEND_SERVICES 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_BAPI_MATERIAL_SAVE
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZAR_BAPI_MATERIAL_SAVE_DET.

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 : lv_file type string.
  DATA : ls_header type BAPIMATHEAD.
  DATA : lt_material_desc type table of BAPI_MAKT,
         ls_material_desc type BAPI_MAKT.
  DATA : ls_clientdata type BAPI_MARA.
  DATA : ls_clientdatax type BAPI_MARAX.
  DATA : lt_return type table of BAPIRET2,
         ls_Return type BAPIRET2.

DATA : lt_table type table of FILE_TABLE.
DATA : lo_rc type i.

START-OF-SELECTION.
 CALL METHOD cl_gui_frontend_services=>file_open_dialog
   CHANGING
     file_table              = lt_table
     rc                      = lo_rc
*     user_action             =
*     file_encoding           =
   EXCEPTIONS
     file_open_dialog_failed = 1
     cntl_error              = 2
     error_no_gui            = 3
     not_supported_by_gui    = 4
     others                  = 5
         .

 DATA : lo_file type string.
 Read table lt_table into data(ls_table) index 1.
 if sy-subrc eq 0.
   lo_file = ls_table-FILENAME.

endif.

lv_file = lo_file.

CALL METHOD cl_gui_frontend_services=>gui_upload
  EXPORTING
    filename                = lv_file
*    filetype                = 'ASC'
    has_field_separator     = 'X'
  CHANGING
    data_tab                = lt_material
*    isscanperformed         = SPACE
  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
    not_supported_by_gui    = 17
    error_no_gui            = 18
    others                  = 19
        .

 Loop at lt_material into ls_material.

ls_header-material = ls_material-matnr.
ls_header-IND_SECTOR = ls_material-mbrsh.
ls_header-MATL_TYPE = ls_material-mtart.
ls_header-BASIC_VIEW = 'X'.

ls_material_desc-MATL_DESC = ls_material-maktx.
ls_material_desc-langu = sy-langu.
append ls_material_desc to lt_material_Desc.
clear ls_material_desc.

ls_clientdata-BASE_UOM = ls_material-meins.
ls_clientdatax-BASE_UOM = 'X'.

 CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
   EXPORTING
     headdata                    = ls_header
    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_material_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       =
           .

refresh : lt_material_desc.
append ls_return to lt_return.
clear : ls_return, ls_clientdata, ls_clientdatax, ls_header.
 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.

  • click on allow.



  • Now, we can go to MARA table to see the contents for these material numbers.



Comments

Popular posts from this blog

Understanding Different Types of SAP Function Modules: Normal, RFC, and Update

Mastering ABAP: A Step-by-Step Guide to Function Modules and Groups for Modularized Programming

Introduction to SAP