< Page d'accueil du blog

SAPCooking recipe: complete MM batch copy including its classification

Publié le 15 février 2024

SAP cooking

How to copy a MM batch (equivalent t-code MSC1N) including the complete classification (class number 023) using BAPIs. Read the complete recipe and watch the video at the bottom of this page !

Main features

Selection-screen

Data selection

Call BAPI to copy the batch

Call BAPI to copy the classification

Program requirements

Selection-screen

Element Type Element Label Properties
Block "Batch to be copied"
Parameter (MCH1-MATNR) Material Mandatory
Parameter (MCH1-CHARG) Batch Mandatory

Data selection

Fetch data from table MCH1 with Material and Batch matching selection-screen values.

Stop the treatment if no data found.

Call BAPI to copy the batch

Call function BAPI_BATCH_GET_DETAIL with following parameters:

Importing parameter Value
MATERIAL MCH1-MATNR
BATCH MCH1-CHARG

And retrieve the structure BATCHATTRIBUTES.

Call function BAPI_BATCH_CREATE with following parameters:

Importing parameter Value
MATERIAL MCH1-MATNR
BATCHATTRIBUTES Structure previously retrieved

If table RETURN contains any error, call function BAPI_TRANSACTION_ROLLBACK, display all errors in a popup using functions MESSAGES_INITIALIZE, MESSAGE_STORE and MESSAGES_SHOW, and stop the treatment.

Else call function BAPI_TRANSACTION_COMMIT.

Call BAPI to copy the classification

Call function BAPI_OBJCL_GETCLASSES with following parameters:

Importing parameter Value
OBJECTKEY_IMP Use BAPI_OBJCL_CONCATENATEKEY with OBJECTTABLE = 'MCH1' and OBJECTKEYTABLE filled with duets MATNR/MCH1-MATNR and CHARG/MCH1-CHARG and get the resulting OBJECTKEY.
OBJECTTABLE_IMP 'MCH1'
CLASSTYPE_IMP 023

And get the first line in resulting table ALLOCLIST.

Call function BAPI_OBJCL_GETDETAIL with following parameters:

Importing parameter Value
OBJECTKEY Same value than above
OBJECTTABLE 'MCH1'
CLASSNUM ALLOCLIST-CLASSNUM
CLASSTYPE 023

And get tables ALLOCVALUESNUM, ALLOCVALUESCHAR and ALLOCVALUESCURR.

Call function BAPI_OBJCL_CREATE with following parameters:

Importing parameter Value
OBJECTKEYNEW Use BAPI_OBJCL_CONCATENATEKEY with OBJECTTABLE = 'MCH1' and OBJECTKEYTABLE filled with duets MATNR/MCH1-MATNR and CHARG/Batch number issued from BAPI_BATCH_CREATE, and get the resulting OBJECTKEY.
OBJECTTABLE 'MCH1'
CLASSNUM ALLOCLIST-CLASSNUM
CLASSTYPE 023
ALLOCVALUESNUM Table ALLOCVALUESNUM issued from BAPI_OBJCL_GETDETAIL
ALLOCVALUESCHAR Table ALLOCVALUESCHAR issued from BAPI_OBJCL_GETDETAIL
ALLOCVALUESCURR Table ALLOCVALUESCURR issued from BAPI_OBJCL_GETDETAIL

If table RETURN contains any error, call function BAPI_TRANSACTION_ROLLBACK, display all errors in a popup using functions MESSAGES_INITIALIZE, MESSAGE_STORE and MESSAGES_SHOW, and stop the treatment.

Else call function BAPI_TRANSACTION_COMMIT and display success messages for both creation of new batch and creation of the classification, using the same 3 functions.

The recipe in video