‎2007 Apr 24 1:43 PM
i need STEP BY STEP coding for BDCs for transferring flat file to SAP system.Can anybody help me?
‎2007 Apr 24 1:45 PM
Refer
http://www.sap-img.com/abap/learning-bdc-programming.htm
refer to this sample program
REPORT ztest_report
NO STANDARD PAGE HEADING
LINE-SIZE 255
MESSAGE-ID ZRASH.
************************************************************************
* Internal Table Declarations *
************************************************************************
*--Internal Table for Data Uploading.
DATA : BEGIN OF IT_FFCUST OCCURS 0,
KUNNR(10),
BUKRS(4),
KTOKD(4),
ANRED(15),
NAME1(35),
SORTL(10),
STRAS(35),
ORT01(35),
PSTLZ(10),
LAND1(3),
SPRAS(2),
AKONT(10),
END OF IT_FFCUST.
*--Internal Table to Store Error Records.
DATA : BEGIN OF IT_ERRCUST OCCURS 0,
KUNNR(10),
EMSG(255),
END OF IT_ERRCUST.
*--Internal Table to Store Successful Records.
DATA : BEGIN OF IT_SUCCUST OCCURS 0,
KUNNR(10),
SMSG(255),
END OF IT_SUCCUST.
*--Internal Table for Storing the BDC data.
DATA : IT_CUSTBDC LIKE BDCDATA OCCURS 0 WITH HEADER LINE.
*--Internal Table for storing the messages.
DATA : IT_CUSTMSG LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.
DATA : V_FLAG1(1) VALUE ' ',
"Flag used for opening session.
V_TLINES LIKE SY-TABIX,
"For storing total records processed.
V_ELINES LIKE SY-TABIX,
"For storing the no of error records.
V_SLINES LIKE SY-TABIX.
"For storing the no of success records.
************************************************************************
* Selection screen *
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK B1.
PARAMETERS : V_FNAME LIKE RLGRAP-FILENAME,
V_SESNAM LIKE RLGRAP-FILENAME.
SELECTION-SCREEN END OF BLOCK B1.
************************************************************************
* Start-of-selection *
************************************************************************
START-OF-SELECTION.
*-- Form to upload flatfile data into the internal table.
PERFORM FORM_UPLOADFF.
************************************************************************
* TOP-OF-PAGE *
************************************************************************
TOP-OF-PAGE.
WRITE:/ 'Details of the error and success records for the transaction'
.
ULINE.
SKIP.
************************************************************************
* End of Selection *
************************************************************************
END-OF-SELECTION.
*-- Form to Generate a BDC from the Uploaded Internal table
PERFORM FORM_BDCGENERATE.
*--To write the totals and the session name.
PERFORM FORM_WRITEOP.
*&---------------------------------------------------------------------*
*& Form form_uploadff
*&---------------------------------------------------------------------*
* Form to upload flatfile data into the internal table.
*----------------------------------------------------------------------*
FORM FORM_UPLOADFF .
*--Variable to change the type of the parameter file name.
DATA : LV_FILE TYPE STRING.
LV_FILE = V_FNAME.
*--Function to upload the flat file to the internal table.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = LV_FILE
* FILETYPE = 'ASC'
HAS_FIELD_SEPARATOR = 'X'
* HEADER_LENGTH = 0
* READ_BY_LINE = 'X'
* DAT_MODE = ' '
* IMPORTING
* FILELENGTH =
* HEADER =
TABLES
DATA_TAB = IT_FFCUST
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.
*--Deleting the headings from the internal table.
DELETE IT_FFCUST INDEX 1.
*--Getting the total number of records uploaded.
DESCRIBE TABLE IT_FFCUST LINES V_TLINES.
ENDIF.
ENDFORM. " form_uploadff
*&---------------------------------------------------------------------*
*& Form Form_bdcgenerate
*&---------------------------------------------------------------------*
* Form to Generate a BDC from the Uploaded Internal table
*----------------------------------------------------------------------*
FORM FORM_BDCGENERATE .
*--Generating the BDC table for the fields of the internal table.
LOOP AT IT_FFCUST.
PERFORM POPULATEBDC USING :
'X' 'SAPMF02D' '0105',
' ' 'BDC_OKCODE' '/00' ,
' ' 'RF02D-KUNNR' IT_FFCUST-KUNNR,
' ' 'RF02D-BUKRS' IT_FFCUST-BUKRS,
' ' 'RF02D-KTOKD' IT_FFCUST-KTOKD,
'X' 'SAPMF02D' '0110' ,
' ' 'BDC_OKCODE' '/00',
' ' 'KNA1-ANRED' IT_FFCUST-ANRED,
' ' 'KNA1-NAME1' IT_FFCUST-NAME1,
' ' 'KNA1-SORTL' IT_FFCUST-SORTL,
' ' 'KNA1-STRAS' IT_FFCUST-STRAS,
' ' 'KNA1-ORT01' IT_FFCUST-ORT01,
' ' 'KNA1-PSTLZ' IT_FFCUST-PSTLZ,
' ' 'KNA1-LAND1' IT_FFCUST-LAND1,
' ' 'KNA1-SPRAS' IT_FFCUST-SPRAS,
'X' 'SAPMFO2D' '0120',
' ' 'BDC_OKCODE' '/00',
'X' 'SAPMF02D' '0125',
' ' 'BDC_OKCODE' '/00',
'X' 'SAPMF02D' '0130',
' ' 'BDC_OKCODE' '=ENTR',
'X' 'SAPMF02D' '0340',
' ' 'BDC_OKCODE' '=ENTR',
'X' 'SAPMF02D' '0360',
' ' 'BDC_OKCODE' '=ENTR',
'X' 'SAPMF02D' '0210',
' ' 'KNB1-AKONT' IT_FFCUST-AKONT,
' ' 'BDC_OKCODE' '/00',
'X' 'SAPMF02D' '0215',
' ' 'BDC_OKCODE' '/00',
'X' 'SAPMF02D' '0220',
' ' 'BDC_OKCODE' '/00',
'X' 'SAPMF02D' '0230',
' ' 'BDC_OKCODE' '=UPDA'.
*--Calling the transaction 'fd01'.
CALL TRANSACTION 'FD01' USING IT_CUSTBDC MODE 'N' UPDATE 'S'
MESSAGES INTO IT_CUSTMSG.
IF SY-SUBRC <> 0.
*--Populating the error records internal table.
IT_ERRCUST-KUNNR = IT_FFCUST-KUNNR.
APPEND IT_ERRCUST.
CLEAR IT_ERRCUST.
*--Opening a session if there is an error record.
IF V_FLAG1 = ' '.
PERFORM FORM_OPENSESSION.
V_FLAG1 = 'X'.
ENDIF.
*--Inserting the error records into already open session.
IF V_FLAG1 = 'X'.
PERFORM FORM_INSERT.
ENDIF.
*--Populating the Success records internal table.
ELSE.
IT_SUCCUST-KUNNR = IT_FFCUST-KUNNR.
APPEND IT_SUCCUST.
CLEAR IT_SUCCUST.
ENDIF.
*--Displaying the messages.
IF NOT IT_CUSTMSG[] IS INITIAL.
PERFORM FORM_FORMATMSG.
ENDIF.
*--Clearing the message and bdc tables.
CLEAR : IT_CUSTBDC[],IT_CUSTMSG[].
ENDLOOP.
*--Getting the total no of error records.
DESCRIBE TABLE IT_ERRCUST LINES V_ELINES.
*--Getting the total no of successful records.
DESCRIBE TABLE IT_SUCCUST LINES V_SLINES.
*--Closing the session only if it is open.
IF V_FLAG1 = 'X'.
PERFORM FORM_CLOSESESS.
ENDIF.
ENDFORM. " Form_bdcgenerate
*&---------------------------------------------------------------------*
*& Form populatebdc
*&---------------------------------------------------------------------*
* FOrm to Populate the BDC table.
*----------------------------------------------------------------------*
FORM POPULATEBDC USING VALUE(P_0178)
VALUE(P_0179)
VALUE(P_0180).
IF P_0178 = 'X'.
IT_CUSTBDC-PROGRAM = P_0179.
IT_CUSTBDC-DYNPRO = P_0180.
IT_CUSTBDC-DYNBEGIN = 'X'.
ELSE.
IT_CUSTBDC-FNAM = P_0179.
IT_CUSTBDC-FVAL = P_0180.
ENDIF.
APPEND IT_CUSTBDC.
CLEAR IT_CUSTBDC.
ENDFORM. " populatebdc
*&---------------------------------------------------------------------*
*& Form FORM_OPENSESSION
*&---------------------------------------------------------------------*
* Form to Open a session.
*----------------------------------------------------------------------*
FORM FORM_OPENSESSION .
*--Variable to convert the given session name into reqd type.
DATA : LV_SESNAM(12).
LV_SESNAM = V_SESNAM.
*--Opening a session.
CALL FUNCTION 'BDC_OPEN_GROUP'
EXPORTING
CLIENT = SY-MANDT
GROUP = LV_SESNAM
HOLDDATE = '20040805'
KEEP = 'X'
USER = SY-UNAME
PROG = SY-CPROG
* 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.
WRITE :/ 'Session not open'.
ENDIF.
ENDFORM. " FORM_OPENSESSION
*&---------------------------------------------------------------------*
*& Form FORM_INSERT
*&---------------------------------------------------------------------*
* fORM TO INSERT ERROR RECOED INTO A SESSION.
*----------------------------------------------------------------------*
FORM FORM_INSERT .
*--Inserting the record into session.
CALL FUNCTION 'BDC_INSERT'
EXPORTING
TCODE = 'FD01'
* POST_LOCAL = NOVBLOCAL
* PRINTING = NOPRINT
* SIMUBATCH = ' '
* CTUPARAMS = ' '
TABLES
DYNPROTAB = IT_CUSTBDC
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.
WRITE :/ 'Unable to insert the record'.
ENDIF.
ENDFORM. " FORM_INSERT
*&---------------------------------------------------------------------*
*& Form FORM_CLOSESESS
*&---------------------------------------------------------------------*
* Form to Close the Open Session.
*----------------------------------------------------------------------*
FORM FORM_CLOSESESS .
CALL FUNCTION 'BDC_CLOSE_GROUP'
EXCEPTIONS
NOT_OPEN = 1
QUEUE_ERROR = 2
OTHERS = 3.
IF SY-SUBRC <> 0.
ENDIF.
ENDFORM. " FORM_CLOSESESS
*&---------------------------------------------------------------------*
*& Form FORM_FORMATMSG
*&---------------------------------------------------------------------*
* Form to format messages.
*----------------------------------------------------------------------*
FORM FORM_FORMATMSG .
*--Var to store the formatted msg.
DATA : LV_MSG(255).
CALL FUNCTION 'FORMAT_MESSAGE'
EXPORTING
ID = SY-MSGID
LANG = SY-LANGU
NO = SY-MSGNO
V1 = SY-MSGV1
V2 = SY-MSGV2
V3 = SY-MSGV3
V4 = SY-MSGV4
IMPORTING
MSG = LV_MSG
EXCEPTIONS
NOT_FOUND = 1
OTHERS = 2.
IF SY-SUBRC = 0.
WRITE :/ LV_MSG.
ENDIF.
ULINE.
ENDFORM. " FORM_FORMATMSG
*&---------------------------------------------------------------------*
*& Form form_writeop
*&---------------------------------------------------------------------*
* To write the totals and the session name.
*----------------------------------------------------------------------*
FORM FORM_WRITEOP .
WRITE :/ 'Total Records Uploaded :',V_TLINES,
/ 'No of Error Records :',V_ELINES,
/ 'No of Success Records :',V_SLINES,
/ 'Name of the Session :',V_SESNAM.
ULINE.
ENDFORM. " form_writeop
Message was edited by:
Santosh Kumar Patha
‎2007 Apr 24 1:45 PM
Refer
http://www.sap-img.com/abap/learning-bdc-programming.htm
refer to this sample program
REPORT ztest_report
NO STANDARD PAGE HEADING
LINE-SIZE 255
MESSAGE-ID ZRASH.
************************************************************************
* Internal Table Declarations *
************************************************************************
*--Internal Table for Data Uploading.
DATA : BEGIN OF IT_FFCUST OCCURS 0,
KUNNR(10),
BUKRS(4),
KTOKD(4),
ANRED(15),
NAME1(35),
SORTL(10),
STRAS(35),
ORT01(35),
PSTLZ(10),
LAND1(3),
SPRAS(2),
AKONT(10),
END OF IT_FFCUST.
*--Internal Table to Store Error Records.
DATA : BEGIN OF IT_ERRCUST OCCURS 0,
KUNNR(10),
EMSG(255),
END OF IT_ERRCUST.
*--Internal Table to Store Successful Records.
DATA : BEGIN OF IT_SUCCUST OCCURS 0,
KUNNR(10),
SMSG(255),
END OF IT_SUCCUST.
*--Internal Table for Storing the BDC data.
DATA : IT_CUSTBDC LIKE BDCDATA OCCURS 0 WITH HEADER LINE.
*--Internal Table for storing the messages.
DATA : IT_CUSTMSG LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.
DATA : V_FLAG1(1) VALUE ' ',
"Flag used for opening session.
V_TLINES LIKE SY-TABIX,
"For storing total records processed.
V_ELINES LIKE SY-TABIX,
"For storing the no of error records.
V_SLINES LIKE SY-TABIX.
"For storing the no of success records.
************************************************************************
* Selection screen *
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK B1.
PARAMETERS : V_FNAME LIKE RLGRAP-FILENAME,
V_SESNAM LIKE RLGRAP-FILENAME.
SELECTION-SCREEN END OF BLOCK B1.
************************************************************************
* Start-of-selection *
************************************************************************
START-OF-SELECTION.
*-- Form to upload flatfile data into the internal table.
PERFORM FORM_UPLOADFF.
************************************************************************
* TOP-OF-PAGE *
************************************************************************
TOP-OF-PAGE.
WRITE:/ 'Details of the error and success records for the transaction'
.
ULINE.
SKIP.
************************************************************************
* End of Selection *
************************************************************************
END-OF-SELECTION.
*-- Form to Generate a BDC from the Uploaded Internal table
PERFORM FORM_BDCGENERATE.
*--To write the totals and the session name.
PERFORM FORM_WRITEOP.
*&---------------------------------------------------------------------*
*& Form form_uploadff
*&---------------------------------------------------------------------*
* Form to upload flatfile data into the internal table.
*----------------------------------------------------------------------*
FORM FORM_UPLOADFF .
*--Variable to change the type of the parameter file name.
DATA : LV_FILE TYPE STRING.
LV_FILE = V_FNAME.
*--Function to upload the flat file to the internal table.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = LV_FILE
* FILETYPE = 'ASC'
HAS_FIELD_SEPARATOR = 'X'
* HEADER_LENGTH = 0
* READ_BY_LINE = 'X'
* DAT_MODE = ' '
* IMPORTING
* FILELENGTH =
* HEADER =
TABLES
DATA_TAB = IT_FFCUST
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.
*--Deleting the headings from the internal table.
DELETE IT_FFCUST INDEX 1.
*--Getting the total number of records uploaded.
DESCRIBE TABLE IT_FFCUST LINES V_TLINES.
ENDIF.
ENDFORM. " form_uploadff
*&---------------------------------------------------------------------*
*& Form Form_bdcgenerate
*&---------------------------------------------------------------------*
* Form to Generate a BDC from the Uploaded Internal table
*----------------------------------------------------------------------*
FORM FORM_BDCGENERATE .
*--Generating the BDC table for the fields of the internal table.
LOOP AT IT_FFCUST.
PERFORM POPULATEBDC USING :
'X' 'SAPMF02D' '0105',
' ' 'BDC_OKCODE' '/00' ,
' ' 'RF02D-KUNNR' IT_FFCUST-KUNNR,
' ' 'RF02D-BUKRS' IT_FFCUST-BUKRS,
' ' 'RF02D-KTOKD' IT_FFCUST-KTOKD,
'X' 'SAPMF02D' '0110' ,
' ' 'BDC_OKCODE' '/00',
' ' 'KNA1-ANRED' IT_FFCUST-ANRED,
' ' 'KNA1-NAME1' IT_FFCUST-NAME1,
' ' 'KNA1-SORTL' IT_FFCUST-SORTL,
' ' 'KNA1-STRAS' IT_FFCUST-STRAS,
' ' 'KNA1-ORT01' IT_FFCUST-ORT01,
' ' 'KNA1-PSTLZ' IT_FFCUST-PSTLZ,
' ' 'KNA1-LAND1' IT_FFCUST-LAND1,
' ' 'KNA1-SPRAS' IT_FFCUST-SPRAS,
'X' 'SAPMFO2D' '0120',
' ' 'BDC_OKCODE' '/00',
'X' 'SAPMF02D' '0125',
' ' 'BDC_OKCODE' '/00',
'X' 'SAPMF02D' '0130',
' ' 'BDC_OKCODE' '=ENTR',
'X' 'SAPMF02D' '0340',
' ' 'BDC_OKCODE' '=ENTR',
'X' 'SAPMF02D' '0360',
' ' 'BDC_OKCODE' '=ENTR',
'X' 'SAPMF02D' '0210',
' ' 'KNB1-AKONT' IT_FFCUST-AKONT,
' ' 'BDC_OKCODE' '/00',
'X' 'SAPMF02D' '0215',
' ' 'BDC_OKCODE' '/00',
'X' 'SAPMF02D' '0220',
' ' 'BDC_OKCODE' '/00',
'X' 'SAPMF02D' '0230',
' ' 'BDC_OKCODE' '=UPDA'.
*--Calling the transaction 'fd01'.
CALL TRANSACTION 'FD01' USING IT_CUSTBDC MODE 'N' UPDATE 'S'
MESSAGES INTO IT_CUSTMSG.
IF SY-SUBRC <> 0.
*--Populating the error records internal table.
IT_ERRCUST-KUNNR = IT_FFCUST-KUNNR.
APPEND IT_ERRCUST.
CLEAR IT_ERRCUST.
*--Opening a session if there is an error record.
IF V_FLAG1 = ' '.
PERFORM FORM_OPENSESSION.
V_FLAG1 = 'X'.
ENDIF.
*--Inserting the error records into already open session.
IF V_FLAG1 = 'X'.
PERFORM FORM_INSERT.
ENDIF.
*--Populating the Success records internal table.
ELSE.
IT_SUCCUST-KUNNR = IT_FFCUST-KUNNR.
APPEND IT_SUCCUST.
CLEAR IT_SUCCUST.
ENDIF.
*--Displaying the messages.
IF NOT IT_CUSTMSG[] IS INITIAL.
PERFORM FORM_FORMATMSG.
ENDIF.
*--Clearing the message and bdc tables.
CLEAR : IT_CUSTBDC[],IT_CUSTMSG[].
ENDLOOP.
*--Getting the total no of error records.
DESCRIBE TABLE IT_ERRCUST LINES V_ELINES.
*--Getting the total no of successful records.
DESCRIBE TABLE IT_SUCCUST LINES V_SLINES.
*--Closing the session only if it is open.
IF V_FLAG1 = 'X'.
PERFORM FORM_CLOSESESS.
ENDIF.
ENDFORM. " Form_bdcgenerate
*&---------------------------------------------------------------------*
*& Form populatebdc
*&---------------------------------------------------------------------*
* FOrm to Populate the BDC table.
*----------------------------------------------------------------------*
FORM POPULATEBDC USING VALUE(P_0178)
VALUE(P_0179)
VALUE(P_0180).
IF P_0178 = 'X'.
IT_CUSTBDC-PROGRAM = P_0179.
IT_CUSTBDC-DYNPRO = P_0180.
IT_CUSTBDC-DYNBEGIN = 'X'.
ELSE.
IT_CUSTBDC-FNAM = P_0179.
IT_CUSTBDC-FVAL = P_0180.
ENDIF.
APPEND IT_CUSTBDC.
CLEAR IT_CUSTBDC.
ENDFORM. " populatebdc
*&---------------------------------------------------------------------*
*& Form FORM_OPENSESSION
*&---------------------------------------------------------------------*
* Form to Open a session.
*----------------------------------------------------------------------*
FORM FORM_OPENSESSION .
*--Variable to convert the given session name into reqd type.
DATA : LV_SESNAM(12).
LV_SESNAM = V_SESNAM.
*--Opening a session.
CALL FUNCTION 'BDC_OPEN_GROUP'
EXPORTING
CLIENT = SY-MANDT
GROUP = LV_SESNAM
HOLDDATE = '20040805'
KEEP = 'X'
USER = SY-UNAME
PROG = SY-CPROG
* 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.
WRITE :/ 'Session not open'.
ENDIF.
ENDFORM. " FORM_OPENSESSION
*&---------------------------------------------------------------------*
*& Form FORM_INSERT
*&---------------------------------------------------------------------*
* fORM TO INSERT ERROR RECOED INTO A SESSION.
*----------------------------------------------------------------------*
FORM FORM_INSERT .
*--Inserting the record into session.
CALL FUNCTION 'BDC_INSERT'
EXPORTING
TCODE = 'FD01'
* POST_LOCAL = NOVBLOCAL
* PRINTING = NOPRINT
* SIMUBATCH = ' '
* CTUPARAMS = ' '
TABLES
DYNPROTAB = IT_CUSTBDC
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.
WRITE :/ 'Unable to insert the record'.
ENDIF.
ENDFORM. " FORM_INSERT
*&---------------------------------------------------------------------*
*& Form FORM_CLOSESESS
*&---------------------------------------------------------------------*
* Form to Close the Open Session.
*----------------------------------------------------------------------*
FORM FORM_CLOSESESS .
CALL FUNCTION 'BDC_CLOSE_GROUP'
EXCEPTIONS
NOT_OPEN = 1
QUEUE_ERROR = 2
OTHERS = 3.
IF SY-SUBRC <> 0.
ENDIF.
ENDFORM. " FORM_CLOSESESS
*&---------------------------------------------------------------------*
*& Form FORM_FORMATMSG
*&---------------------------------------------------------------------*
* Form to format messages.
*----------------------------------------------------------------------*
FORM FORM_FORMATMSG .
*--Var to store the formatted msg.
DATA : LV_MSG(255).
CALL FUNCTION 'FORMAT_MESSAGE'
EXPORTING
ID = SY-MSGID
LANG = SY-LANGU
NO = SY-MSGNO
V1 = SY-MSGV1
V2 = SY-MSGV2
V3 = SY-MSGV3
V4 = SY-MSGV4
IMPORTING
MSG = LV_MSG
EXCEPTIONS
NOT_FOUND = 1
OTHERS = 2.
IF SY-SUBRC = 0.
WRITE :/ LV_MSG.
ENDIF.
ULINE.
ENDFORM. " FORM_FORMATMSG
*&---------------------------------------------------------------------*
*& Form form_writeop
*&---------------------------------------------------------------------*
* To write the totals and the session name.
*----------------------------------------------------------------------*
FORM FORM_WRITEOP .
WRITE :/ 'Total Records Uploaded :',V_TLINES,
/ 'No of Error Records :',V_ELINES,
/ 'No of Success Records :',V_SLINES,
/ 'Name of the Session :',V_SESNAM.
ULINE.
ENDFORM. " form_writeop
Message was edited by:
Santosh Kumar Patha
‎2007 Apr 24 1:46 PM
sample code for material creation in BDC.
data: bdcdata like bdcdata occurs 0 with header line.
*data: t_file type standard table of sdokpath.
*data: t_direct type standard table of sdokpath.
parameters:
p_file(200) type c.
data:
w_integer type i,
w_file type string.
data: begin of fs_table,
matnr like mara-matnr,
mbrsh like mara-mbrsh,
mtart like MARA-MTART,
des(30),
meins(3), " like mara-meins,
end of fs_table.
data: t_table like
standard table
of fs_table.
at selection-screen on value-request for p_file.
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
DEF_FILENAME = ' '
DEF_PATH = ' '
MASK = ' '
MODE = ' '
TITLE = ' '
IMPORTING
FILENAME = p_file
RC =
EXCEPTIONS
INV_WINSYS = 1
NO_BATCH = 2
SELECTION_CANCEL = 3
SELECTION_ERROR = 4
OTHERS = 5
.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
at selection-screen.
CALL FUNCTION 'WS_QUERY'
EXPORTING
ENVIRONMENT =
FILENAME = p_file
QUERY = 'FE'
WINID =
IMPORTING
RETURN = w_integer
EXCEPTIONS
INV_QUERY = 1
NO_BATCH = 2
FRONTEND_ERROR = 3
OTHERS = 4
.
if w_integer EQ 0.
message ' Enter Correct Directory' Type 'E'.
ENDIF.
*IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
*ENDIF.
start-of-selection.
w_file = p_file.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = w_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 =
IMPORTING
FILELENGTH =
HEADER =
TABLES
DATA_TAB = t_table
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 ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
loop at t_table into fs_table.
refresh bdcdata.
write:
/ fs_table-matnr,
fs_table-mbrsh,
FS_TABLE-MTaRT,
fs_table-des,
fs_table-meins.
*
*
*endloop.
*
**include bdcrecx1.
*
*start-of-selection.
*
*perform open_group.
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' fs_table-matnr.
perform bdc_field using 'RMMG1-MBRSH' fs_table-mbrsh.
perform bdc_field using 'RMMG1-MTART' fs_table-mtart.
perform bdc_dynpro using 'SAPLMGMM' '0070'.
perform bdc_field using 'BDC_CURSOR' 'MSICHTAUSW-DYTXT(02)'.
perform bdc_field using 'BDC_OKCODE' '=ENTR'.
perform bdc_field using 'MSICHTAUSW-KZSEL(01)' 'X'.
perform bdc_field using 'MSICHTAUSW-KZSEL(02)' 'X'.
perform bdc_dynpro using 'SAPLMGMM' '4004'.
perform bdc_field using 'BDC_OKCODE' '=SP02'.
perform bdc_field using 'MAKT-MAKTX' fs_table-des.
perform bdc_field using 'BDC_CURSOR' 'MARA-MEINS'.
perform bdc_field using 'MARA-MEINS' fs_table-meins.
perform bdc_field using 'MARA-MTPOS_MARA' 'NORM'.
perform bdc_dynpro using 'SAPLMGMM' '4004'.
perform bdc_field using 'BDC_OKCODE' '=BU'.
*perform bdc_field using 'BDC_CURSOR' 'MAKT-MAKTX'.
*perform bdc_field using 'MAKT-MAKTX' fs_table-des.
*perform bdc_dynpro using 'SAPLMGMM' '4004'.
*perform bdc_field using 'BDC_OKCODE' '=BACK'.
*perform bdc_transaction using 'MM01'.
call transaction 'MM01' using bdcdata mode 'a'.
*perform close_group.
*
*clear bdcdata.
endloop.
FORM BDC_DYNPRO USING PROGRAM DYNPRO.
CLEAR BDCDATA.
BDCDATA-PROGRAM = PROGRAM.
BDCDATA-DYNPRO = DYNPRO.
BDCDATA-DYNBEGIN = 'X'.
APPEND BDCDATA.
ENDFORM.
*----
*
Insert field
*
*----
*
FORM BDC_FIELD USING FNAM FVAL.
IF FVAL <> NODATA.
CLEAR BDCDATA.
BDCDATA-FNAM = FNAM.
BDCDATA-FVAL = FVAL.
APPEND BDCDATA.
ENDIF.
ENDFORM.
reward if it helps u
vijay pawar
‎2007 Apr 24 1:48 PM
u may try for simple code BDC
this is download after that try for upload from gui_upload funtion module.
<b>because flat file created for the download after that u may use that flat file to upload to SAP</b>
REPORT YH624_05050101.
TABLES: KNA1.
SELECT-OPTIONS:
S_KUNNR FOR KNA1-KUNNR.
DATA: BEGIN OF FS_KNA1,
KUNNR TYPE KNA1-KUNNR, " Customer Number 1
ADRNR TYPE KNA1-ADRNR, " Address
ANRED TYPE KNA1-ANRED, " Title
ERDAT TYPE KNA1-ERDAT, " Record Was Created Date
ERNAM TYPE KNA1-ERNAM, " who Created the Object
END OF FS_KNA1.
DATA: T_KNA1 LIKE STANDARD TABLE OF FS_KNA1.
AT SELECTION-SCREEN.
SELECT KUNNR
ADRNR
ANRED
ERDAT
ERNAM
FROM KNA1
INTO CORRESPONDING FIELDS OF TABLE T_KNA1
WHERE KUNNR IN S_KUNNR.
IF SY-SUBRC <> 0 .
MESSAGE 'Records Not Found' TYPE 'E'.
ENDIF.
END-OF-SELECTION.
CALL FUNCTION <b>'GUI_DOWNLOAD'</b>
EXPORTING
BIN_FILESIZE =
FILENAME = 'C:\Documents and Settings\vijay.pawar\Desktop\vkna1'
FILETYPE = 'ASC'
APPEND = ' '
WRITE_FIELD_SEPARATOR = ' '
HEADER = '00'
TRUNC_TRAILING_BLANKS = ' '
WRITE_LF = 'X'
COL_SELECT = ' '
COL_SELECT_MASK = ' '
DAT_MODE = ' '
CONFIRM_OVERWRITE = ' '
NO_AUTH_CHECK = ' '
CODEPAGE = ' '
IGNORE_CERR = ABAP_TRUE
REPLACEMENT = '#'
WRITE_BOM = ' '
TRUNC_TRAILING_BLANKS_EOL = 'X'
WK1_N_FORMAT = ' '
WK1_N_SIZE = ' '
WK1_T_FORMAT = ' '
WK1_T_SIZE = ' '
IMPORTING
FILELENGTH =
TABLES
DATA_TAB = T_KNA1
FIELDNAMES =
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 ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
reward if it helps u
vijay pawar
‎2007 Apr 24 1:48 PM
Hi
Check the below code.
Actually this is for uploading data from a location in the unix directory.
----
DATA DEFINITION *
----
data: xfile type string.
class cl_abap_char_utilities definition load.
----
CONSTANTS------------------------------------------------------------*
----
constants:
con_tab type c value cl_abap_char_utilities=>horizontal_tab.
----
TABLES - SAP TABLES--------------------------------------------------*
----
tables: t100, ieqinstall.
----
INTERNAL TABLES------------------------------------------------------*
----
data: bdcdata like bdcdata occurs 0 with header line.
data: messtab like bdcmsgcoll occurs 0 with header line.
data: begin of tbl_data occurs 0,
equnr like equi-equnr,
equn1 like equi-equnr,
equn2 like equi-equnr,
equn3 like equi-equnr,
equn4 like equi-equnr,
equn5 like equi-equnr,
equn6 like equi-equnr,
equn7 like equi-equnr,
equn8 like equi-equnr,
equn9 like equi-equnr,
equn10 like equi-equnr,
equn11 like equi-equnr,
end of tbl_data.
data: wa_tbl_data like tbl_data.
data: begin of msg_tab occurs 0,
equnr like equi-equnr,
msg(60) type c,
end of msg_tab.
----
initialization.
----
xfile = '/users/common/all/Serial/serial.txt'.
----
start-of-selection.
----
perform load_file.
perform create_bdc.
perform send_mail.
----
END OF SELECTION-----------------------------------------------------*
----
----
top-of-page.
----
perform write_report_header.
----
Form load_file
----
form load_file.
open dataset xfile for input in text mode encoding default.
do.
clear record.
read dataset xfile into record.
if sy-subrc <> 0.
exit.
else.
split record at con_tab into wa_tbl_data-equnr
wa_tbl_data-equn1
wa_tbl_data-equn2
wa_tbl_data-equn3
wa_tbl_data-equn4
wa_tbl_data-equn5
wa_tbl_data-equn6
wa_tbl_data-equn7
wa_tbl_data-equn8
wa_tbl_data-equn9
wa_tbl_data-equn10
wa_tbl_data-equn11.
if not wa_tbl_data-equnr is initial.
append wa_tbl_data to tbl_data.
endif.
endif.
enddo.
close dataset xfile.
endform. "load_file
----
Form create_bdc
----
form create_bdc.
loop at tbl_data.
perform bdc_dynpro using 'SAPMIEQ0' '0100'.
perform bdc_field using 'BDC_CURSOR'
'RM63E-EQUNR'.
perform bdc_field using 'BDC_OKCODE'
'/00'.
perform bdc_field using 'RM63E-EQUNR'
tbl_data-equnr.
perform bdc_dynpro using 'SAPMIEQ0' '0101'.
perform bdc_field using 'BDC_OKCODE'
'=UEQU'.
perform bdc_field using 'BDC_CURSOR'
'ITOB-SHTXT'.
perform bdc_dynpro using 'SAPLIEL2' '0101'.
perform bdc_field using 'BDC_OKCODE'
'/00'.
perform bdc_field using 'BDC_CURSOR'
'IEQINSTALL-EQUNR(11)'.
perform bdc_field using 'IEQINSTALL-EQUNR(01)'
tbl_data-equn1.
perform bdc_field using 'IEQINSTALL-EQUNR(02)'
tbl_data-equn2.
perform bdc_field using 'IEQINSTALL-EQUNR(03)'
tbl_data-equn3.
perform bdc_field using 'IEQINSTALL-EQUNR(04)'
tbl_data-equn4.
perform bdc_field using 'IEQINSTALL-EQUNR(05)'
tbl_data-equn5.
perform bdc_field using 'IEQINSTALL-EQUNR(06)'
tbl_data-equn6.
perform bdc_field using 'IEQINSTALL-EQUNR(07)'
tbl_data-equn7.
perform bdc_field using 'IEQINSTALL-EQUNR(08)'
tbl_data-equn8.
perform bdc_field using 'IEQINSTALL-EQUNR(09)'
tbl_data-equn9.
perform bdc_field using 'IEQINSTALL-EQUNR(10)'
tbl_data-equn10.
perform bdc_field using 'IEQINSTALL-EQUNR(11)'
tbl_data-equn11.
perform bdc_dynpro using 'SAPLIEL2' '0101'.
perform bdc_field using 'BDC_OKCODE'
'/ERW'.
perform bdc_field using 'BDC_CURSOR'
'IEQINSTALL-TPLNR'.
perform bdc_dynpro using 'SAPMIEQ0' '0101'.
perform bdc_field using 'BDC_OKCODE'
'=BU'.
perform bdc_field using 'BDC_CURSOR'
'ITOB-SHTXT'.
perform bdc_transaction using 'IE02'.
endloop.
endform. "create_bdc
----
Start new transaction according to parameters *
----
form bdc_transaction using tcode.
data: l_mstring(480).
data: l_subrc like sy-subrc.
refresh messtab.
call transaction tcode using bdcdata
mode 'N'
update 'S'
messages into messtab.
l_subrc = sy-subrc.
write: / 'CALL_TRANSACTION',
tcode,
'returncode:'(i05),
l_subrc,
'RECORD:',
sy-index.
move tbl_data-equnr to msg_tab-equnr.
loop at messtab.
select single * from t100 where sprsl = messtab-msgspra
and arbgb = messtab-msgid
and msgnr = messtab-msgnr.
if sy-subrc = 0.
l_mstring = t100-text.
if l_mstring cs '&1'.
replace '&1' with messtab-msgv1 into l_mstring.
replace '&2' with messtab-msgv2 into l_mstring.
replace '&3' with messtab-msgv3 into l_mstring.
replace '&4' with messtab-msgv4 into l_mstring.
else.
replace '&' with messtab-msgv1 into l_mstring.
replace '&' with messtab-msgv2 into l_mstring.
replace '&' with messtab-msgv3 into l_mstring.
replace '&' with messtab-msgv4 into l_mstring.
endif.
condense l_mstring.
if l_mstring eq 'Field IEQINSTALL-EQUNR (1) is not an input field'.
write: / 'Error: Sub Equipment Already Exists !!!'.
move ' Error: Sub Equipment Already Exists !!!' to msg_tab-msg.
exit.
else.
move ' Success: Sub Equipment Assigned' to msg_tab-msg.
endif.
write: / messtab-msgtyp, l_mstring(250).
else.
write: / messtab.
endif.
endloop.
append msg_tab.
skip.
refresh bdcdata.
endform. "bdc_transaction
----
Start new screen *
----
form bdc_dynpro using program dynpro.
clear bdcdata.
bdcdata-program = program.
bdcdata-dynpro = dynpro.
bdcdata-dynbegin = 'X'.
append bdcdata.
endform. "bdc_dynpro
----
Insert field *
----
form bdc_field using fnam fval.
clear bdcdata.
bdcdata-fnam = fnam.
bdcdata-fval = fval.
append bdcdata.
endform. "bdc_field
Hope this helps.
Britto
‎2007 Apr 24 1:56 PM