Go on link..

link us with ...

Monday, January 3, 2011

Step-by-Step approach to write a BDC program



BDCs are one of the most important objects of an implementation project and they usually give shivers to young (junior) ABAPers. They are used to populate the R/3 database with legacy data. BDCs are an efficient way to populate large amounts of R/3 data using minimum resources and time. BDC programs also (must) ensure that data populated is accurate as the whole application/module runs on that data.

Before starting to code the BDC program, it is a must to understand the transaction into which data is to be populated. Flat files with data need to be studied well for the order, format and accuracy of data. The best way to do it is to have a functional consultant record the transaction (using SHDB - Transaction recorder) entering all the data that needs to get into the transaction. Another way is to do the recording ourselves by entering all the data given to us in the flat file. Once the recording is ready with a simulation of the transaction to be populated, the ABAPer can start writing code to populate the transaction.

Here is a sample BDC for uploading Material Master data (MM01). It gives the users options to choose the upload method (Session/Call Txn), and also accepts files in DAT, XLS, TXT format, with header lines. Feel free to use it. What's more important to me is your suggestions. So do write back :)


REPORT ZKRP_BDC_MM01
NO STANDARD PAGE HEADING
LINE-SIZE 132
LINE-COUNT 65
MESSAGE-ID ZKRP_MSGCLS.

*&-------------------------------------------------------------------&*
* THIS PROGRAM SERVES AS A REUSABLE TEMPLATE AND IS FREE FOR USE *
* HOWEVER, USERS ARE REQUESTED TO PRESERVE THIS PART *
*&-------------------------------------------------------------------&*
* Program Name : ZKRP_BDC_MM01 *
* Description : Material Master (MM01 BDC) *
* Developer : Karthik Potharaju *
* Package : ZKAR *
* Description : *
* *
* PROGRAM FLOW *
* 1. Read input file *
* 2. Check the mandatory fields and if any one of them is missing *
* move the record to error file. *
* 3. Fill the bdcdata and call MM01 transaction *
* 4. If any errors format the message and send it for reporting *
* 5. If any errors, it downloads the error file *
* 6. Based on user input, this program does the following *
* a) Call transaction and in case of errors add to a session *
* b) Create session *
* 7. Display the Audit report *
*&-------------------------------------------------------------------&*

*&-------------------------------------------------------------------&*
* INTERNAL TABLE & STRUCTURE DEFINITIONS
*&-------------------------------------------------------------------&*
*- Internal table for input file for ASCII data load
*&-------------------------------------------------------------------&*
* The ASCII file is uploaded and the lines in it are read as strings *
* Later on, they are converted by separating fields using offsets *
*&-------------------------------------------------------------------&*

DATA:
BEGIN OF I_INFILE_ASC OCCURS 0,
STRING(1300) TYPE C,
END OF I_INFILE_ASC.

DATA:
*- Internal table to store messages from Call Transaction
I_BDCMSGCOLL TYPE STANDARD TABLE OF BDCMSGCOLL WITH HEADER LINE,
*- Internal table for BDC data
IT_BDCTAB TYPE BDCDATA OCCURS 0 WITH HEADER LINE,
*- Internal table to hold standard text (for long text)
I_TDLINES LIKE TLINE OCCURS 0 WITH HEADER LINE.

*- Internal to hold converted records from the text file for BDC data

*- upload (typical structure for MM01)
DATA :
BEGIN OF IT_DATA OCCURS 10,
MATNR(18), " Material Code
MBRSH(1), " Industry sector
MTART(4), " Material Type
WERKS(4), " Plant
LGORT(4), " Storage Location
MAKTX(40), " Material Description
MEINS(3), " Base unit of Measure
MATKL(9), " Material Group
BISMT(18), " Old Material Number
EKGRP(3), " Purchase grp
MFRPN(40), " Manufacturer part number
LGPBE(10), " Storage bin
DISMM(2), " MRP Type
DISPO(3), " MRP controller
MINBE(13), " Reorder Point
DISLS(2), " Lot size (materials planning)
MABST(17), " Maximum stock level
BESKZ(1), " Procurement type
PLIFZ(3), " Planned delivery time in days
FHORI(3), " Scheduling Margin Key for Floats
MTVFP(2), " Availability check
BKLAS(4), " Valuation Class
VPRSV(1), " Price Control
EKALR(1), " Mat. Is Costed with Qty Structure
HKMAT(1), " Material-related origin
LTEXT(1000), " Variable to hold Long Text
END OF IT_DATA.

*- Internal table to hold the long text
DATA:
BEGIN OF IT_LTEXT OCCURS 0,
MATNR(18),
LTEXT(982),
END OF IT_LTEXT.

*- Internal table to hold the error records
DATA:
BEGIN OF IT_ERROR OCCURS 0.
INCLUDE STRUCTURE IT_DATA.
DATA:
MESSAGE(80) TYPE C,
END OF IT_ERROR.

*- Internal table for session names
DATA:
BEGIN OF IT_SESSION OCCURS 0,
NAME(12),
END OF IT_SESSION.

*- Internal table to hold Collecting messages in the SAP System
DATA:
IT_MSG LIKE BDCMSGCOLL OCCURS 10 WITH HEADER LINE.

*- Internal table to hold error records to be downloaded
DATA:
BEGIN OF I_FINAL OCCURS 0,
STRING(1300),
END OF I_FINAL.

*- Internal table for session names
DATA:
BEGIN OF I_SESSION OCCURS 0,
NAME(12),
END OF I_SESSION.

DATA:
IT_INTERN TYPE ALSMEX_TABLINE OCCURS 0 WITH HEADER LINE.

*- Parameter string for runtime of CALL TRANSACTION USING..
DATA:
X_OPTIONS TYPE CTU_PARAMS.

*&-------------------------------------------------------------------&*
* CONSTANTS
*&-------------------------------------------------------------------&*

CONSTANTS:
C_FILETYPE LIKE RLGRAP-FILETYPE VALUE 'DAT', " File type
C_TCODE LIKE TSTC-TCODE VALUE 'MM01', " Transaction
C_MODE(1) VALUE 'A', " Mode
C_UPDATE(1) VALUE 'S', " Update
C_SESS(12) TYPE C VALUE 'MAT_SESS'. " Session Name

*&-------------------------------------------------------------------&*
* GLOBAL VARIABLES
*&-------------------------------------------------------------------&*


DATA:
GV_TABIX LIKE SY-TABIX, " Table Index
GV_MESG(80), " Message
GV_ERROR TYPE I, " Errors
GV_SUCCESS TYPE I, " Successes
GV_BDC_SESS TYPE APQI-GROUPID VALUE 'UPLOAD',
GV_MAX TYPE I VALUE '900', " Max records
GV_LINES TYPE I, " N.of records
GV_INSERT TYPE I,
GV_COUNTER(2) TYPE N, " Sessions
GF_OPENSESSION TYPE C,
GV_RECORDS TYPE I,
GV_EMPTY TYPE I, " Empty records
GV_TOTAL TYPE I, " Records with data
GV_SESSION TYPE APQI-GROUPID, " Session name
GV_DEL TYPE X VALUE '09', " Dat Delimiter
GV_SFILE(255), " File path
GV_EXT(3). " File Extension

*&-------------------------------------------------------------------&*
* SELECTION SCREEN.
*&-------------------------------------------------------------------&*


SELECTION-SCREEN BEGIN OF BLOCK SCR1 WITH FRAME TITLE TEXT-111.
PARAMETER:
P_FILE TYPE RLGRAP-FILENAME OBLIGATORY DEFAULT 'C:\MM01.TXT',
P_ERR TYPE RLGRAP-FILENAME OBLIGATORY DEFAULT 'C:\MM01_ERR.TXT',
P_FTYPE LIKE RLGRAP-FILETYPE DEFAULT 'DAT'.
SELECTION-SCREEN END OF BLOCK SCR1.

SELECTION-SCREEN BEGIN OF BLOCK MODE WITH FRAME TITLE TEXT-222.
PARAMETER:
P_CT RADIOBUTTON GROUP BDC, " Call Trans
P_BDC RADIOBUTTON GROUP BDC DEFAULT 'X'. " Create BDC sess

PARAMETER:
P_SESS1 TYPE APQI-GROUPID OBLIGATORY DEFAULT C_SESS.
" Session name
PARAMETER:
P_MAX(4) OBLIGATORY DEFAULT '100'. " Max. recs in a session
PARAMETERS:
P_HEADER TYPE I DEFAULT 0. " Header Lines
P_BEGCOL TYPE I DEFAULT 1 NO-DISPLAY,
P_BEGROW TYPE I DEFAULT 1 NO-DISPLAY,
P_ENDCOL TYPE I DEFAULT 100 NO-DISPLAY,
P_ENDROW TYPE I DEFAULT 32000 NO-DISPLAY,
P_TEST AS CHECKBOX DEFAULT 'X'. " Test mode

SELECTION-SCREEN END OF BLOCK MODE.

*&-------------------------------------------------------------------&*
* AT SELECTION SCREEN.
*&-------------------------------------------------------------------&*

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
*- Input File help (for upload path)
PERFORM FILE_HELP USING P_FILE.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_ERR.
*- Error File help (for download path)
PERFORM FILE_HELP USING P_ERR.

*&-------------------------------------------------------------------&*
* START-OF-SELECTION
*&-------------------------------------------------------------------&*

START-OF-SELECTION.

*- Perform to store the data to an internal table
PERFORM READ_INPUT_DATA.

*- Process data in non test mode only
IF P_TEST = SPACE.
IF NOT IT_DATA[] IS INITIAL.
PERFORM PROCESS_DATA.
ELSE.
MESSAGE S003(ZKRP_MSGCLS).
*- Setting flag to stop processing incase of data insufficiency
F_STOP = 'X'.
STOP.
ENDIF.
ENDIF.
*-------------------------------------------------------------------*
* END OF SELECTION.
*-------------------------------------------------------------------*

END-OF-SELECTION.

IF F_STOP = SPACE.
*- Perform to display the selection screen report
PERFORM DISPLAY_SEL_SCR_REPORT.
*- Perform to display the no.of records,success and error records
PERFORM DISPLAY_SUMMARY_REPORT.
*- Perform to display the session report
PERFORM SESSION_REPORT.
*- Downloading error records.
PERFORM DOWNLOAD_ERROR_FILES.
*- Perform to display the error records
PERFORM DISPLAY_ERROR_RECORDS.

ENDIF.

*-------------------------------------------------------------------*
* Form : FILE_HELP
*-------------------------------------------------------------------*
* Description : To provide F4 help for file if read from PC
*-------------------------------------------------------------------*

FORM FILE_HELP USING F_FILE TYPE RLGRAP-FILENAME.

*- The FM 'KD_GET_FILENAME_ON_F4' opens up a “browse for file” window

*- to select the input file
CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
CHANGING
FILE_NAME = F_FILE
EXCEPTIONS
MASK_TOO_LONG = 1
OTHERS = 2.

IF SY-SUBRC <> 0.
MESSAGE S010(ZKRP_MSGCLS). " 'Error in getting filename'.
ENDIF.

ENDFORM. " FILE_HELP

*&amp;amp;amp;amp;amp;--------------------------------------------------------------------*
*& Form READ_INPUT_DATA
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*

FORM READ_INPUT_DATA.

*- Splitting the filename and its extension
PERFORM SPLIT_FILENAME_EXT.

CASE GV_EXT.

WHEN 'XLS'.
*- Perform to convert the Excel data into an internal table
PERFORM CONVERT_XLS_ITAB.
WHEN 'TXT'.
IF P_FTYPE = 'ASC'.
*- Uploading the data from server
PERFORM READ_INPUTFILE TABLES I_INFILE_ASC
USING P_FILE P_FTYPE.
*- Perform Formating ASC type data
PERFORM FORMAT_ASC_DATA.
ELSEIF P_FTYPE = 'DAT'.
*- Uploading the data from server
PERFORM READ_INPUTFILE TABLES IT_DATA
USING P_FILE P_FTYPE.
ENDIF.
WHEN OTHERS.
MESSAGE S000(ZKRP_MSGCLS).
F_STOP = 'X'.
STOP.
ENDCASE.
IF NOT IT_DATA[] IS INITIAL.
*- Perform to delete Header lines
PERFORM DELETE_HEADER_EMPTY_RECS.
ENDIF.

ENDFORM. " READ_INPUT_DATA
*&--------------------------------------------------------------------*
*& Form SPLIT_FILENAME_EXT
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*

FORM SPLIT_FILENAME_EXT.

MOVE P_FILE TO GV_SFILE.
*- To get the extensions of the file
CALL FUNCTION 'CV120_SPLIT_FILE'
EXPORTING
PF_FILE = GV_SFILE
IMPORTING
PFX_EXTENSION = GV_EXT
.
*- Checking the case of file extension
IF GV_EXT NA SY-ABCDE.
TRANSLATE GV_EXT TO UPPER CASE.
ENDIF.


ENDFORM. " SPLIT_FILENAME_EXT

*&--------------------------------------------------------------------*
*& Form CONVER_XLS_ITAB
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*

FORM CONVERT_XLS_ITAB.

CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
FILENAME = P_FILE
I_BEGIN_COL = P_BEGCOL
I_BEGIN_ROW = P_BEGROW
I_END_COL = P_ENDCOL
I_END_ROW = P_ENDROW
TABLES
INTERN = IT_INTERN
EXCEPTIONS
INCONSISTENT_PARAMETERS = 1
UPLOAD_OLE = 2
OTHERS = 3
.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

*- Perform to move the data into an internal data
PERFORM MOVE_DATA.

ENDFORM. " CONVERT_XLS_ITAB

*&--------------------------------------------------------------------*
*& Form MOVE_DATA
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*

FORM MOVE_DATA.

DATA : LV_INDEX TYPE I.
FIELD-SYMBOLS .
*- Sorting the internal table
SORT IT_INTERN BY ROW COL.
CLEAR IT_INTERN.

LOOP AT IT_INTERN.
MOVE IT_INTERN-COL TO LV_INDEX.
*- Assigning the each record to an internal table row
ASSIGN COMPONENT LV_INDEX OF STRUCTURE IT_DATA TO .
*- Asigning the field value to a field symbol
MOVE IT_INTERN-VALUE TO .
AT END OF ROW.
APPEND IT_DATA.
CLEAR IT_DATA.
ENDAT.
ENDLOOP.

ENDFORM. " MOVE_DATA

*-------------------------------------------------------------------*
* Form : Read_inputfile
*-------------------------------------------------------------------*
* Description : To upload the data into internal table
*-------------------------------------------------------------------*

FORM READ_INPUTFILE TABLES F_INFILE
USING F_FILE TYPE RLGRAP-FILENAME
F_FILTYP TYPE RLGRAP-FILETYPE.
DATA : LV_DEL TYPE CHAR01,
LV_FILE TYPE STRING.
CLEAR F_INFILE.
REFRESH F_INFILE.
LV_FILE = F_FILE.

*- Function module to upload the input file
IF F_FILTYP = 'DAT'.
LV_DEL = ''.
ENDIF.

CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = LV_FILE
FILETYPE = 'ASC'
HAS_FIELD_SEPARATOR = LV_DEL
TABLES
DATA_TAB = F_INFILE
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.
MESSAGE I011(ZKRP_MSGCLS). " 'Error in uploading the file'.
F_STOP = 'X'.
STOP.
ENDIF.

ENDFORM. "Read_inputfile
*&--------------------------------------------------------------------*
*& Form FORMAT_ASC_DATA
*&--------------------------------------------------------------------*
* To format the ASCII data
*---------------------------------------------------------------------*

FORM FORMAT_ASC_DATA.

LOOP AT I_INFILE_ASC.

IT_DATA-MATNR = I_INFILE_ASC-STRING+0(18).
IT_DATA-MBRSH = I_INFILE_ASC-STRING+18(1).
IT_DATA-MTART = I_INFILE_ASC-STRING+19(4).
IT_DATA-WERKS = I_INFILE_ASC-STRING+23(4).
IT_DATA-LGORT = I_INFILE_ASC-STRING+27(4).
IT_DATA-MAKTX = I_INFILE_ASC-STRING+31(40).
IT_DATA-MEINS = I_INFILE_ASC-STRING+71(3).
IT_DATA-MATKL = I_INFILE_ASC-STRING+74(9).
IT_DATA-BISMT = I_INFILE_ASC-STRING+83(18).
IT_DATA-EKGRP = I_INFILE_ASC-STRING+101(3).
IT_DATA-MFRPN = I_INFILE_ASC-STRING+104(40).
IT_DATA-LGPBE = I_INFILE_ASC-STRING+144(10).
IT_DATA-DISMM = I_INFILE_ASC-STRING+154(2).
IT_DATA-DISPO = I_INFILE_ASC-STRING+156(3).
IT_DATA-MINBE = I_INFILE_ASC-STRING+159(13).
IT_DATA-DISLS = I_INFILE_ASC-STRING+172(2).
IT_DATA-MABST = I_INFILE_ASC-STRING+174(17).
IT_DATA-BESKZ = I_INFILE_ASC-STRING+191(1).
IT_DATA-PLIFZ = I_INFILE_ASC-STRING+192(3).
IT_DATA-FHORI = I_INFILE_ASC-STRING+195(3).
IT_DATA-MTVFP = I_INFILE_ASC-STRING+198(2).
IT_DATA-BKLAS = I_INFILE_ASC-STRING+200(4).
IT_DATA-VPRSV = I_INFILE_ASC-STRING+204(1).
IT_DATA-EKALR = I_INFILE_ASC-STRING+205(1).
IT_DATA-HKMAT = I_INFILE_ASC-STRING+206(1).
IT_DATA-LTEXT = I_INFILE_ASC-STRING+207(1000).

ENDLOOP.

ENDFORM. " FORMAT_ASC_DATA

*-------------------------------------------------------------------*
* Form FILE_VALIDATION
*-------------------------------------------------------------------*
* To validate data
*-------------------------------------------------------------------*

FORM FILE_VALIDATION.

DATA: LGV_TABIX LIKE SY-TABIX,
LV_FLAG(1).

LOOP AT IT_DATA.
CLEAR GV_MESG.
*- check for the mandatory fields
IF IT_DATA-MAKTX IS INITIAL
OR IT_DATA-MEINS IS INITIAL
OR IT_DATA-MATKL IS INITIAL
OR IT_DATA-EKGRP IS INITIAL
OR IT_DATA-DISMM IS INITIAL
OR IT_DATA-VPRSV IS INITIAL .


LGV_TABIX = SY-TABIX.
GV_MESG = 'Mandatory fields missing'(002).
*- Perform to store the error record
PERFORM ERROR_PROCESSING. " USING IT_DATA GV_MESG.
*- Deleting the record which is initial
DELETE IT_DATA INDEX LV_TABIX.
ENDIF.
ENDLOOP.
CLEAR GV_MESG.

ENDFORM. " DATA_VALIDATION
*&--------------------------------------------------------------------*
*& Form error_processing
*&--------------------------------------------------------------------*
* To process the error records
*---------------------------------------------------------------------*

FORM ERROR_PROCESSING.

GV_ERROR = GV_ERROR + 1.
IT_ERROR = IT_DATA.
IT_ERROR-MESSAGE = GV_MESG.
APPEND IT_ERROR.
CLEAR IT_ERROR.

ENDFORM. " error_processing
*-------------------------------------------------------------------*
* Form : process_data
*-------------------------------------------------------------------*
* --- Populating the BDC Data
*-------------------------------------------------------------------*

FORM PROCESS_DATA .

LOOP AT IT_DATA.

CLEAR :IT_BDCTAB,
I_BDCMSGCOLL.
REFRESH:IT_BDCTAB,
I_BDCMSGCOLL.
*- To fill the data from the text file
PERFORM FILL_BDCDATA.
IF P_CT EQ 'X'.
*- Posts data using call transaction method.
PERFORM CALL_TRANSACTION.
ELSE.
*- Posts data using session method.
PERFORM CALL_SESSION USING C_TCODE P_MAX P_SESS1.
ENDIF.

ENDLOOP.
IF GF_OPENSESSION = 'X'.
PERFORM CLOSE_SESSION.
ENDIF.

ENDFORM. " PROCESS_DATA

*&amp;amp;amp;amp;amp;--------------------------------------------------------------------*
*& Form FILL_BDCDATA
*&--------------------------------------------------------------------*
* To populate the data
*-------------------------------------------------------------------*

FORM FILL_BDCDATA.

*- MM01 first screen
PERFORM BDC_DYNPRO USING 'SAPLMGMM' '0060'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RMMG1-MATNR'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=AUSW'.
PERFORM BDC_FIELD USING 'RMMG1-MATNR' IT_DATA-MATNR.
PERFORM BDC_FIELD USING 'RMMG1-MBRSH' IT_DATA-MBRSH.
PERFORM BDC_FIELD USING 'RMMG1-MTART' IT_DATA-MTART.

*- MM01 view selection
PERFORM BDC_DYNPRO USING 'SAPLMGMM' '0070'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'MSICHTAUSW-DYTXT(15)'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '/00'.
PERFORM BDC_FIELD USING 'MSICHTAUSW-KZSEL(01)' 'X'.
PERFORM BDC_FIELD USING 'MSICHTAUSW-KZSEL(02)' 'X'.
PERFORM BDC_FIELD USING 'MSICHTAUSW-KZSEL(03)' 'X'.
PERFORM BDC_FIELD USING 'MSICHTAUSW-KZSEL(05)' 'X'.
PERFORM BDC_FIELD USING 'MSICHTAUSW-KZSEL(06)' 'X'.
PERFORM BDC_FIELD USING 'MSICHTAUSW-KZSEL(07)' 'X'.
PERFORM BDC_FIELD USING 'MSICHTAUSW-KZSEL(08)' 'X'.
PERFORM BDC_FIELD USING 'MSICHTAUSW-KZSEL(09)' 'X'.
PERFORM BDC_FIELD USING 'MSICHTAUSW-KZSEL(10)' 'X'.
PERFORM BDC_FIELD USING 'MSICHTAUSW-KZSEL(11)' 'X'.
PERFORM BDC_FIELD USING 'MSICHTAUSW-KZSEL(12)' 'X'.
PERFORM BDC_FIELD USING 'MSICHTAUSW-KZSEL(13)' 'X'.
PERFORM BDC_FIELD USING 'MSICHTAUSW-KZSEL(14)' 'X'.
PERFORM BDC_FIELD USING 'MSICHTAUSW-KZSEL(15)' 'X'.

*- MM01 first screen
PERFORM BDC_DYNPRO USING 'SAPLMGMM' '0080'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RMMG1-LGORT'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=ENTR'.
PERFORM BDC_FIELD USING 'RMMG1-WERKS' IT_DATA-WERKS.
PERFORM BDC_FIELD USING 'RMMG1-LGORT' IT_DATA-LGORT.

*- MM01 first screen
PERFORM BDC_DYNPRO USING 'SAPLMGMM' '4004'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=SP02'.
PERFORM BDC_FIELD USING 'MAKT-MAKTX' IT_DATA-MAKTX.
PERFORM BDC_FIELD USING 'MARA-BISMT' IT_DATA-BISMT.
PERFORM BDC_FIELD USING 'MARA-MEINS' IT_DATA-MEINS.
PERFORM BDC_FIELD USING 'MARA-MATKL' IT_DATA-MATKL.

*- MM01 first screen
PERFORM BDC_DYNPRO USING 'SAPLMGMM' '4004'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'MAKT-MAKTX'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=SP09'.

PERFORM BDC_DYNPRO USING 'SAPLMGMM' '4000'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=SP11'.
PERFORM BDC_FIELD USING 'MARC-EKGRP' IT_DATA-EKGRP.
PERFORM BDC_FIELD USING 'MARA-MFRPN' IT_DATA-MFRPN.

IF NOT IT_DATA-LTEXT IS INITIAL.
*- Perform to populate Long text
PERFORM POPULATE_LONG_TEXT.
ENDIF.

*- MM01 first screen
PERFORM BDC_DYNPRO USING 'SAPLMGMM' '4040'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=SP12'.

*- MM01 first screen
PERFORM BDC_DYNPRO USING 'SAPLMGMM' '4000'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=SP13'.
PERFORM BDC_FIELD USING 'MARC-DISMM' IT_DATA-DISMM.
PERFORM BDC_FIELD USING 'MARC-MINBE' IT_DATA-MINBE.
PERFORM BDC_FIELD USING 'MARC-DISPO' IT_DATA-DISPO.
PERFORM BDC_FIELD USING 'MARC-DISLS' IT_DATA-DISLS.
PERFORM BDC_FIELD USING 'MARC-MABST' IT_DATA-MABST.

*- MM01 first screen
PERFORM BDC_DYNPRO USING 'SAPLMGMM' '4000'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=SP14'.
PERFORM BDC_FIELD USING 'MARC-PLIFZ' IT_DATA-PLIFZ.
PERFORM BDC_FIELD USING 'MARC-FHORI' IT_DATA-FHORI.

*- MM01 first screen
PERFORM BDC_DYNPRO USING 'SAPLMGMM' '4000'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=SP15'.
PERFORM BDC_FIELD USING 'MARC-MTVFP' IT_DATA-MTVFP.

*- MM01 first screen
PERFORM BDC_DYNPRO USING 'SAPLMGMM' '4000'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'MAKT-MAKTX'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=SP19'.

*- MM01 first screen
PERFORM BDC_DYNPRO USING 'SAPLMGMM' '4000'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'MAKT-MAKTX'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=SP20'.
PERFORM BDC_FIELD USING 'MARD-LGPBE' IT_DATA-LGPBE.

*- MM01 first screen
PERFORM BDC_DYNPRO USING 'SAPLMGMM' '4000'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'MAKT-MAKTX'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=SP24'.

*- MM01 first screen
PERFORM BDC_DYNPRO USING 'SAPLMGMM' '4000'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'MBEW-BKLAS'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=SP25'.
PERFORM BDC_FIELD USING 'MBEW-BKLAS' IT_DATA-BKLAS.
PERFORM BDC_FIELD USING 'MBEW-VPRSV' IT_DATA-VPRSV.

*- MM01 first screen
PERFORM BDC_DYNPRO USING 'SAPLMGMM' '4000'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'MAKT-MAKTX'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=SP26'.

*- MM01 first screen

PERFORM BDC_DYNPRO USING 'SAPLMGMM' '4000'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'MAKT-MAKTX'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=SP27'.
PERFORM BDC_FIELD USING 'MBEW-EKALR' IT_DATA-EKALR.
PERFORM BDC_FIELD USING 'MBEW-HKMAT' IT_DATA-HKMAT.

*- MM01 first screen
PERFORM BDC_DYNPRO USING 'SAPLMGMM' '4000'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'MAKT-MAKTX'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=BU'.

ENDFORM. " FILL_BDCDATA

*-------------------------------------------------------------------*
* Form : call_transaction *
*-------------------------------------------------------------------*
*- Posts data using call transaction method. *
*-------------------------------------------------------------------*

FORM CALL_TRANSACTION .

CLEAR: V_LINES,
GV_MESG.
CLEAR X_OPTIONS.
X_OPTIONS-DISMODE = C_MODE.
X_OPTIONS-UPDMODE = C_UPDATE.
X_OPTIONS-DEFSIZE = 'X'.

*- call transaction method to upload data
CALL TRANSACTION C_TCODE USING IT_BDCTAB
OPTIONS FROM X_OPTIONS
MESSAGES INTO I_BDCMSGCOLL.

IF SY-SUBRC <> 0.
DESCRIBE TABLE I_BDCMSGCOLL LINES V_LINES.
READ TABLE I_BDCMSGCOLL INDEX GV_LINES TRANSPORTING ALL FIELDS.
*- Formats message returned by Call transaction.
PERFORM FORMAT_MESSAGE.
*- Perform to process error processing
PERFORM ERROR_PROCESSING." USING IT_DATA GV_MESG.
PERFORM CALL_SESSION USING C_TCODE P_MAX P_SESS1.
ELSE. "If call transaction is successful.
GV_SUCCESS = GV_SUCCESS + 1.
ENDIF.

ENDFORM. " call_transaction
*&--------------------------------------------------------------------*
*& Form format_message
*&--------------------------------------------------------------------*
*-----Reading messages
*---------------------------------------------------------------------*

FORM FORMAT_MESSAGE.
CLEAR GV_MESG.
*- Function module to get the message.
CALL FUNCTION 'FORMAT_MESSAGE'
EXPORTING
ID = I_BDCMSGCOLL-MSGID
LANG = SY-LANGU
NO = I_BDCMSGCOLL-MSGNR
V1 = I_BDCMSGCOLL-MSGV1
V2 = I_BDCMSGCOLL-MSGV2
V3 = I_BDCMSGCOLL-MSGV3
V4 = I_BDCMSGCOLL-MSGV4
IMPORTING
MSG = GV_MESG.

ENDFORM. " format_message
*-------------------------------------------------------------------*
* Form : call_session
*-------------------------------------------------------------------*
* Description : To call the session either for FSP0 or FSP2
*-------------------------------------------------------------------*

FORM CALL_SESSION USING FP_TCODE TYPE ANY
F_MAX TYPE ANY
F_SESS1 TYPE ANY.
*- To open the session
PERFORM OPEN_SESSION USING F_MAX F_SESS1.
*- Perform Insert BDC Session
PERFORM BDC_INSERT USING FP_TCODE.
*- To close the session
IF GV_INSERT = F_MAX .
PERFORM CLOSE_SESSION.
ENDIF.
ENDFORM. " call_session
*-------------------------------------------------------------------*
* Form : open_session
*-------------------------------------------------------------------*
* Description : Form to open BDC open group.
*-------------------------------------------------------------------*

FORM OPEN_SESSION USING F_MAX TYPE ANY
F_SESS1 TYPE ANY.

IF GV_INSERT = F_MAX OR GF_OPENSESSION = SPACE.
GV_COUNTER = GV_COUNTER + 1.
CLEAR GV_SESSION.
CONCATENATE F_SESS1 '_' GV_COUNTER INTO GV_SESSION.
* Opening the BDC Session
CALL FUNCTION 'BDC_OPEN_GROUP'
EXPORTING
CLIENT = SY-MANDT
GROUP = GV_SESSION
KEEP = 'X'
USER = SY-UNAME
EXCEPTIONS
CLIENT_INVALID = 1
DESTINATION_INVALID = 2
GROUP_INVALID = 3
GROUP_IS_LOCKED = 4
HOLDDATE_INVALID = 5
INTERNAL_ERROR = 6
QUEUE_ERROR = 7
RUNNING = 8
SYSTEM_LOCK_ERROR = 9
USER_INVALID = 10
OTHERS = 11.
IF SY-SUBRC <> 0.
MESSAGE I007(ZKRP_MSGCLS). " 'Error in Opening the Session'.
EXIT.
ELSE.
CLEAR V_INSERT.
GF_OPENSESSION = 'X'.
ENDIF.
ENDIF.
ENDFORM. " open_session
*-------------------------------------------------------------------*
* Form : bdc_insert
*-------------------------------------------------------------------*
* Description : Form to Insert data.
*-------------------------------------------------------------------*

FORM BDC_INSERT USING FP_TCODE TYPE ANY.
* Inserting BDC
CALL FUNCTION 'BDC_INSERT'
EXPORTING
TCODE = FP_TCODE
TABLES
DYNPROTAB = IT_BDCTAB
EXCEPTIONS
INTERNAL_ERROR = 1
NOT_OPEN = 2
QUEUE_ERROR = 3
TCODE_INVALID = 4
PRINTING_INVALID = 5
POSTING_INVALID = 6
OTHERS = 7.
IF SY-SUBRC <> 0.
MESSAGE I008(ZKRP_MSGCLS). " 'Error in inserting the record'.
ELSE.
GV_INSERT = GV_INSERT + 1.
ENDIF.
ENDFORM. " bdc_insert
*-------------------------------------------------------------------*
* Form : close_session
*-------------------------------------------------------------------*
* Description : To close the session
*-------------------------------------------------------------------*
FORM CLOSE_SESSION.
CALL FUNCTION 'BDC_CLOSE_GROUP'
EXCEPTIONS
NOT_OPEN = 1
QUEUE_ERROR = 2
OTHERS = 3.

IF SY-SUBRC <> 0.
MESSAGE I009(ZKRP_MSGCLS). " 'Session cannot be closed'.
ELSE.
I_SESSION-NAME = GV_SESSION.
APPEND I_SESSION.
CLEAR I_SESSION.
CLEAR F_OPENSESSION.
ENDIF.
ENDFORM. " close_session
*&--------------------------------------------------------------------*
*& Form DISPLAY_SUCCESS_ERROR_DATA
*&--------------------------------------------------------------------*
* To display the no.of records,success and error records
*-------------------------------------------------------------------*
FORM DISPLAY_SUMMARY_REPORT.

SKIP 3.
FORMAT COLOR COL_HEADING INTENSIFIED ON.
WRITE :/1(132) SY-ULINE.
WRITE :/1(1) SY-VLINE,
65 'SUMMARY REPORT'(003),
132(1) SY-VLINE.
FORMAT INTENSIFIED OFF.
PERFORM COLOR_FILL.
WRITE :/1(132) SY-ULINE.
WRITE :/1(1) SY-VLINE,
2(24) 'Processed Records'(004),
27(2) ' : ',
30 GV_TOTAL ,
132(1) SY-VLINE.
PERFORM COLOR_FILL.
WRITE :/1(1) SY-VLINE,
2(24) 'Successfully Uploaded'(005),
27(2) ' : ',
30 V_SUCCESS,
132(1) SY-VLINE.
PERFORM COLOR_FILL.
WRITE :/1(1) SY-VLINE,
2(24) 'Error Records'(006),
27(2) ' : ',
30 V_ERROR,
132(1) SY-VLINE.
PERFORM COLOR_FILL.
WRITE :/1(1) SY-VLINE,
2(24) 'Empty Records'(007),
27(2) ' : ',
30 GV_EMPTY,
132(1) SY-VLINE.
WRITE :/1(132) SY-ULINE.

ENDFORM. " DISPLAY_SUCCESS_ERROR_DATA

*&--------------------------------------------------------------------*
*& Form DISPLAY_ERROR_RECORDS
*&--------------------------------------------------------------------*
* To display the error records
*-------------------------------------------------------------------*
FORM DISPLAY_ERROR_RECORDS.

IF GV_ERROR > 0.
*- Perform to display the error header
PERFORM DISPLAY_ERROR_HEADER.
*- Perform to display the error records
PERFORM DISPLAY_ERROR_DETAIL.
ENDIF.

ENDFORM. " DISPLAY_ERROR_RECORDS

*&--------------------------------------------------------------------*
*& Form DISPLAY_ERROR_HEADER
*&--------------------------------------------------------------------*
* To display the error header
*-------------------------------------------------------------------*
FORM DISPLAY_ERROR_HEADER.

SKIP.
WRITE :/1(132) SY-ULINE.
WRITE :/1(1) SY-VLINE,
40 'ERROR RECORDS'(008),
132(1) SY-VLINE.
WRITE :/1(132) SY-ULINE.
WRITE : /1(1) SY-VLINE,
2(18) 'Material Number'(009),
20(1) SY-VLINE,
21(4) 'Type'(010),
25(1) SY-VLINE,
26(5) 'Plant'(011),
31(1) SY-VLINE,
32(16) 'storage location'(012),
48(1) SY-VLINE,
49(80) 'Message'(013),
132(1) SY-VLINE.


WRITE :/1(132) SY-ULINE.

ENDFORM. " DISPLAY_ERROR_HEADER

*&--------------------------------------------------------------------*
*& Form DISPLAY_ERROR_DETAIL
*&--------------------------------------------------------------------*
* To display the error records
*-------------------------------------------------------------------*
FORM DISPLAY_ERROR_DETAIL.

LOOP AT IT_ERROR.
WRITE : /1(1) SY-VLINE,
2(18) IT_ERROR-MATNR,
20(1) SY-VLINE,
21(4) IT_ERROR-MTART,
25(1) SY-VLINE,
26(5) IT_ERROR-WERKS,
31(1) SY-VLINE,
32(16) IT_ERROR-LGORT,
48(1) SY-VLINE,
49(80) IT_ERROR-MESSAGE,
132(1) SY-VLINE.

ENDLOOP.
WRITE :/1(132) SY-ULINE.

ENDFORM. " DISPLAY_ERROR_DETAIL
*-------------------------------------------------------------------*
* Form DOWNLOAD_ERROR_FILES
*-------------------------------------------------------------------*
*- Downloading the error records to erro file
*-------------------------------------------------------------------*
FORM DOWNLOAD_ERROR_FILES.

DATA : LV_DEL TYPE CHAR01,
LV_FILE TYPE STRING.

IF NOT IT_ERROR[] IS INITIAL.
IF P_FTYPE = 'DAT'.
LOOP AT IT_ERROR.

CONCATENATE IT_ERROR-MATNR
IT_ERROR-MBRSH
IT_ERROR-MTART
IT_ERROR-WERKS
IT_ERROR-LGORT
IT_ERROR-MAKTX
IT_ERROR-MEINS
IT_ERROR-MATKL
IT_ERROR-BISMT
IT_ERROR-EKGRP
IT_ERROR-MFRPN
IT_ERROR-LGPBE
IT_ERROR-DISMM
IT_ERROR-DISPO
IT_ERROR-MINBE
IT_ERROR-DISLS
IT_ERROR-MABST
IT_ERROR-BESKZ
IT_ERROR-PLIFZ
IT_ERROR-FHORI
IT_ERROR-MTVFP
IT_ERROR-BKLAS
IT_ERROR-VPRSV
IT_ERROR-EKALR
IT_ERROR-HKMAT
IT_ERROR-LTEXT
INTO I_FINAL-STRING SEPARATED BY V_DEL.
APPEND I_FINAL.
CLEAR I_FINAL.
ENDLOOP.
CLEAR I_FINAL.
ELSEIF P_FTYPE = 'ASC'.
LOOP AT IT_ERROR.
I_FINAL-STRING+0(18) = IT_ERROR-MATNR.
I_FINAL-STRING+18(1) = IT_ERROR-MBRSH.
I_FINAL-STRING+19(4) = IT_ERROR-MTART.
I_FINAL-STRING+23(4) = IT_ERROR-WERKS.
I_FINAL-STRING+27(4) = IT_ERROR-LGORT.
I_FINAL-STRING+31(40) = IT_ERROR-MAKTX.
I_FINAL-STRING+71(3) = IT_ERROR-MEINS.
I_FINAL-STRING+74(9) = IT_ERROR-MATKL.
I_FINAL-STRING+83(18) = IT_ERROR-BISMT.
I_FINAL-STRING+101(3) = IT_ERROR-EKGRP.
I_FINAL-STRING+104(40) = IT_ERROR-MFRPN.
I_FINAL-STRING+144(10) = IT_ERROR-LGPBE.
I_FINAL-STRING+154(2) = IT_ERROR-DISMM.
I_FINAL-STRING+156(3) = IT_ERROR-DISPO.
I_FINAL-STRING+159(13) = IT_ERROR-MINBE.
I_FINAL-STRING+172(2) = IT_ERROR-DISLS.
I_FINAL-STRING+174(17) = IT_ERROR-MABST.
I_FINAL-STRING+191(1) = IT_ERROR-BESKZ.
I_FINAL-STRING+192(3) = IT_ERROR-PLIFZ.
I_FINAL-STRING+195(3) = IT_ERROR-FHORI.
I_FINAL-STRING+198(2) = IT_ERROR-MTVFP.
I_FINAL-STRING+200(4) = IT_ERROR-BKLAS.
I_FINAL-STRING+204(1) = IT_ERROR-VPRSV.
I_FINAL-STRING+205(1) = IT_ERROR-EKALR.
I_FINAL-STRING+206(1) = IT_ERROR-HKMAT.
I_FINAL-STRING+207(1000) = IT_ERROR-LTEXT.

APPEND I_FINAL.
CLEAR I_FINAL.
ENDLOOP.
ENDIF. "IF P_FTYPE = 'DAT'.

LV_FILE = P_ERR.

IF P_FTYPE = 'DAT'.
LV_DEL = ''.
ENDIF.

CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
FILENAME = LV_FILE
FILETYPE = 'ASC'
WRITE_FIELD_SEPARATOR = LV_DEL
TABLES
DATA_TAB = I_FINAL
EXCEPTIONS
FILE_WRITE_ERROR = 1
NO_BATCH = 2
GUI_REFUSE_FILETRANSFER = 3
INVALID_TYPE = 4
NO_AUTHORITY = 5
UNKNOWN_ERROR = 6
HEADER_NOT_ALLOWED = 7
SEPARATOR_NOT_ALLOWED = 8
FILESIZE_NOT_ALLOWED = 9
HEADER_TOO_LONG = 10
DP_ERROR_CREATE = 11
DP_ERROR_SEND = 12
DP_ERROR_WRITE = 13
UNKNOWN_DP_ERROR = 14
ACCESS_DENIED = 15
DP_OUT_OF_MEMORY = 16
DISK_FULL = 17
DP_TIMEOUT = 18
FILE_NOT_FOUND = 19
DATAPROVIDER_EXCEPTION = 20
CONTROL_FLUSH_ERROR = 21
OTHERS = 22.
IF SY-SUBRC <> 0.
MESSAGE I012(ZKRP_MSGCLS). " Error in downloading the file.
ENDIF.

ENDIF. "IF NOT I_FILE_ERROR[] IS INITIAL.

ENDFORM. " DOWNLOAD_ERROR_FILES

*&amp;amp;amp;amp;amp;--------------------------------------------------------------------*
*& Form POPULATE_LONG_TEXT
*&--------------------------------------------------------------------*
* text
*-------------------------------------------------------------------*
FORM POPULATE_LONG_TEXT .

DATA: LV_LNGTEXT(72).

DATA: LV_STRING(1000),
LEN TYPE I,
I_POS TYPE I,
LV_LNLEN TYPE I,
LV_LINE(2) TYPE N,
LV_FIRST,
LV_LNCNT TYPE I,
LV_FLAG.

LV_LNLEN = 72.
LV_STRING = IT_DATA-LTEXT.
LEN = STRLEN( LV_STRING ).
PERFORM BDC_DYNPRO USING 'SAPLMGMM' '4040'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=LTEX'.
LV_LINE = 2.
DO.
IF LV_FLAG = SPACE.
LEN = STRLEN( LV_STRING+I_POS ).
ENDIF.
IF LEN < lv_lnlen =" LEN." lv_lngtext =" LV_STRING+I_POS(LV_LNLEN)." lv_first =" SPACE." lv_first =" 'X'." lv_lnlen =" 72." lv_flag =" 'X'." i_pos =" I_POS" lv_lncnt =" LV_LNCNT" lv_line =" LV_LINE" gv_ext =" 'XLS'." p_err =" P_ERR(SY-FDPOS)." p_ftype =" 'DAT'." gv_del =" '2C'." gv_ext =" 'XLS'." p_ct =" 'X'." f_color =" SPACE." f_color =" 'X'."> 0 AND NOT IT_DATA IS INITIAL.
DELETE IT_DATA FROM 1 TO P_HEADER.
EXIT.
ENDIF.
ENDLOOP.
ENDIF.

CLEAR IT_DATA.
*- To delete the empty lines from internal table
LOOP AT IT_DATA.
LGV_TABIX = SY-TABIX.
IF IT_DATA IS INITIAL.
GV_EMPTY = GV_EMPTY + 1.
DELETE IT_DATA INDEX LV_TABIX..
ENDIF.
ENDLOOP.

CLEAR IT_DATA.
*- Total no of recs in file
DESCRIBE TABLE IT_DATA LINES GV_TOTAL .
IF GV_TOTAL = 0.
MESSAGE I013(ZKRP_MSGCLS). " No records in the file
F_STOP = 'X'.
STOP.
ENDIF.

ENDFORM. " DELETE_HEADER_EMPTY_RECS

*-------------------------------------------------------------------*
* Start new screen *
*-------------------------------------------------------------------*
FORM BDC_DYNPRO USING F_PROGRAM F_DYNPRO.
CLEAR IT_BDCTAB.
IT_BDCTAB-PROGRAM = F_PROGRAM.
IT_BDCTAB-DYNPRO = F_DYNPRO.
IT_BDCTAB-DYNBEGIN = 'X'.
APPEND IT_BDCTAB.
ENDFORM. "BDC_DYNPRO

*-------------------------------------------------------------------*
* Insert field
*-------------------------------------------------------------------*
FORM BDC_FIELD USING FNAM FVAL.
IF FVAL <> SPACE.
CLEAR IT_BDCTAB.
IT_BDCTAB-FNAM = FNAM.
IT_BDCTAB-FVAL = FVAL.
APPEND IT_BDCTAB.
ENDIF.

ENDFORM. "BDC_FIELD

2 comments:

Div said...

Thanks for giving each step in detail, its really helpful people who don't have an idea about BDC.

Business World said...

The information provided here is of great use as I got to learn new things. Keep blogging.
SAP ABAP TRAINING IN HYDERABAD

link us...

For Visitors

if you want to publish or Add something on ERP, SAP , SAP FUNCTIONAL, SAP ABAP then mail us along with your email-id. contain must be yours

email-id :- avinashkr_raj@yahoo.com(any email)

email-id :- avinaskr_raj.abap@blogger.com ( use only gmail)