Using BAPI to Migrate Material Master MM01/MM02

Crate/update materials using material BAPI, migrate material master using BAPI_MATERIAL_SAVEDATA

+ -

Material Master Data migration using BAPI.

The below example explains of using BAPI for migrating material master into SAP System using a flat file.

Before using a BAPI to update a table/transaction, you should know about below two function modules.

BAPI_TRANSACTION_COMMIT

It is a Function module, which is used update database after all transactions in a LUW are completed.BY calling this all the related tables for that transaction will be updated. We must use this if we do not have any errors in BAPI call.

BAPI_TRANSACTION_ROLLBACK

It is a function module which is used to roll back all the changes if there are any errors in BAPI call. If there are any errors in BAPI call, we must roll back all changes.

Requirement: Migrate material master with basic data from a flat file (.txt tab delimited) separated by tab using BAPI method. Available file format is below:

Migrate Material Master SAP ABAPI

Input Screen:

Migrate material master SAP ABAP

Follow below steps to migrate material master using BAPI from a flat file.

  1. Upload flat file to an internal table.
  2. Loop through internal table and pass data to BAPI.

Upload flat file to internal table

TYPES: BEGIN OF TY_MARA, "internal table as per flat file structure
        MATNR TYPE MARA-MATNR,
        MTART TYPE MARA-MTART,
        MBRSH TYPE MARA-MBRSH,
        MATKL TYPE MARA-MATKL,
        MEINS TYPE MARA-MEINS,
        MAKTX TYPE MAKT-MAKTX,
      END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA,
      WA_MARA TYPE TY_MARA.
**selection screen
PARAMETERS: P_FILE TYPE RLGRAP-FILENAME. "file input

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE. "f4 helop for file input
  PERFORM FILE_HELP.

AT SELECTION-SCREEN ON HELP-REQUEST FOR P_FILE. "F1 help for file input
  MESSAGE 'Please press f4 to select file' TYPE 'I'.
START-OF-SELECTION.
  DATA : LV_FILE TYPE STRING.
  LV_FILE = P_FILE. "to avoid type conflict
  CALL FUNCTION 'GUI_UPLOAD' "upload flat file
    EXPORTING
      FILENAME            = LV_FILE
      FILETYPE            = 'ASC'
      HAS_FIELD_SEPARATOR = 'X'
*     HEADER_LENGTH       = 0
*     READ_BY_LINE        = 'X'
*     DAT_MODE            = ' '
*     CODEPAGE            = ' '
*     IGNORE_CERR         = ABAP_TRUE
*     REPLACEMENT         = '#'
*     CHECK_BOM           = ' '
*     VIRUS_SCAN_PROFILE  =
*     NO_AUTH_CHECK       = ' '
* IMPORTING
*     FILELENGTH          =
*     HEADER              =
    TABLES
      DATA_TAB            = IT_MARA "internal table
* CHANGING
*     ISSCANPERFORMED     = ' '
* 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.
* Implement suitable error handling here
  ENDIF.
FORM FILE_HELP .
  CALL FUNCTION 'KD_GET_FILENAME_ON_F4' "get file name on F4 help
* EXPORTING
*   PROGRAM_NAME        = SYST-REPID
*   DYNPRO_NUMBER       = SYST-DYNNR
*   FIELD_NAME          = ' '
*   STATIC              = ' '
*   MASK                = ' '
*   FILEOPERATION       = 'R'
*   PATH                =
    CHANGING
      FILE_NAME = P_FILE
*     LOCATION_FLAG       = 'P'
* EXCEPTIONS
*     MASK_TOO_LONG       = 1
*     OTHERS    = 2
    .
  IF SY-SUBRC <> 0.
* Implement suitable error handling here
  ENDIF.
ENDFORM.                    " FILE_HELP

Loop through internal table and pass data to BAPI

*BAPI declarations
DATA : WA_HEADDATA TYPE BAPIMATHEAD,
       WA_CLIENTDATA TYPE BAPI_MARA,
       WA_CLIENTDATAX TYPE BAPI_MARAX.
DATA : IT_MATERIALDESCRIPTION TYPE TABLE OF BAPI_MAKT,
       WA_MATERIALDESCRIPTION TYPE BAPI_MAKT.
DATA : IT_RETURN TYPE TABLE OF BAPIRET2,
       WA_RETURN TYPE BAPIRET2.
LOOP AT IT_MARA INTO WA_MARA.
    WA_HEADDATA-MATERIAL = WA_MARA-MATNR. "material no
    WA_HEADDATA-IND_SECTOR = WA_MARA-MBRSH. "industry sector
    WA_HEADDATA-MATL_TYPE = WA_MARA-MTART. "material type
    WA_HEADDATA-BASIC_VIEW = 'X'. "basic view
    WA_CLIENTDATA-MATL_GROUP = WA_MARA-MATKL. "material group
    WA_CLIENTDATA-BASE_UOM = WA_MARA-MEINS. "base Unit Of measure
    WA_CLIENTDATAX-MATL_GROUP = 'X'. "passing material group
    WA_CLIENTDATAX-BASE_UOM = 'X'. "passing base unit of measure

    WA_MATERIALDESCRIPTION-LANGU = 'EN'. "english language
    WA_MATERIALDESCRIPTION-LANGU_ISO = 'EN'. "ISO language
    WA_MATERIALDESCRIPTION-MATL_DESC = WA_MARA-MAKTX. "material descriptions
    APPEND WA_MATERIALDESCRIPTION TO IT_MATERIALDESCRIPTION.
    CLEAR WA_MATERIALDESCRIPTION.
**Create/update materials using BAPI_MATERIAL_SAVEDATA
    CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
      EXPORTING
        HEADDATA            = WA_HEADDATA
        CLIENTDATA          = WA_CLIENTDATA
        CLIENTDATAX         = WA_CLIENTDATAX
*       PLANTDATA           = WA_PLANTDATA
*       PLANTDATAX          = WA_PLANTDATAX
*       FORECASTPARAMETERS  =
*       FORECASTPARAMETERSX =
*       PLANNINGDATA        =
*       PLANNINGDATAX       =
*       STORAGELOCATIONDATA = WA_STORAGELOCATIONDATA
*       STORAGELOCATIONDATAX = WA_STORAGELOCATIONDATAX
*       VALUATIONDATA       =
*       VALUATIONDATAX      =
*       WAREHOUSENUMBERDATA =
*       WAREHOUSENUMBERDATAX =
*       SALESDATA           = WA_SALESDATA
*       SALESDATAX          = WA_SALESDATAX
*       STORAGETYPEDATA     =
*       STORAGETYPEDATAX    =
*       FLAG_ONLINE         = ' '
*       FLAG_CAD_CALL       = ' '
*       NO_DEQUEUE          = ' '
*       NO_ROLLBACK_WORK    = ' '
      IMPORTING
        RETURN              = WA_RETURN
      TABLES
        MATERIALDESCRIPTION = IT_MATERIALDESCRIPTION
*       UNITSOFMEASURE      = IT_UNITSOFMEASURE
*       UNITSOFMEASUREX     = IT_UNITSOFMEASUREX
*       INTERNATIONALARTNOS =
*       MATERIALLONGTEXT    = IT_MATERIALLONGTEXT
*       TAXCLASSIFICATIONS  = IT_TAXCLASSIFICATIONS
*       RETURNMESSAGES      = IT_RETURN
*       PRTDATA             =
*       PRTDATAX            =
*       EXTENSIONIN         =
*       EXTENSIONINX        =
      .
    IF WA_RETURN-TYPE = 'E'.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      WRITE:/ WA_HEADDATA-MATERIAL, 'is not created' COLOR 3.
    ELSE.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          WAIT = 'X'.
      IF SY-SUBRC EQ 0.
        WRITE:/ WA_HEADDATA-MATERIAL, 'is created' COLOR 5.
      ENDIF.
    ENDIF.
  ENDLOOP.

Final code will be

REPORT ZSAPN_MATERIAL_BAPI.
*BAPI declarations
DATA : WA_HEADDATA TYPE BAPIMATHEAD,
       WA_CLIENTDATA TYPE BAPI_MARA,
       WA_CLIENTDATAX TYPE BAPI_MARAX.
DATA : IT_MATERIALDESCRIPTION TYPE TABLE OF BAPI_MAKT,
       WA_MATERIALDESCRIPTION TYPE BAPI_MAKT.
DATA : IT_RETURN TYPE TABLE OF BAPIRET2,
       WA_RETURN TYPE BAPIRET2.
TYPES: BEGIN OF TY_MARA, "internal table as per flat file structure
        MATNR TYPE MARA-MATNR,
        MTART TYPE MARA-MTART,
        MBRSH TYPE MARA-MBRSH,
        MATKL TYPE MARA-MATKL,
        MEINS TYPE MARA-MEINS,
        MAKTX TYPE MAKT-MAKTX,
      END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA,
      WA_MARA TYPE TY_MARA.
**selection screen
PARAMETERS: P_FILE TYPE RLGRAP-FILENAME. "file input

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE. "f4 helop for file input
  PERFORM FILE_HELP.

AT SELECTION-SCREEN ON HELP-REQUEST FOR P_FILE. "F1 help for file input
  MESSAGE 'Please press f4 to select file' TYPE 'I'.

START-OF-SELECTION.
  DATA : LV_FILE TYPE STRING.
  LV_FILE = P_FILE. "to avoid type conflict
  CALL FUNCTION 'GUI_UPLOAD' "upload flat file
    EXPORTING
      FILENAME            = LV_FILE
      FILETYPE            = 'ASC'
      HAS_FIELD_SEPARATOR = 'X'
*     HEADER_LENGTH       = 0
*     READ_BY_LINE        = 'X'
*     DAT_MODE            = ' '
*     CODEPAGE            = ' '
*     IGNORE_CERR         = ABAP_TRUE
*     REPLACEMENT         = '#'
*     CHECK_BOM           = ' '
*     VIRUS_SCAN_PROFILE  =
*     NO_AUTH_CHECK       = ' '
* IMPORTING
*     FILELENGTH          =
*     HEADER              =
    TABLES
      DATA_TAB            = IT_MARA "internal table
* CHANGING
*     ISSCANPERFORMED     = ' '
* 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.
* Implement suitable error handling here
  ENDIF.
  LOOP AT IT_MARA INTO WA_MARA.
    WA_HEADDATA-MATERIAL = WA_MARA-MATNR. "material no
    WA_HEADDATA-IND_SECTOR = WA_MARA-MBRSH. "industry sector
    WA_HEADDATA-MATL_TYPE = WA_MARA-MTART. "material type
    WA_HEADDATA-BASIC_VIEW = 'X'. "basic view
    WA_CLIENTDATA-MATL_GROUP = WA_MARA-MATKL. "material group
    WA_CLIENTDATA-BASE_UOM = WA_MARA-MEINS. "base Unit Of measure
    WA_CLIENTDATAX-MATL_GROUP = 'X'. "passing material group
    WA_CLIENTDATAX-BASE_UOM = 'X'. "passing base unit of measure

    WA_MATERIALDESCRIPTION-LANGU = 'EN'. "english language
    WA_MATERIALDESCRIPTION-LANGU_ISO = 'EN'. "ISO language
    WA_MATERIALDESCRIPTION-MATL_DESC = WA_MARA-MAKTX. "material descriptions
    APPEND WA_MATERIALDESCRIPTION TO IT_MATERIALDESCRIPTION.
    CLEAR WA_MATERIALDESCRIPTION.
**Create/update materials using BAPI_MATERIAL_SAVEDATA
    CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
      EXPORTING
        HEADDATA            = WA_HEADDATA
        CLIENTDATA          = WA_CLIENTDATA
        CLIENTDATAX         = WA_CLIENTDATAX
*       PLANTDATA           = WA_PLANTDATA
*       PLANTDATAX          = WA_PLANTDATAX
*       FORECASTPARAMETERS  =
*       FORECASTPARAMETERSX =
*       PLANNINGDATA        =
*       PLANNINGDATAX       =
*       STORAGELOCATIONDATA = WA_STORAGELOCATIONDATA
*       STORAGELOCATIONDATAX = WA_STORAGELOCATIONDATAX
*       VALUATIONDATA       =
*       VALUATIONDATAX      =
*       WAREHOUSENUMBERDATA =
*       WAREHOUSENUMBERDATAX =
*       SALESDATA           = WA_SALESDATA
*       SALESDATAX          = WA_SALESDATAX
*       STORAGETYPEDATA     =
*       STORAGETYPEDATAX    =
*       FLAG_ONLINE         = ' '
*       FLAG_CAD_CALL       = ' '
*       NO_DEQUEUE          = ' '
*       NO_ROLLBACK_WORK    = ' '
      IMPORTING
        RETURN              = WA_RETURN
      TABLES
        MATERIALDESCRIPTION = IT_MATERIALDESCRIPTION
*       UNITSOFMEASURE      = IT_UNITSOFMEASURE
*       UNITSOFMEASUREX     = IT_UNITSOFMEASUREX
*       INTERNATIONALARTNOS =
*       MATERIALLONGTEXT    = IT_MATERIALLONGTEXT
*       TAXCLASSIFICATIONS  = IT_TAXCLASSIFICATIONS
*       RETURNMESSAGES      = IT_RETURN
*       PRTDATA             =
*       PRTDATAX            =
*       EXTENSIONIN         =
*       EXTENSIONINX        =
      .
    IF WA_RETURN-TYPE = 'E'.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      WRITE:/ WA_HEADDATA-MATERIAL, 'is not created' COLOR 3.
    ELSE.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          WAIT = 'X'.
      IF SY-SUBRC EQ 0.
        WRITE:/ WA_HEADDATA-MATERIAL, 'is created' COLOR 5.
      ENDIF.
    ENDIF.
  ENDLOOP.
FORM FILE_HELP .
  CALL FUNCTION 'KD_GET_FILENAME_ON_F4' "get file name on F4 help
* EXPORTING
*   PROGRAM_NAME        = SYST-REPID
*   DYNPRO_NUMBER       = SYST-DYNNR
*   FIELD_NAME          = ' '
*   STATIC              = ' '
*   MASK                = ' '
*   FILEOPERATION       = 'R'
*   PATH                =
    CHANGING
      FILE_NAME = P_FILE
*     LOCATION_FLAG       = 'P'
* EXCEPTIONS
*     MASK_TOO_LONG       = 1
*     OTHERS    = 2
    .
  IF SY-SUBRC <> 0.
* Implement suitable error handling here
  ENDIF.
ENDFORM.                    " FILE_HELP

Testing

Download the flat file and test the above program.

Please Sign in to ask a question

Was this lesson helpful to you? Yes No 50 People out of 54 think this lesson helpful


Lesson Navigation

←Previous Chapter
Next Lesson → ALE-IDOCS Development