Skip to Content

Introduction

You can use standard Business Application Programming Interfaces (BAPIs) to create or change Media Product Master records in the SAP S/4HANA system. In this blog, I would like to give an example of how to enhance the BAPI_MATERIAL_SAVEREPLICA BAPI by the custom fields you have created in the Custom Fields and Logic App, and how to call the BAPIs to create a new Media Product Master record in the SAP S/4HANA system.

Process

There are 3 steps involved in Creation of Media Product Master using BAPIs:

  1. Enhance BAPI by Custom Fields
  2. Create Product via BAPI
  3. Create Classification via BAPI

1: Enhance BAPI_MATERIAL_SAVEREPLICA by Custom Fields

To fill the custom fields created by the Custom Fields and Logic App, you need to do an extension of the BAPI_MATERIAL_SAVEREPLICA BAPI.

First,  BAPI_TE_MARA structure and the appropriate check structure BAPI_TE_MARAX must be extended by the custom fields. The structures that are delivered as standard only contain the relevant key fields. When new fields are added to this structure, you should ensure that the field name matches the field name in the database table. Furthermore, the fields in the structures BAPI_TE_<NAME> can only be CHARACTER fields. The data element BAPIUPDATE is to be used for the fields in the check structure (exception to this: key fields.)

  1. Call the transaction SE11 and display data type (structure) BAPI_TE_E1MARA.
  2. Create an append structure ZZMARA_CUSTOM_FIELDS.
  3. Add Component ZZ1_GENRE_PRD with Typing Method 1 and Component Type ZZ1_GENRE.
  4. Save and activate the append structure.
  5. In transaction SE11 display data type (structure) BAPI_TE_MARAX.
  6. Create an append structure ZZMARA_CUSTOM_FIELDSX.
  7. Add Component ZZ1_GENRE_PRD with Typing Method 1 and Component Type BAPIUPDATE.
  8. Save and activate the append structure.

2: Create Product via BAPI

Next, I would like to give an example of how to call the BAPI for creation of a new product master and get the custom field filled.

When calling BAPI_MATERIAL_SAVEREPLICA, the parameters EXTENSIONIN and EXTENSIONINX are used to transfer data to the custom fields. The STRUCTURE field contains the name of the structure (BAPI_TE_MARA or BAPI_TE_MARAX in our case). The remaining fields in parameters EXTENSIONIN or EXTENSIONINX contain data for the key fields (for example, the material number) and the data for customer-specific fields. Bear in mind that only data for which an appropriate indicator has been set can be updated to the database.

To explain how the parameters should be filled, find enclosed a sample report:

REPORT ZZ_BAPI_PRODUCT_CREATE.

PARAMETERS: p_matnr  TYPE matnr.

DATA:
  ls_return         TYPE bapie1ret2,
  lt_return         TYPE TABLE OF bapie1ret2,
  ls_headdata       TYPE bapie1matheader,
  lt_headdata       TYPE TABLE OF bapie1matheader,
  ls_clientdata     TYPE bapie1mara,
  ls_clientdatax    TYPE bapie1marax,
  lt_clientdata     TYPE TABLE OF bapie1mara,
  lt_clientdatax    TYPE TABLE OF bapie1marax,
  ls_description    TYPE bapie1makt,
  lt_description    TYPE TABLE OF bapie1makt,
  ls_extension      TYPE bapie1parex,
  ls_extensionx     TYPE bapie1parexx,
  lt_extension      TYPE TABLE OF bapie1parex,
  lt_extensionx     TYPE TABLE OF bapie1parexx,
  ls_ext_mara       TYPE bapi_te_e1mara,
  ls_ext_marax      TYPE bapi_te_e1marax.

" General data needed for product creation
ls_headdata-material       = p_matnr.
ls_headdata-ind_sector     = 'M'.
ls_headdata-matl_type      = 'FERT' .
ls_headdata-basic_view     = 'X' . " Maintenance status K

INSERT ls_headdata INTO TABLE lt_headdata .

" General data needed for product creation (optional for change)
ls_clientdata-material     = ls_headdata-material .
ls_clientdata-base_uom     = 'EA' .
INSERT ls_clientdata INTO TABLE lt_clientdata .

ls_clientdatax-material    = ls_headdata-material .
ls_clientdatax-base_uom    = 'X' .
INSERT ls_clientdatax INTO TABLE lt_clientdatax .

" Description needed for product creation (optional for change)
ls_description-material    = ls_headdata-material .
ls_description-langu       = 'EN' .
ls_description-matl_desc   = 'TEST MPM Creation via BAPI' .
INSERT ls_description INTO TABLE lt_description .


* Extension Field MARA-ZZ1_GENRE_PRD (same name as DB field)
ls_ext_mara-material       = ls_headdata-material.
ls_ext_mara-ZZ1_GENRE_PRD  = 'SCFI'.
ls_extension-material      = ls_headdata-material.
ls_extension-structure     = 'BAPI_TE_E1MARA'.
ls_extension-valuepart1    = ls_ext_mara.
INSERT ls_extension INTO TABLE lt_extension.

ls_ext_marax-material      = ls_headdata-material.
ls_ext_marax-ZZ1_GENRE_PRD = abap_true.
ls_extensionx-material     = ls_headdata-material.
ls_extensionx-structure    = 'BAPI_TE_E1MARAX'.
ls_extensionx-valuepart1   = ls_ext_marax.
INSERT ls_extensionx INTO TABLE lt_extensionx.

CALL FUNCTION 'BAPI_MATERIAL_SAVEREPLICA'
  EXPORTING
    noappllog              = ' '
    nochangedoc            = ' '
    testrun                = ' '
    inpfldcheck            = ' '
*  IMPORTING
*    return                = ls_bapiret2
  TABLES
    headdata               = lt_headdata
    clientdata             = lt_clientdata
    clientdatax            = lt_clientdatax
    materialdescription    = lt_description
    extensionin            = lt_extension
    extensioninx           = lt_extensionx
    returnmessages         = lt_return.

COMMIT WORK.

LOOP AT lt_return INTO ls_return.
  WRITE: / ls_return-message.
ENDLOOP.

3: Create Classification via BAPI

You can create a new product with BAPI_MATERIAL_SAVEREPLICA and get all fields (including custom fields) filled. For the classification (which contains the information of the hierarchy level and the parent product) you can call BAPI BAPI_OBJCL_CREATE.

In the sample code below, I also included a check of the hierarchy level of the parent product by calling BAPI_OBJCL_GETCLASSES:

REPORT zz_bapi_classification_create.

PARAMETERS: p_matnr  TYPE matnr.  " classification should be created for this product
PARAMETERS: p_parent TYPE matnr.  " parent product

DATA:
  ls_return      TYPE bapiret2,
  lt_return      TYPE TABLE OF bapiret2,
  lv_object      LIKE bapi1003_key-object,
  lv_classnum    TYPE klasse_d,
  lv_object_prt  LIKE bapi1003_key-object,  "object key of parent product
  ls_classes     TYPE bapi1003_alloc_list,
  lt_classes     TYPE TABLE OF bapi1003_alloc_list,
  ls_values_char TYPE bapi1003_alloc_values_char,
  lt_values_char TYPE TABLE OF bapi1003_alloc_values_char.

* object key = material number
lv_object_prt                   = p_parent.

* get assigned classes to determine hierarchy level
CALL FUNCTION 'BAPI_OBJCL_GETCLASSES'
  EXPORTING
    objectkey_imp   = lv_object_prt
    objecttable_imp = 'MARA'
    classtype_imp   = '001'
*   READ_VALUATIONS =
*   KEYDATE         = SY-DATUM
*   LANGUAGE        = SY-LANGU
*   OBJECTKEY_IMP_LONG       =
  TABLES
    alloclist       = lt_classes
*   ALLOCVALUESCHAR =
*   ALLOCVALUESCURR =
*   ALLOCVALUESNUM  =
    return          = lt_return.

LOOP AT lt_return INTO ls_return.
  WRITE: / ls_return-message.
ENDLOOP.

* based on hierarchy level of parent determine class of the subordinate level
LOOP AT lt_classes INTO ls_classes.
  CASE ls_classes-classnum.
    WHEN 'MEDIA_PR_FAMILY'.
      lv_classnum = 'MEDIA_PRODUCT'.
      lv_object   = p_matnr.
      " characteristic value for parent media product family
      ls_values_char-charact          = 'MEDIAPRODUCTFAMILY'.
      ls_values_char-value_char_long  = p_parent.
      INSERT ls_values_char INTO TABLE lt_values_char.
   WHEN 'MEDIA_PRODUCT'.
      lv_classnum = 'MEDIA_ISSUE'.
      lv_object   = p_matnr.
      " characteristic value for parent media product
      ls_values_char-charact          = 'MEDIAPRODUCT'.
      ls_values_char-value_char_long  = p_parent.
      INSERT ls_values_char INTO TABLE lt_values_char.
    WHEN 'MEDIA_ISSUE'. " error case: no subnode possible
      EXIT.
  ENDCASE.
ENDLOOP.

IF lv_classnum IS NOT INITIAL.

  CALL FUNCTION 'BAPI_OBJCL_CREATE'
    EXPORTING
      objectkeynew    = lv_object
      objecttablenew  = 'MARA'
      classnumnew     = lv_classnum
      classtypenew    = '001'
*     STATUS          = '1'
*     STANDARDCLASS   =
*     CHANGENUMBER    =
*     KEYDATE         = SY-DATUM
*     NO_DEFAULT_VALUES       = ' '
*     OBJECTKEYNEW_LONG       =
* IMPORTING
*     CLASSIF_STATUS  =
    TABLES
*     ALLOCVALUESNUM  =
      allocvalueschar = lt_values_char
*     ALLOCVALUESCURR =
      return          = lt_return.

  COMMIT WORK.

ENDIF.

LOOP AT lt_return INTO ls_return.
  WRITE: / ls_return-message.
ENDLOOP.

Conclusion

With this example you should be able to create your own coding for your specific needs to create Media Product Master records through BAPI calls.

There can also be additional cases where you must perform additional checks or change a Media Product Master. You will find the required BAPIs in the same function groups as the BAPIs mentioned above.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply