Powered By

Free XML Skins for Blogger

Powered by Blogger

Thursday, January 8, 2009

SAP ABAP BDC SAMPLE CODE XD01

*----------------------------------------------------------------------*
* Table/Structure declarations. *
*----------------------------------------------------------------------*

TABLES : KNA1. "Customer master

*----------------------------------------------------------------------*
* Constants declarations. *
*----------------------------------------------------------------------*

CONSTANTS : C_MODE VALUE 'A',
C_UPDATE VALUE 'S',
C_X VALUE 'X',
C_SESS TYPE APQI-GROUPID VALUE 'ZCUSTOMER', "Session Name
C_XD01 LIKE TSTC-TCODE VALUE 'XD01'.

*----------------------------------------------------------------------*
* Variable declarations. *
*----------------------------------------------------------------------*

DATA : V_FNAME(15) VALUE SPACE, " Name of file to be created
V_FAILREC TYPE I, " No of failed records
V_MSG(255), " Message Text
V_ERRREC TYPE I, " No of failed records
V_LINES TYPE I, " No of records
V_BANKS(15), " Table column BANKS
V_BANKL(15), " Table column BANKL
V_BANKN(15), " Table column BANKN
V_TIDX(2) TYPE N. " Table row index



*----------------------------------------------------------------------*
*-- FLAG DECLARATIONS
*----------------------------------------------------------------------*

DATA : FG_DATA_EXIST VALUE 'X', " Check for data
FG_SESSION_OPEN VALUE ' '. " Check for Session Open

*----------------------------------------------------------------------*
*-- MACRO DEFINITIONS
*----------------------------------------------------------------------*

*-- Macro for BANKS
DEFINE BANKS.
CLEAR V_BANKS.
CONCATENATE 'KNBK-BANKS(' &1 ')' INTO V_BANKS.
CONDENSE V_BANKS.
END-OF-DEFINITION.


*-- Macro for BANKL
DEFINE BANKL.
CLEAR V_BANKL.
CONCATENATE 'KNBK-BANKL(' &1 ')' INTO V_BANKL.
CONDENSE V_BANKL.
END-OF-DEFINITION.


*-- Macro for BANKN
DEFINE BANKN.
CLEAR V_BANKN.
CONCATENATE 'KNBK-BANKN(' &1 ')' INTO V_BANKN.
CONDENSE V_BANKN.
END-OF-DEFINITION.



*----------------------------------------------------------------------*
* Structures / Internal table declarations *
*----------------------------------------------------------------------*

*-- Structure to hold BDC data
TYPES : BEGIN OF T_BDCTABLE.
INCLUDE STRUCTURE BDCDATA.
TYPES END OF T_BDCTABLE.

*-- Structure to trap BDC messages
TYPES : BEGIN OF T_MSG.
INCLUDE STRUCTURE BDCMSGCOLL.
TYPES : END OF T_MSG.

*-- Structure to trap ERROR messages
TYPES : BEGIN OF T_ERR_MSG,
MESSAGE(255),
END OF T_ERR_MSG.


*--Internal table to store flat file data
DATA : BEGIN OF IT_KNA1 OCCURS 0,
KUNNR LIKE KNA1-KUNNR,
KTOKD LIKE T077D-KTOKD,
NAME1 LIKE KNA1-NAME1,
SORTL LIKE KNA1-SORTL,
ORT01 LIKE KNA1-ORT01,
PSTLZ LIKE KNA1-PSTLZ,
LAND1 LIKE KNA1-LAND1,
SPRAS LIKE KNA1-SPRAS,
LZONE LIKE KNA1-LZONE,
END OF IT_KNA1.

*--Internal table to store bank details
DATA : BEGIN OF IT_BANK OCCURS 0,
BANKS LIKE KNBK-BANKS,
BANKL LIKE KNBK-BANKL,
BANKN LIKE KNBK-BANKN,
END OF IT_BANK.

*-- Internal table to hold BDC data
DATA: IT_BDCDATA TYPE STANDARD TABLE OF T_BDCTABLE WITH HEADER LINE,

*-- Internal Table to store ALL messages
IT_MSG TYPE STANDARD TABLE OF T_MSG WITH HEADER LINE,

*-- Internal Table to store error messages
IT_ERR_MSG TYPE STANDARD TABLE OF T_ERR_MSG WITH HEADER LINE.


*----------------------------------------------------------------------*
* Selection Screen. *
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.

PARAMETERS : P_FLNAME(15) OBLIGATORY.

SELECTION-SCREEN END OF BLOCK B1.

SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME TITLE TEXT-002.

SELECTION-SCREEN BEGIN OF LINE.

PARAMETERS : R_LIST RADIOBUTTON GROUP GRP1.
SELECTION-SCREEN COMMENT 5(20) TEXT-003.

PARAMETERS : R_SESS RADIOBUTTON GROUP GRP1.
SELECTION-SCREEN COMMENT 30(20) TEXT-004.

SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN END OF BLOCK B2.



*----------------------------------------------------------------------*
* Event:Initialization *
*----------------------------------------------------------------------*
INITIALIZATION.

*----------------------------------------------------------------------*
* AT Selection Screen. *
*----------------------------------------------------------------------*
AT SELECTION-SCREEN.

*----------------------------------------------------------------------*
* Event: Start-of-Selection *
*----------------------------------------------------------------------*
START-OF-SELECTION.

V_FNAME = P_FLNAME.
PERFORM GET_DATA.
PERFORM GET_BANKDATA.
PERFORM GENERATE_DATASET.

*----------------------------------------------------------------------*
* Event: End-of-Selection *
*----------------------------------------------------------------------*
END-OF-SELECTION.

IF FG_DATA_EXIST = ' '.
MESSAGE I010 WITH TEXT-009.
EXIT.
ENDIF.

PERFORM GENERATE_BDCDATA.
PERFORM DISPLAY_ERR_RECS.

*----------------------------------------------------------------------*
* Event: top-of-page
*----------------------------------------------------------------------*
TOP-OF-PAGE.


*//////////////////////////////////////////////////////////////////////*
*
* FORM DEFINITIONS *
*
*//////////////////////////////////////////////////////////////////////
*&---------------------------------------------------------------------*
*& Form get_data
*&---------------------------------------------------------------------*
* Subroutine to get the data from mard
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM GET_DATA.

CALL FUNCTION 'UPLOAD'
EXPORTING
* CODEPAGE = ' '
FILENAME = 'C:\XD01.TXT'
FILETYPE = 'DAT'
* ITEM = ' '
* FILEMASK_MASK = ' '
* FILEMASK_TEXT = ' '
* FILETYPE_NO_CHANGE = ' '
* FILEMASK_ALL = ' '
* FILETYPE_NO_SHOW = ' '
* LINE_EXIT = ' '
* USER_FORM = ' '
* USER_PROG = ' '
* SILENT = 'S'
* IMPORTING
* FILESIZE =
* CANCEL =
* ACT_FILENAME =
* ACT_FILETYPE =
TABLES
DATA_TAB = IT_KNA1
* EXCEPTIONS
* CONVERSION_ERROR = 1
* INVALID_TABLE_WIDTH = 2
* INVALID_TYPE = 3
* NO_BATCH = 4
* UNKNOWN_ERROR = 5
* GUI_REFUSE_FILETRANSFER = 6
* OTHERS = 7
.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.


IF IT_KNA1[] IS INITIAL.
FG_DATA_EXIST = ' '.
ENDIF.

ENDFORM. " get_data
*&---------------------------------------------------------------------*
*& Form GENERATE_DATASET
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM GENERATE_DATASET.

MESSAGE I010 WITH 'OPENING FILE IN APPLICATION SERVER'.

**--Creating a data set in application server

OPEN DATASET V_FNAME FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.


**---Transfering data from internal table to dataset

MESSAGE I010 WITH 'TRANSFERING DATA FROM INETERAL TABLE TO THE FILE'.

LOOP AT IT_KNA1.

TRANSFER IT_KNA1 TO V_FNAME.

ENDLOOP.

**--Closing the dataset

MESSAGE I010 WITH 'CLOSING THE FILE'.
CLOSE DATASET V_FNAME.

ENDFORM. " GENERATE_DATASET

*&---------------------------------------------------------------------*
*& Form BDC_DYNPRO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0467 text
* -->P_0468 text
*----------------------------------------------------------------------*
FORM BDC_DYNPRO USING PROGRAM DYNPRO.

CLEAR IT_BDCDATA.
IT_BDCDATA-PROGRAM = PROGRAM.
IT_BDCDATA-DYNPRO = DYNPRO.
IT_BDCDATA-DYNBEGIN = 'X'.
APPEND IT_BDCDATA.

ENDFORM.

*&---------------------------------------------------------------------*
*& Form BDC_FIELD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0472 text
* -->P_0473 text
*----------------------------------------------------------------------*
FORM BDC_FIELD USING FNAM FVAL.

IF NOT FVAL IS INITIAL.

CLEAR IT_BDCDATA.
IT_BDCDATA-FNAM = FNAM.
IT_BDCDATA-FVAL = FVAL.
APPEND IT_BDCDATA.

ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form GENERATE_BDCDATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM GENERATE_BDCDATA.


REFRESH IT_KNA1.
* Opening dataset for reading

OPEN DATASET V_FNAME FOR INPUT IN TEXT MODE ENCODING DEFAULT.

**** Reading the file from application server
DO.

CLEAR: IT_KNA1,IT_BDCDATA.
REFRESH IT_BDCDATA.

READ DATASET V_FNAME INTO IT_KNA1.

IF SY-SUBRC <> 0.

EXIT.

ELSE.
* Populate BDC Data for Initial Screen
PERFORM : BDC_DYNPRO USING 'SAPMF02D' '0100',
BDC_FIELD USING 'BDC_CURSOR' 'RF02D-KUNNR',
BDC_FIELD USING 'BDC_OKCODE' '/00',
BDC_FIELD USING 'RF02D-KUNNR' IT_KNA1-KUNNR,
BDC_FIELD USING 'RF02D-KTOKD' IT_KNA1-KTOKD.

* Populate BDC Data for Second Screen
PERFORM : BDC_DYNPRO USING 'SAPMF02D' '0110',
BDC_FIELD USING 'BDC_CURSOR' 'KNA1-NAME1',
BDC_FIELD USING 'BDC_OKCODE' '/00',
BDC_FIELD USING 'KNA1-NAME1' IT_KNA1-NAME1,
BDC_FIELD USING 'KNA1-SORTL' IT_KNA1-SORTL,
BDC_FIELD USING 'KNA1-ORT01' IT_KNA1-ORT01,
BDC_FIELD USING 'KNA1-PSTLZ' IT_KNA1-PSTLZ,
BDC_FIELD USING 'KNA1-LAND1' IT_KNA1-LAND1,
BDC_FIELD USING 'KNA1-SPRAS' IT_KNA1-SPRAS.

* Populate BDC Data for Third Screen
PERFORM : BDC_DYNPRO USING 'SAPMF02D' '0120',
BDC_FIELD USING 'BDC_CURSOR' 'KNA1-LZONE',
BDC_FIELD USING 'BDC_OKCODE' '/00',
BDC_FIELD USING 'KNA1-LZONE' IT_KNA1-LZONE.
* Populate BDC Data for Fourth Screen
PERFORM : BDC_DYNPRO USING 'SAPMF02D' '0125',
BDC_FIELD USING 'BDC_CURSOR' 'KNA1-NIELS',
BDC_FIELD USING 'BDC_OKCODE' '/00'.

* Populate BDC Data for Table control for bank details.
V_TIDX = '01'.
LOOP AT IT_BANK.

BANKS V_TIDX.
BANKL V_TIDX.
BANKN V_TIDX.

PERFORM : BDC_DYNPRO USING 'SAPMF02D' '0130',
BDC_FIELD USING 'BDC_CURSOR' V_BANKN,
BDC_FIELD USING 'BDC_OKCODE' '=ENTR',
BDC_FIELD USING V_BANKS IT_BANK-BANKS,
BDC_FIELD USING V_BANKL IT_BANK-BANKL,
BDC_FIELD USING V_BANKN IT_BANK-BANKN.

V_TIDX = V_TIDX + 1.

ENDLOOP.

PERFORM : BDC_DYNPRO USING 'SAPMF02D' '0130',
BDC_FIELD USING 'BDC_CURSOR' V_BANKS,
BDC_FIELD USING 'BDC_OKCODE' '=UPDA'.

CALL TRANSACTION C_XD01 USING IT_BDCDATA
MODE C_MODE
UPDATE C_UPDATE
MESSAGES INTO IT_MSG.

IF SY-SUBRC <> 0.
*--In case of error list display
IF R_LIST = C_X.

V_ERRREC = V_ERRREC + 1.
PERFORM FORMAT_MESSAGE.

IT_ERR_MSG-MESSAGE = V_MSG.
APPEND IT_ERR_MSG.

CLEAR : V_MSG,IT_ERR_MSG.

ENDIF.
*--In case of session log
IF R_SESS = C_X.
*-- In case of transaction fails.
IF FG_SESSION_OPEN = ' '.

FG_SESSION_OPEN = C_X.
PERFORM BDC_OPEN_GROUP.

ENDIF. " IF FG_SESSION_OPEN = ' '.
*-- Insert BDC Data..
PERFORM BDC_INSERT_DATA.

ENDIF. " IF R_SESS = C_X.

ENDIF. " IF SY-SUBRC <> 0.

ENDIF. " IF SY-SUBRC <> 0.


ENDDO.


* Closing the dataset

CLOSE DATASET V_FNAME.

*-- Close the session if opened
IF FG_SESSION_OPEN = C_X.
PERFORM BDC_CLOSE_GROUP.
CALL TRANSACTION 'SM35'.
ENDIF.

ENDFORM. " GENERATE_BDCDATA
*&---------------------------------------------------------------------*
*& Form BDC_OPEN_GROUP
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM BDC_OPEN_GROUP.


CALL FUNCTION 'BDC_OPEN_GROUP'
EXPORTING
CLIENT = SY-MANDT
* DEST = FILLER8
GROUP = C_SESS
* HOLDDATE = FILLER8
KEEP = C_X
USER = SY-UNAME
* RECORD = FILLER1
* IMPORTING
* QID =
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 ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

ENDFORM. " BDC_OPEN_GROUP
*&---------------------------------------------------------------------*
*& Form BDC_INSERT_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM BDC_INSERT_DATA.


CALL FUNCTION 'BDC_INSERT'
EXPORTING
TCODE = C_XD01
* POST_LOCAL = NOVBLOCAL
* PRINTING = NOPRINT
TABLES
DYNPROTAB = IT_BDCDATA
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 ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

ENDFORM. " BDC_INSERT_DATA
*&---------------------------------------------------------------------*
*& Form BDC_CLOSE_GROUP
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM BDC_CLOSE_GROUP.

CALL FUNCTION 'BDC_CLOSE_GROUP'
EXCEPTIONS
NOT_OPEN = 1
QUEUE_ERROR = 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.

ENDFORM. " BDC_CLOSE_GROUP
*&---------------------------------------------------------------------*
*& Form FORMAT_MESSAGE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FORMAT_MESSAGE.


CLEAR V_LINES.
DESCRIBE TABLE IT_MSG LINES V_LINES.

READ TABLE IT_MSG INDEX V_LINES.

CLEAR V_MSG.

CALL FUNCTION 'FORMAT_MESSAGE'
EXPORTING
ID = IT_MSG-MSGID
LANG = IT_MSG-MSGSPRA
NO = IT_MSG-MSGNR
V1 = IT_MSG-MSGV1
V2 = IT_MSG-MSGV2
V3 = IT_MSG-MSGV3
V4 = IT_MSG-MSGV4
IMPORTING
MSG = V_MSG
EXCEPTIONS
NOT_FOUND = 1
OTHERS = 2.

IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.



ENDFORM. " FORMAT_MESSAGE
*&---------------------------------------------------------------------*
*& Form DISPLAY_ERR_RECS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM DISPLAY_ERR_RECS.

LOOP AT IT_ERR_MSG.

WRITE: / IT_ERR_MSG-MESSAGE.

ENDLOOP.

ENDFORM. " DISPLAY_ERR_RECS
*&---------------------------------------------------------------------*
*& Form GET_BANKDATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM GET_BANKDATA.

IT_BANK-BANKS = 'AD'.
IT_BANK-BANKL = '1000'.
IT_BANK-BANKN = 'S.B A/C'.
APPEND IT_BANK.

IT_BANK-BANKS = 'AD'.
IT_BANK-BANKL = 'CITY'.
IT_BANK-BANKN = 'CURR. A/C'.
APPEND IT_BANK.

IT_BANK-BANKS = 'AD'.
IT_BANK-BANKL = 'H001'.
IT_BANK-BANKN = 'S.B A/C'.
APPEND IT_BANK.