Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

step by step BDC using session method

Former Member
0 Likes
4,258

hello experts,

im loking for BDC by session method, but i couldnt find any tutorial on BDC- session method.

can anyone suggest any link ot tutorial wher i can find step by step tutorial on bdc by session method.

your suggestion will help me lot,

thank you,

regards,

nikhil

4 REPLIES 4
Read only

Former Member
0 Likes
2,850
Read only

Former Member
0 Likes
2,850

Hi Nikhil,

This is the basic program to learn BDC session .Run this program and change the file path.

Make a recording for MM01 In T-Code SHDB for creating material.

&----


*& Report ZBDC_SESSION

*&

&----


*&

*&

&----


REPORT ZBDC_SESSION no standard page heading line-size 255.

DATA : BEGIN OF BDC OCCURS 100 ,

MBRSH TYPE RMMG1-MBRSH,

MTART TYPE RMMG1-MTART,

MAKTX TYPE MAKT-MAKTX,

MEINS TYPE MARA-MEINS,

END OF BDC.

  • ITAB LIKE TABLE OF BDC WITH HEADER LINE,

DATA : BDCDATA LIKE TABLE OF BDCDATA WITH HEADER LINE.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

FILENAME ='D:\MM01.TXT'

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 = BDC

  • 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.

CALL FUNCTION 'BDC_OPEN_GROUP'

EXPORTING

CLIENT = SY-MANDT

  • DEST = FILLER8

GROUP = 'ZAK'

  • HOLDDATE = FILLER8

KEEP = 'X'

USER = SY-UNAME

  • RECORD = FILLER1

  • PROG = SY-CPROG

  • DCPFM = '%'

  • DATFM = '%'

  • 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.

*include bdcrecx1.

*

*start-of-selection.

*

*perform open_group.

LOOP AT BDC.

CLEAR BDCDATA[].

REFRESH BDCDATA[].

perform bdc_dynpro using 'SAPLMGMM' '0060'.

perform bdc_field using 'BDC_CURSOR'

'RMMG1-MTART'.

perform bdc_field using 'BDC_OKCODE'

'/00'.

*perform bdc_field using 'RMMG1-MATNR'

  • 'ZRAW001'.

perform bdc_field using 'RMMG1-MBRSH'

  • 'M'.

BDC-MBRSH.

perform bdc_field using 'RMMG1-MTART'

  • 'FERT'.

BDC-MTART.

perform bdc_dynpro using 'SAPLMGMM' '0070'.

perform bdc_field using 'BDC_CURSOR'

'MSICHTAUSW-DYTXT(01)'.

perform bdc_field using 'BDC_OKCODE'

'=ENTR'.

perform bdc_field using 'MSICHTAUSW-KZSEL(01)'

'X'.

perform bdc_dynpro using 'SAPLMGMM' '4004'.

perform bdc_field using 'BDC_OKCODE'

'/00'.

perform bdc_field using 'MAKT-MAKTX'

  • 'NUT'.

BDC-MAKTX.

perform bdc_field using 'BDC_CURSOR'

'MARA-MEINS'.

perform bdc_field using 'MARA-MEINS'

  • 'PC'.

BDC-MEINS.

perform bdc_field using 'MARA-MTPOS_MARA'

'NORM'.

perform bdc_dynpro using 'SAPLSPO1' '0300'.

perform bdc_field using 'BDC_OKCODE'

'=YES'.

*perform bdc_transaction using 'MM01'.

*

*perform close_group.

CALL FUNCTION 'BDC_INSERT'

EXPORTING

TCODE = 'MM01'

  • POST_LOCAL = NOVBLOCAL

  • PRINTING = NOPRINT

  • SIMUBATCH = ' '

  • CTUPARAMS = ' '

TABLES

DYNPROTAB = 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.

ENDLOOP.

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.

----


  • Start new screen *

----


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 IS NOT INITIAL.

CLEAR BDCDATA.

BDCDATA-FNAM = FNAM.

BDCDATA-FVAL = FVAL.

APPEND BDCDATA.

ENDIF.

ENDFORM.

Read only

Former Member
0 Likes
2,850

Batch Data Communication is one of the vital data upload / transaction automation option in many SAP components . I am suggesting here a way out to simplify the programming for a BDC application . It utilizes a template program and gives steps to use it to create your own BDC program on the fly.

This step by step approach can be used to create a Simple BDC program i.e for a transaction which does not have any table control / looping at screen table. The same can also be enhanced to develop a program for transactions involving table controls .

1. Create a new program as executable program using SE38 transaction code.

2. Copy the following template code into your program .

----


Start of Template -

REPORT <Your Prgram Name>

NO STANDARD PAGE HEADING

LINE-SIZE 200

LINE-COUNT 300.

*--


DATA DECLARATION--


*---Types

DATA : BEGIN OF t_upload,

FIELD1(10),

FIELD2(2),

FIELD3(18),

FIELD4(35),

END OF t_upload.

*--- Tables

DATA : BEGIN OF i_bdcdata OCCURS 0."to hold the transaction record

INCLUDE STRUCTURE bdcdata.

DATA: END OF i_bdcdata.

DATA: i_upload LIKE STANDARD TABLE OF t_upload," to hold file data.

i_upload1 LIKE STANDARD TABLE OF t_upload." to hold file data.

*--- Work Areas

DATA: wa_upload2 LIKE t_upload,

wa_upload LIKE t_upload,

wa_upload1 LIKE t_upload.

*--- Variables

DATA: v_count1(4) TYPE n,

v_error TYPE c,

v_session(12),

v_field(21) TYPE c,

v_message(60) type 'C'.

*--Constants

DATA: c_open TYPE c VALUE '(',

c_close TYPE c VALUE ')',

c_x TYPE c VALUE 'X'.

*---Initialisation

initialization.

refresh : i_upload , i_upload1 ,i_bdcdata.

-------Selection Screen Design -


*Selection screen for input of upload file address

SELECTION-SCREEN SKIP 2.

SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME.

PARAMETERS : p_file LIKE rlgrap-filename OBLIGATORY.

SELECTION-SCREEN END OF BLOCK blk1.

---AT SELECTION SCREEN -


AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

*--For popup to select file.

PERFORM give_help.

-----START OF SELECTION -


START-OF-SELECTION.

*--Data upload using WS_Upload.

PERFORM get_data.

*-- OPEN SESSION

PERFORM open_group.

*--Insert transactions using BDCDATA table in the session.

PERFORM do_transaction .

*-- Close the session.

PERFORM close_group.

END-OF-SELECTION.

&----


*& Form f_get_data

&----


  • For data upload from external file.

----


FORM get_data.

CALL FUNCTION 'WS_UPLOAD'

EXPORTING

filename = p_file

filetype = 'DAT'

TABLES

data_tab = i_upload

EXCEPTIONS

conversion_error = 1

file_open_error = 2

file_read_error = 3

invalid_type = 4

no_batch = 5

unknown_error = 6

invalid_table_width = 7

gui_refuse_filetransfer = 8

customer_error = 9

OTHERS = 10.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ELSE.

DELETE I_UPLOAD INDEX 1.

ENDIF.

ENDFORM. " f_get_data

&----


*& Form F_open_group

&----


  • To open session in session management.

----


FORM open_group.

v_session = 'TCODE'.

CALL FUNCTION 'BDC_OPEN_GROUP'

EXPORTING

client = sy-mandt

group = v_session

user = sy-uname

keep = 'X'.

ENDFORM. " F_open_group

&----


*& Form f_do_transaction

&----


  • Insert transactions in session after passing values to BDCDATA

----


FORM do_transaction.

LOOP AT i_upload INTO wa_upload .

*---- insert your generated codes from recording at SHDB here

*----- insertion ends

perform bdc_transaction using 'TCODE'.

REFRESH : I_BDCDATA.

CLEAR : WA_UPLOAD.

  • ENDIF.

ENDLOOP.

ENDFORM. " f_do_transaction

&----


*& Form bdc_dynpro

&----


  • For appending screen details to BDCDATA

----


FORM bdc_dynpro USING program dynpro.

CLEAR i_bdcdata.

i_bdcdata-program = program.

i_bdcdata-dynpro = dynpro.

i_bdcdata-dynbegin = 'X'.

APPEND i_bdcdata.

CLEAR i_bdcdata.

ENDFORM. "bdc_dynpro

&----


*& Form bdc_field

&----


  • For appending field details to bdcdata table

----


FORM bdc_field USING fnam fval.

CLEAR i_bdcdata.

i_bdcdata-fnam = fnam.

i_bdcdata-fval = fval.

APPEND i_bdcdata.

CLEAR i_bdcdata.

ENDFORM. " bdc_field

&----


*& Form bdc_transaction

&----


  • For inserting Transaction in the session

----


FORM bdc_transaction USING tcode.

CALL FUNCTION 'BDC_INSERT'

EXPORTING

tcode = tcode

TABLES

dynprotab = i_bdcdata.

ENDFORM. " bdc_transaction

&----


*& Form F_close_group

&----


  • For closing the session created in Session manager SM35

----


FORM close_group.

CALL FUNCTION 'BDC_CLOSE_GROUP'.

concatenate 'Session ' v_session 'successfully created' into v_field.

MESSAGE v_field type 'I'..

CALL TRANSACTION 'SM35'.

ENDFORM. "f_close_group

&----


*& Form f_give_help

&----


  • For user help to select file

----


FORM give_help.

CALL FUNCTION 'WS_FILENAME_GET'

EXPORTING

mask = ',.,..'

mode = 'O'

IMPORTING

filename = p_file

EXCEPTIONS

inv_winsys = 1

no_batch = 2

selection_cancel = 3

selection_error = 4

OTHERS = 5.

IF sy-subrc <> 0 AND NOT sy-msgty IS INITIAL.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

ENDFORM. " f_give_help

----


End of Template -

3.Go to the transaction SHDB in another session ( you can also give TCODE - OSHDB in transaction code window and hit enter ).

4.Create you transaction recording in SHDB . Ensure your transaction recording takes care of input values to all the

fields to which you intend to pass values in the BDC.

5.Once your recording is done correctly create the program from recording in the SHDB transction . You will find the button to create program in SHDB . Choose the option of creating from file when you proceed through the subsequent steps.

6. Define data type t_upload and structure wa_upload in the template program with fields of structure 'RECORD' from the program generated using SHDB recording ( replace the field1, field2 and so on as per your requirement). For your understanding you can remove the suffixes to the

field name but keep the field size unchanged.

7.Copy the coding existing between 'do' and 'enddo' statement from the generated program . Insert the copied code between the loop and endloop code of form 'Do_transaction '. Replace fields of structure 'RECORD' with respective fields of structure wa_upload.Insert constant values wherever possible in transaction recording.You can also handle customised data conversions here.

8.Replace 'TCODE' in the template program with the transaction code you intend to process in this BDC.

9.Please carry out further syntax check and resolve the related issues.

This program will provide for input help to select upload file from local machine. The file needs to be in Tab delimited format and is assumed to have first row as column headers .

On successful creation of session you will be prompted with a Information popup giving the name of session ,and will take you to the SM35 transaction to process your session.

Edited by: Kanthi.Sajja on Nov 6, 2009 10:34 AM

Edited by: Kanthi.Sajja on Nov 6, 2009 10:36 AM

Read only

Former Member
0 Likes
2,850

This message was moderated.