Thursday, June 14, 2018

BDC Sample Program Template



Batch Data Communication is one of the easiest  transaction automation options available 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
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.

No comments:

Post a Comment