Creating a SAP Function Module to Create Material Master Data using a Template Material
Introduction
In this guide, we will walk through the process of creating a SAP function module that creates material master data using a template material. This function module will utilize the BAPI_MATERIAL_CREATE and BAPI_MATERIAL_COPY function modules to achieve this functionality.
Prerequisites
- SAP NetWeaver 7.0 or higher
- SAP GUI 7.3 or higher
- ABAP Development Tools (ADT) or SAP GUI with ABAP Editor
Function Module Code
FUNCTION Z_CREATE_MATERIAL_FROM_TEMPLATE.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(P_TEMPLATE_MATERIAL) TYPE MATNR
*" VALUE(P_NEW_MATERIAL) TYPE MATNR
*" VALUE(P_MATERIAL_TYPE) TYPE MATYP
*" VALUE(P_MATERIAL_GROUP) TYPE MTART
*" VALUE(P_BASE_UNIT) TYPE MEINS
*" EXPORTING
*" VALUE(E_RETURN_CODE) TYPE SY-SUBRC
*" VALUE(E_RETURN_MESSAGE) TYPE STRING
*"----------------------------------------------------------------------
DATA: lt_bapi_material_create TYPE STANDARD TABLE OF bapi_material_create,
ls_bapi_material_create TYPE bapi_material_create,
lt_bapi_material_copy TYPE STANDARD TABLE OF bapi_material_copy,
ls_bapi_material_copy TYPE bapi_material_copy,
lv_return_code TYPE sy-subrc,
lv_return_message TYPE string.
" Copy material from template
CALL FUNCTION 'BAPI_MATERIAL_COPY'
EXPORTING
material = p_template_material
new_material = p_new_material
IMPORTING
return = lv_return_code
EXCEPTIONS
material_not_found = 1
material_locked = 2
material_already_exists = 3
OTHERS = 4.
IF sy-subrc <> 0.
e_return_code = sy-subrc.
e_return_message = 'Error copying material from template'.
RETURN.
ENDIF.
" Create material master data
CALL FUNCTION 'BAPI_MATERIAL_CREATE'
EXPORTING
material = p_new_material
material_type = p_material_type
material_group = p_material_group
base_unit = p_base_unit
IMPORTING
return = lv_return_code
EXCEPTIONS
material_not_found = 1
material_locked = 2
material_already_exists = 3
OTHERS = 4.
IF sy-subrc <> 0.
e_return_code = sy-subrc.
e_return_message = 'Error creating material master data'.
RETURN.
ENDIF.
e_return_code = 0.
e_return_message = 'Material created successfully'.
ENDFUNCTION.
Explanation
This function module Z_CREATE_MATERIAL_FROM_TEMPLATE takes in the following import parameters:
P_TEMPLATE_MATERIAL: The material number of the template material
P_NEW_MATERIAL: The material number of the new material to be created
P_MATERIAL_TYPE: The material type of the new material
P_MATERIAL_GROUP: The material group of the new material
P_BASE_UNIT: The base unit of the new material
The function module first calls the BAPI_MATERIAL_COPY function module to copy the material from the template. If the copy operation is successful, it then calls the BAPI_MATERIAL_CREATE function module to create the material master data.
The function module exports the following parameters:
E_RETURN_CODE: The return code of the function module
E_RETURN_MESSAGE: The return message of the function module
Example Usage
To use this function module, you can call it from another ABAP program like this:
DATA: lv_return_code TYPE sy-subrc,
lv_return_message TYPE string.
CALL FUNCTION 'Z_CREATE_MATERIAL_FROM_TEMPLATE'
EXPORTING
p_template_material = 'TEMPLATE_MATERIAL'
p_new_material = 'NEW_MATERIAL'
p_material_type = 'FERT'
p_material_group = 'GROUP1'
p_base_unit = 'KG'
IMPORTING
e_return_code = lv_return_code
e_return_message = lv_return_message.
IF lv_return_code = 0.
WRITE: / 'Material created successfully'.
ELSE.
WRITE: / 'Error creating material:', lv_return_message.
ENDIF.
Note: Replace the TEMPLATE_MATERIAL, NEW_MATERIAL, GROUP1, and KG with your actual material numbers and values.