Using BAPI To Migrate Material Master - BAPI's in SAP - SAPNuts

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

5/4/2015

UsingBAPItoMigrateMaterialMasterBAPI`sinSAP| SAPNuts

You are here / SAPNuts / / Courses /courses/


/ SAP Cross Applications /courses/SAPCrossApplications.html
/ BAPI`s in SAP /courses/SAPCrossApplications/bapisinsap.html
/ Using BAPI to Migrate Material Master

https://play.google.com/store/apps/details?id=com.sapnutsweb.com.sapnuts_web
SAPNuts.com on google play https://play.google.com/store/apps/details?
id=com.sapnutsweb.com.sapnuts_web

Using BAPI to Migrate Material Master


Last Updated: March 3rd 2014 by Admin
+

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 dont 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:

https://www.sapnuts.com/courses/SAPCrossApplications/bapisinsap/usingbapimaterial.html

1/10

5/4/2015

UsingBAPItoMigrateMaterialMasterBAPI`sinSAP| SAPNuts

Input Screen:

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:BEGINOFTY_MARA,"internaltableasperflatfilestructure
MATNRTYPEMARAMATNR,
MTARTTYPEMARAMTART,
MBRSHTYPEMARAMBRSH,
MATKLTYPEMARAMATKL,
MEINSTYPEMARAMEINS,
MAKTXTYPEMAKTMAKTX,
ENDOFTY_MARA.
DATA:IT_MARATYPETABLEOFTY_MARA,
WA_MARATYPETY_MARA.
**selectionscreen
PARAMETERS:P_FILETYPERLGRAPFILENAME."fileinput
ATSELECTIONSCREENONVALUEREQUESTFORP_FILE."f4helopforfileinput
PERFORMFILE_HELP.
ATSELECTIONSCREENONHELPREQUESTFORP_FILE."F1helpforfileinput
MESSAGE'Pleasepressf4toselectfile'TYPE'I'.
STARTOFSELECTION.
DATA:LV_FILETYPESTRING.
LV_FILE=P_FILE."toavoidtypeconflict
CALLFUNCTION'GUI_UPLOAD'"uploadflatfile
EXPORTING
FILENAME=LV_FILE
FILETYPE='ASC'
HAS_FIELD_SEPARATOR='X'
*HEADER_LENGTH=0
*READ_BY_LINE='X'
*DAT_MODE=''
*CODEPAGE=''
*IGNORE_CERR=ABAP_TRUE

https://www.sapnuts.com/courses/SAPCrossApplications/bapisinsap/usingbapimaterial.html

2/10

5/4/2015

UsingBAPItoMigrateMaterialMasterBAPI`sinSAP| SAPNuts

*REPLACEMENT='#'
*CHECK_BOM=''
*VIRUS_SCAN_PROFILE=
*NO_AUTH_CHECK=''
*IMPORTING
*FILELENGTH=
*HEADER=
TABLES
DATA_TAB=IT_MARA"internaltable
*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
.
IFSYSUBRC<>0.
*Implementsuitableerrorhandlinghere
ENDIF.
FORMFILE_HELP.
CALLFUNCTION'KD_GET_FILENAME_ON_F4'"getfilenameonF4help
*EXPORTING
*PROGRAM_NAME=SYSTREPID
*DYNPRO_NUMBER=SYSTDYNNR
*FIELD_NAME=''
*STATIC=''
*MASK=''
*FILEOPERATION='R'
*PATH=
CHANGING
FILE_NAME=P_FILE
*LOCATION_FLAG='P'
*EXCEPTIONS
*MASK_TOO_LONG=1
*OTHERS=2
.

https://www.sapnuts.com/courses/SAPCrossApplications/bapisinsap/usingbapimaterial.html

3/10

5/4/2015

UsingBAPItoMigrateMaterialMasterBAPI`sinSAP| SAPNuts

IFSYSUBRC<>0.
*Implementsuitableerrorhandlinghere
ENDIF.
ENDFORM."FILE_HELP

Loop through internal table and pass data to BAPI


*BAPIdeclarations
DATA:WA_HEADDATATYPEBAPIMATHEAD,
WA_CLIENTDATATYPEBAPI_MARA,
WA_CLIENTDATAXTYPEBAPI_MARAX.
DATA:IT_MATERIALDESCRIPTIONTYPETABLEOFBAPI_MAKT,
WA_MATERIALDESCRIPTIONTYPEBAPI_MAKT.
DATA:IT_RETURNTYPETABLEOFBAPIRET2,
WA_RETURNTYPEBAPIRET2.
LOOPATIT_MARAINTOWA_MARA.
WA_HEADDATAMATERIAL=WA_MARAMATNR."materialno
WA_HEADDATAIND_SECTOR=WA_MARAMBRSH."industrysector
WA_HEADDATAMATL_TYPE=WA_MARAMTART."materialtype
WA_HEADDATABASIC_VIEW='X'."basicview
WA_CLIENTDATAMATL_GROUP=WA_MARAMATKL."materialgroup
WA_CLIENTDATABASE_UOM=WA_MARAMEINS."baseUnitOfmeasure
WA_CLIENTDATAXMATL_GROUP='X'."passingmaterialgroup
WA_CLIENTDATAXBASE_UOM='X'."passingbaseunitofmeasure
WA_MATERIALDESCRIPTIONLANGU='EN'."englishlanguage
WA_MATERIALDESCRIPTIONLANGU_ISO='EN'."ISOlanguage
WA_MATERIALDESCRIPTIONMATL_DESC=WA_MARAMAKTX."materialdescriptions
APPENDWA_MATERIALDESCRIPTIONTOIT_MATERIALDESCRIPTION.
CLEARWA_MATERIALDESCRIPTION.
**Create/updatematerialsusingBAPI_MATERIAL_SAVEDATA
CALLFUNCTION'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

https://www.sapnuts.com/courses/SAPCrossApplications/bapisinsap/usingbapimaterial.html

4/10

5/4/2015

UsingBAPItoMigrateMaterialMasterBAPI`sinSAP| SAPNuts

*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=
.
IFWA_RETURNTYPE='E'.
CALLFUNCTION'BAPI_TRANSACTION_ROLLBACK'.
WRITE:/WA_HEADDATAMATERIAL,'isnotcreated'COLOR3.
ELSE.
CALLFUNCTION'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT='X'.
IFSYSUBRCEQ0.
WRITE:/WA_HEADDATAMATERIAL,'iscreated'COLOR5.
ENDIF.
ENDIF.
ENDLOOP.

Final code will be


REPORTZSAPN_MATERIAL_BAPI.
*BAPIdeclarations
DATA:WA_HEADDATATYPEBAPIMATHEAD,
WA_CLIENTDATATYPEBAPI_MARA,
WA_CLIENTDATAXTYPEBAPI_MARAX.
DATA:IT_MATERIALDESCRIPTIONTYPETABLEOFBAPI_MAKT,
WA_MATERIALDESCRIPTIONTYPEBAPI_MAKT.
DATA:IT_RETURNTYPETABLEOFBAPIRET2,
WA_RETURNTYPEBAPIRET2.
TYPES:BEGINOFTY_MARA,"internaltableasperflatfilestructure
MATNRTYPEMARAMATNR,
MTARTTYPEMARAMTART,
MBRSHTYPEMARAMBRSH,
MATKLTYPEMARAMATKL,

https://www.sapnuts.com/courses/SAPCrossApplications/bapisinsap/usingbapimaterial.html

5/10

5/4/2015

UsingBAPItoMigrateMaterialMasterBAPI`sinSAP| SAPNuts

MEINSTYPEMARAMEINS,
MAKTXTYPEMAKTMAKTX,
ENDOFTY_MARA.
DATA:IT_MARATYPETABLEOFTY_MARA,
WA_MARATYPETY_MARA.
**selectionscreen
PARAMETERS:P_FILETYPERLGRAPFILENAME."fileinput
ATSELECTIONSCREENONVALUEREQUESTFORP_FILE."f4helopforfileinput
PERFORMFILE_HELP.
ATSELECTIONSCREENONHELPREQUESTFORP_FILE."F1helpforfileinput
MESSAGE'Pleasepressf4toselectfile'TYPE'I'.
STARTOFSELECTION.
DATA:LV_FILETYPESTRING.
LV_FILE=P_FILE."toavoidtypeconflict
CALLFUNCTION'GUI_UPLOAD'"uploadflatfile
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"internaltable
*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

https://www.sapnuts.com/courses/SAPCrossApplications/bapisinsap/usingbapimaterial.html

6/10

5/4/2015

UsingBAPItoMigrateMaterialMasterBAPI`sinSAP| SAPNuts

*UNKNOWN_DP_ERROR=12
*ACCESS_DENIED=13
*DP_OUT_OF_MEMORY=14
*DISK_FULL=15
*DP_TIMEOUT=16
*OTHERS=17
.
IFSYSUBRC<>0.
*Implementsuitableerrorhandlinghere
ENDIF.
LOOPATIT_MARAINTOWA_MARA.
WA_HEADDATAMATERIAL=WA_MARAMATNR."materialno
WA_HEADDATAIND_SECTOR=WA_MARAMBRSH."industrysector
WA_HEADDATAMATL_TYPE=WA_MARAMTART."materialtype
WA_HEADDATABASIC_VIEW='X'."basicview
WA_CLIENTDATAMATL_GROUP=WA_MARAMATKL."materialgroup
WA_CLIENTDATABASE_UOM=WA_MARAMEINS."baseUnitOfmeasure
WA_CLIENTDATAXMATL_GROUP='X'."passingmaterialgroup
WA_CLIENTDATAXBASE_UOM='X'."passingbaseunitofmeasure
WA_MATERIALDESCRIPTIONLANGU='EN'."englishlanguage
WA_MATERIALDESCRIPTIONLANGU_ISO='EN'."ISOlanguage
WA_MATERIALDESCRIPTIONMATL_DESC=WA_MARAMAKTX."materialdescriptions
APPENDWA_MATERIALDESCRIPTIONTOIT_MATERIALDESCRIPTION.
CLEARWA_MATERIALDESCRIPTION.
**Create/updatematerialsusingBAPI_MATERIAL_SAVEDATA
CALLFUNCTION'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=''

https://www.sapnuts.com/courses/SAPCrossApplications/bapisinsap/usingbapimaterial.html

7/10

5/4/2015

UsingBAPItoMigrateMaterialMasterBAPI`sinSAP| SAPNuts

*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=
.
IFWA_RETURNTYPE='E'.
CALLFUNCTION'BAPI_TRANSACTION_ROLLBACK'.
WRITE:/WA_HEADDATAMATERIAL,'isnotcreated'COLOR3.
ELSE.
CALLFUNCTION'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT='X'.
IFSYSUBRCEQ0.
WRITE:/WA_HEADDATAMATERIAL,'iscreated'COLOR5.
ENDIF.
ENDIF.
ENDLOOP.
FORMFILE_HELP.
CALLFUNCTION'KD_GET_FILENAME_ON_F4'"getfilenameonF4help
*EXPORTING
*PROGRAM_NAME=SYSTREPID
*DYNPRO_NUMBER=SYSTDYNNR
*FIELD_NAME=''
*STATIC=''
*MASK=''
*FILEOPERATION='R'
*PATH=
CHANGING
FILE_NAME=P_FILE
*LOCATION_FLAG='P'
*EXCEPTIONS
*MASK_TOO_LONG=1
*OTHERS=2
.
IFSYSUBRC<>0.
*Implementsuitableerrorhandlinghere
ENDIF.
ENDFORM."FILE_HELP

https://www.sapnuts.com/courses/SAPCrossApplications/bapisinsap/usingbapimaterial.html

8/10

5/4/2015

UsingBAPItoMigrateMaterialMasterBAPI`sinSAP| SAPNuts

Testing
Download the flat file https://docs.google.com/file/d/0B5cU7rtWrKNlczFHRHBEOFROWFE/edit and test the
above program.

Lesson Navigation
Creating BAPI and Business Object /courses/SAPCrossApplications/bapisinsap/creatingbapi.html
Previous Chapter
Next Lesson ALEIDOCS Development /courses/SAPCrossApplications/aleidocsdevelopment.html
Was this lesson helpful to you? Yes

No 25 People out of 29 think this lesson helpful


Ask a Question ?

Lessons under SAP Cross Applications


SAP Cross Applications Introduction /courses/SAPCrossApplications/sapcrossapplications.html
RFC Development /courses/SAPCrossApplications/rfcdevelopment.html
BAPI`s in SAP /courses/SAPCrossApplications/bapisinsap.html
ALEIDOCS Development /courses/SAPCrossApplications/aleidocsdevelopment.html

SAP Usefil Links


SAP ABAP Interview Questions /faq/?area=Core%20ABAP
SAP Web Dynpro ABAP interview Questions /faq/?area=WebDynpro%20ABAP
SAP Community Documents /docs/
SAP Certification Exams /certification/
SAP ABAP Tutorials /tutorials/

2015 SAPNuts.com. Contribute /data/ Terms /site/terms/ About /site/about/ Contact /site/contact/
Feedback

https://www.sapnuts.com/courses/SAPCrossApplications/bapisinsap/usingbapimaterial.html

9/10

5/4/2015

UsingBAPItoMigrateMaterialMasterBAPI`sinSAP| SAPNuts

https://www.sapnuts.com/courses/SAPCrossApplications/bapisinsap/usingbapimaterial.html

10/10

You might also like