You are on page 1of 9

How to use bdc on table control.

1) Suppose we need to enter some records in a ztable through table maintenance generater SM30. For this first you have to create a program from SHDB after recording .record how a record is entered in the table zvbak from the transaction SM30 and create a program. Now that source code is required in this program. Let the table is zvbak and it has four fields, so first create an internal table to get the records from the flat file. And start coding like this. DATA: BDCDATA LIKE BDCDATA OCCURS 0 WITH HEADER LINE. DATA: BEGIN OF ITAB OCCURS 0, VBELN TYPE VBELN, VKORG TYPE VKORG, VTWEG TYPE VTWEG, SPART(2), END OF ITAB. daTA: MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE. 2) This count variable is to count the number of lines displayed at one time in the table control. DATA: V_COUNT1 TYPE I. DATA: V_COUNT(3) TYPE C. DATA: V_CHAR(16). 3) Now take the file from where records are to be uploaded. PARAMETERS: P_FILE TYPE LOCALFILE. start-of-selection. DATA: W_FILE TYPE STRING. W_FILE = P_FILE. 4) Call function gui_upload to upload the records from flatfile to an internal table itab. CALL FUNCTION 'GUI_UPLOAD' EXPORTING

filename FILETYPE

= W_FILE = 'DAT' ='' =0 = 'X' ='' ='' = ABAP_TRUE = '#' ='' = =''

* HAS_FIELD_SEPARATOR * HEADER_LENGTH * READ_BY_LINE * DAT_MODE * CODEPAGE * IGNORE_CERR * REPLACEMENT * CHECK_BOM * VIRUS_SCAN_PROFILE * NO_AUTH_CHECK * IMPORTING * FILELENGTH * HEADER tables data_tab * EXCEPTIONS * FILE_OPEN_ERROR * FILE_READ_ERROR * NO_BATCH =3 =1 =2 = ITAB = =

* GUI_REFUSE_FILETRANSFER * INVALID_TYPE * NO_AUTHORITY * UNKNOWN_ERROR =5 =6 =7

=4

* BAD_DATA_FORMAT * HEADER_NOT_ALLOWED

=8 =9 = 10

* SEPARATOR_NOT_ALLOWED * HEADER_TOO_LONG * UNKNOWN_DP_ERROR * ACCESS_DENIED * DP_OUT_OF_MEMORY * DISK_FULL * DP_TIMEOUT * OTHERS . IF sy-subrc <> 0. = 15 = 16 = 17 = 13 = 14 = 11

= 12

* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF. 5) Now call function BDC_open_group to open the session. CALL FUNCTION 'BDC_OPEN_GROUP' EXPORTING CLIENT * DEST GROUP * HOLDDATE KEEP USER * RECORD = 'X' = SY-UNAME = FILLER = SY-MANDT = FILLER8 = 'ZBDC' = FILLER8

PROG * DCPFM ** DATFM * IMPORTING * QID EXCEPTIONS

= SY-CPROG = '%' = '%,'

CLIENT_INVALID

=1 =2

DESTINATION_INVALID GROUP_INVALID GROUP_IS_LOCKED HOLDDATE_INVALID INTERNAL_ERROR QUEUE_ERROR RUNNING =8 =3

=4 =5 =6 =7

SYSTEM_LOCK_ERROR USER_INVALID OTHERS . IF sy-subrc <> 0. = 11 = 10

=9

* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF. 6) Here we will use the source code obtained from the recording. perform bdc_dynpro perform bdc_field using 'SAPMSVMA' '0100'. using 'BDC_CURSOR'

'VIEWNAME'. perform bdc_field using 'BDC_OKCODE' '=SHOW'. perform bdc_field using 'VIEWNAME' 'ZVBAK'. perform bdc_field 'X'. perform bdc_dynpro perform bdc_field using 'SAPLZVBAK' '0001'. using 'BDC_CURSOR' 'ZVBAK-VBELN(01)'. perform bdc_field using 'BDC_OKCODE' '=AEND'. perform bdc_dynpro perform bdc_field using 'SAPLZVBAK' '0001'. using 'BDC_CURSOR' 'ZVBAK-VBELN(01)'. perform bdc_field using 'BDC_OKCODE' '=NEWL'. 7) Now when bdcdata table is being filled with the records then keep on incrementing the count variable by 1 as it is representing the line number of the table control . concatenate the field name with the count to represent the row and column of the table control. LOOP AT ITAB. V_COUNT1 = V_COUNT1 + 1. V_COUNT = V_COUNT1. CONDENSE V_COUNT. perform bdc_dynpro *perform bdc_field using 'SAPLZVBAK' '0001'. using 'BDC_CURSOR' using 'VIMDYNFLDS-LTD_DTA_NO'

'ZVBAK-SPART(01)'.

CLEAR V_CHAR. CONCATENATE 'ZVBAK-VBELN(' V_COUNT ')' INTO V_CHAR. perform bdc_field using V_CHAR ITAB-VBELN. CLEAR V_CHAR. CONCATENATE 'ZVBAK-VKORG(' V_COUNT ')' INTO V_CHAR. perform bdc_field using V_CHAR ITAB-VKORG. CLEAR V_CHAR. CONCATENATE 'ZVBAK-VTWEG(' V_COUNT ')' INTO V_CHAR. perform bdc_field using V_CHAR ITAB-VTWEG. 8) When the last line of the table control is reached then again reset the value of the count as one and set the command of page up i.e. '=P+' inside the loop only.this is done only for that screen where table control is being used. IF V_COUNT1 = 24. V_COUNT1 = 1. perform bdc_field using 'BDC_OKCODE' '=P+'. ENDIF. * CLEAR V_CHAR. *CONCATENATE 'ZVBAK-SPART(' V_COUNT ')' INTO V_CHAR. *perform bdc_field * ENDLOOP. using V_CHAR ITAB-SPART.

9) After the loop statement continue the steps as per recording. perform bdc_dynpro perform bdc_field using 'SAPLZVBAK' '0001'. using 'BDC_CURSOR' 'ZVBAK-VBELN(02)'. perform bdc_field using 'BDC_OKCODE' '=SAVE'. perform bdc_dynpro perform bdc_field using 'SAPLZVBAK' '0001'. using 'BDC_CURSOR' 'ZVBAK-VBELN(03)'. perform bdc_field using 'BDC_OKCODE' '=BACK'. perform bdc_dynpro perform bdc_field using 'SAPLZVBAK' '0001'. using 'BDC_CURSOR' 'ZVBAK-VBELN(03)'. perform bdc_field using 'BDC_OKCODE' '=BACK'. perform bdc_dynpro perform bdc_field using 'SAPMSVMA' '0100'. using 'BDC_OKCODE' '/EBACK'. perform bdc_field using 'BDC_CURSOR' 'VIEWNAME'. 10) Now call the function bdc_insert and pass the bdcdata table to this function. CALL FUNCTION 'BDC_INSERT' EXPORTING TCODE = 'SM30'

* POST_LOCAL * PRINTING * SIMUBATCH * CTUPARAMS TABLES dynprotab * EXCEPTIONS * INTERNAL_ERROR * NOT_OPEN * QUEUE_ERROR * TCODE_INVALID * PRINTING_INVALID * POSTING_INVALID * OTHERS . IF sy-subrc <> 0. =7

= NOVBLOCAL = NOPRINT ='' =''

= BDCDATA

=1 =2 =3 =4 =5 =6

* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF. 11) Now close the group. CALL FUNCTION 'BDC_CLOSE_GROUP' * EXCEPTIONS * NOT_OPEN * QUEUE_ERROR * OTHERS =3 =1 =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. 12) Define the subroutines used in the recording here. 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. CLEAR BDCDATA. BDCDATA-FNAM = FNAM. BDCDATA-FVAL = FVAL. APPEND BDCDATA. ENDFORM.

You might also like