SAP Help Example ABAP source code for enhanced SAP BDC program using call transaction









Enhanced ABAP source code for SAP BDC Call Transaction program


*Code used to create BDC
REPORT zf005rep NO STANDARD PAGE HEADING
                LINE-SIZE 212.

*---------------------------------------------------------------------------------*
* Data declaration
TABLES: kna1, industype.

TYPES: BEGIN OF t_kna1,
  dummy(2)    TYPE c,
  check    TYPE c,
  kunnr     LIKE kna1-kunnr,
  name1     LIKE kna1-name1,
  sortl     LIKE kna1-sortl,
  j_1kftind LIKE kna1-j_1kftind,
  new_inst  LIKE kna1-j_1kftind,
  err_msg   LIKE t100-text,
 END OF t_kna1.

DATA: it_kna1  TYPE STANDARD TABLE OF t_kna1 INITIAL SIZE 0,
      wa_kna1  TYPE t_kna1,
      it_error TYPE STANDARD TABLE OF t_kna1 INITIAL SIZE 0,
      wa_error TYPE t_kna1.

DATA: w_textout            LIKE t100-text.
DATA: gd_update TYPE i,
      gd_lines TYPE i.

*Used to store BDC data
DATA: BEGIN OF bdc_tab OCCURS 0.
        INCLUDE STRUCTURE bdcdata.
DATA: END OF bdc_tab.

*Used to stores error information from CALL TRANSACTION Function Module
DATA: BEGIN OF messtab OCCURS 0.
        INCLUDE STRUCTURE bdcmsgcoll.
DATA: END OF messtab.


*---------------------------------------------------------------------------------*
*Screen declaration
SELECTION-SCREEN BEGIN OF BLOCK block1 WITH FRAME
                                    TITLE text-001.
SELECT-OPTIONS: so_kunnr FOR kna1-kunnr OBLIGATORY,
                p_ftind FOR industype-indtype.

SELECTION-SCREEN END OF BLOCK block1.

SELECTION-SCREEN BEGIN OF BLOCK block2 WITH FRAME
                                    TITLE text-002.
PARAMETERS:  p_new   LIKE kna1-j_1kftind.
SELECTION-SCREEN END OF BLOCK block2.


************************************************************************
*AT SELECTION-SCREEN.
AT SELECTION-SCREEN.

* Check for valid instalment text
  LOOP AT p_ftind.
    CALL FUNCTION 'FIELD_EXIT_INDTYP'
         EXPORTING
              input  = p_ftind-low
         IMPORTING
              output = p_ftind-low.

    CALL FUNCTION 'FIELD_EXIT_INDTYP'
         EXPORTING
              input  = p_ftind-high
         IMPORTING
              output = p_ftind-high.
   modify p_ftind.
  ENDLOOP.

  CALL FUNCTION 'FIELD_EXIT_INDTYP'
       EXPORTING
            input  = p_new
       IMPORTING
            output = p_new.


************************************************************************
*START-OF-SELECTION
START-OF-SELECTION.

* Retrieve data from customer master table(KNA1)
  SELECT kunnr name1 sortl j_1kftind
    INTO CORRESPONDING FIELDS OF TABLE it_kna1
    FROM kna1
   WHERE kunnr IN so_kunnr AND
         j_1kftind IN p_ftind.


************************************************************************
*END-OF-SELECTION
END-OF-SELECTION.
* Check data has been retrieved ready for processing
  DESCRIBE TABLE it_kna1 LINES gd_lines.
  IF gd_lines LE 0.
*   Display message if no data has been retrieved
    MESSAGE i003(zp) WITH 'No Records Found'(003).
  ELSE.
*   Set GUI status for screen
    PERFORM set_gui_status.

*   Display column headings
    PERFORM display_column_headings.

*   Display Report
    PERFORM display_report.
  ENDIF.


************************************************************************
*AT USER-COMMAND.
AT USER-COMMAND.
  CLEAR: bdc_tab, gd_update.
  REFRESH: bdc_tab.
  CASE sy-ucomm.
*:::WHEN.............................Perform customer instalment update
    WHEN 'SAVE'.
      DO.
*     "Reads the who report line
*     read line sy-index line value into wa_kna1

        CLEAR wa_kna1.
*       Read displayed report line
*       Only retreives specific fields i.e. kna1-check
        READ LINE sy-index FIELD VALUE wa_kna1-check INTO wa_kna1-check
                                       wa_kna1-kunnr INTO wa_kna1-kunnr
                                       wa_kna1-name1 INTO wa_kna1-name1
                                       wa_kna1-sortl INTO wa_kna1-sortl
                                       wa_kna1-j_1kftind INTO
                                                     wa_kna1-j_1kftind
                                       p_new INTO p_new.
*       If NE 0 then end of report has been reached
        IF sy-subrc NE 0.
          EXIT.
        ELSE.
          CASE wa_kna1-check.
            WHEN 'X'.
*             Update Customer master data (instalment text)
              PERFORM bdc_update.
          ENDCASE.
        ENDIF.
      ENDDO.
*     Display message confirming number of records updated
      IF gd_update GT 1.
        MESSAGE i003(zp) WITH gd_update 'Records updated'(004).
      ELSE.
        MESSAGE i003(zp) WITH gd_update 'Record updated'(005).
      ENDIF.

*     Check errors table
      DESCRIBE TABLE it_error LINES gd_lines.
*     If errors exist then display errors report
      IF gd_lines GT 0.
*       Display errors report
        PERFORM display_error_headings.
        PERFORM display_error_report.
      ELSE.
*       If no errors then return to selection screen
        LEAVE TO SCREEN 0.
      ENDIF.

*:::WHEN............................Select all entries on report
    WHEN 'ALL'.
*     Set all check values to 'X'
      LOOP AT it_kna1 INTO wa_kna1.
        wa_kna1-check = 'X'.
        MODIFY it_kna1 FROM wa_kna1.
      ENDLOOP.

*     Display column headings
      PERFORM display_column_headings.

*     Display Report
      PERFORM display_report.
      sy-lsind = sy-lsind - 1.

*:::WHEN............................De-select all entries on report
    WHEN 'DESEL'.
*     Set all check values to space
      LOOP AT it_kna1 INTO wa_kna1.
        wa_kna1-check = ' '.
        MODIFY it_kna1 FROM wa_kna1.
      ENDLOOP.

*     Display column headings
      PERFORM display_column_headings.

*     Display Report
      PERFORM display_report.
      sy-lsind = sy-lsind - 1.
  ENDCASE.


*&---------------------------------------------------------------------*

*& Form DISPLAY COLUMN HEADINGS

*&---------------------------------------------------------------------* * Display column headings *---------------------------------------------------------------------------------* FORM display_column_headings. WRITE: sy-uline(135). FORMAT COLOR COL_HEADING. WRITE:/ sy-vline, ' ', sy-vline, (10) 'Customer'(006), sy-vline, (35) 'Name'(007), sy-vline, (10) 'Sort'(008), sy-vline, (30) 'Instalment plan'(009), sy-vline, (30) 'New Instalment plan'(010), sy-vline. WRITE: sy-uline(135). FORMAT COLOR COL_NORMAL. ENDFORM. " DISPLAY_COLUMN_HEADINGS *&---------------------------------------------------------------------*

*& Form SET GUI STATUS

*&---------------------------------------------------------------------* * Sets gui status for screen (pf-status) *---------------------------------------------------------------------------------* FORM set_gui_status. SET PF-STATUS 'REPORT'. ENDFORM. " SET_GUI_STATUS *&---------------------------------------------------------------------*

*& Form BDC UPDATE

*&---------------------------------------------------------------------* * Populate BDC table and call transaction FD02 *---------------------------------------------------------------------------------* FORM bdc_update. PERFORM dynpro USING: 'X' 'SAPMF02D' '0106', ' ' 'BDC_CURSOR' 'RF02D-D0120', ' ' 'RF02D-KUNNR' wa_kna1-kunnr, "Customer num ' ' 'RF02D-D0120' 'X', ' ' 'BDC_OKCODE' '/00', "OK code 'X' 'SAPMF02D' '0120', ' ' 'BDC_CURSOR' 'KNA1-J_1KFTIND', ' ' 'KNA1-J_1KFTIND' p_new, ' ' 'BDC_OKCODE' '=UPDA'. "OK code * Call transaction to update customer instalment text CALL TRANSACTION 'FD02' USING bdc_tab MODE 'N' UPDATE 'S' MESSAGES INTO messtab. * Check if update was succesful IF sy-subrc EQ 0. ADD 1 TO gd_update. ELSE. * Retrieve error messages displayed during BDC update LOOP AT messtab WHERE msgtyp = 'E'. * Builds actual message based on info returned from Call transaction CALL FUNCTION 'MESSAGE_TEXT_BUILD' EXPORTING msgid = messtab-msgid msgnr = messtab-msgnr msgv1 = messtab-msgv1 msgv2 = messtab-msgv2 msgv3 = messtab-msgv3 msgv4 = messtab-msgv4 IMPORTING message_text_output = w_textout. ENDLOOP. * Build error table, display as report which allows reprocessing wa_error = wa_kna1. wa_error-check = ' '. wa_error-new_inst = p_new. wa_error-err_msg = w_textout. APPEND wa_error TO it_error. CLEAR: wa_error. ENDIF. * Clear bdc date table CLEAR: bdc_tab. REFRESH: bdc_tab. ENDFORM. " BDC_UPDATE *---------------------------------------------------------------------------------*

* FORM DYNPRO *

*---------------------------------------------------------------------------------* * stores values to bdc table * *---------------------------------------------------------------------------------* * --> DYNBEGIN * * --> NAME * * --> VALUE * *---------------------------------------------------------------------------------* FORM dynpro USING dynbegin name value. IF dynbegin = 'X'. CLEAR bdc_tab. MOVE: name TO bdc_tab-program, value TO bdc_tab-dynpro, 'X' TO bdc_tab-dynbegin. APPEND bdc_tab. ELSE. CLEAR bdc_tab. MOVE: name TO bdc_tab-fnam, value TO bdc_tab-fval. APPEND bdc_tab. ENDIF. ENDFORM. " DYNPRO *&---------------------------------------------------------------------*

*& Form DISPLAY REPORT

*&---------------------------------------------------------------------* * Display Report *---------------------------------------------------------------------------------* FORM display_report. * Sort table by sort field (it_kna1-sortl). SORT it_kna1 BY sortl. * Loop at data table LOOP AT it_kna1 INTO wa_kna1. WRITE:/ sy-vline, wa_kna1-check AS CHECKBOX, sy-vline, (10) wa_kna1-kunnr, sy-vline, (35) wa_kna1-name1, sy-vline, (10) wa_kna1-sortl, sy-vline, (30) wa_kna1-j_1kftind, sy-vline, (30) p_new, sy-vline. HIDE: wa_kna1, p_new. CLEAR: wa_kna1. ENDLOOP. WRITE: sy-uline(135). ENDFORM. " DISPLAY_REPORT *&---------------------------------------------------------------------*

*& Form DISPLAY ERROR REPORT

*&---------------------------------------------------------------------* * Display error report data *---------------------------------------------------------------------------------* FORM display_error_report. LOOP AT it_error INTO wa_kna1. WRITE:/ sy-vline, wa_kna1-check AS CHECKBOX, sy-vline, (10) wa_kna1-kunnr, sy-vline, (35) wa_kna1-name1, sy-vline, (10) wa_kna1-sortl, sy-vline, (30) wa_kna1-j_1kftind, sy-vline, (30) p_new, sy-vline, (73) wa_kna1-err_msg, sy-vline. HIDE: wa_kna1, p_new. CLEAR: wa_kna1. ENDLOOP. WRITE: sy-uline(211). sy-lsind = sy-lsind - 1. REFRESH: it_error. ENDFORM. " DISPLAY_ERROR_REPORT *&---------------------------------------------------------------------*

*& Form DISPLAY ERROR HEADINGS

*&---------------------------------------------------------------------* * Display error report headings *---------------------------------------------------------------------------------* FORM display_error_headings. WRITE:2 ' Error Report '(018) COLOR COL_NEGATIVE. SKIP. WRITE:2 'Ther following records failed during update:'(017). WRITE: sy-uline(211). FORMAT COLOR COL_HEADING. WRITE:/ sy-vline, ' ', sy-vline, (10) 'Customer'(011), sy-vline, (35) 'Name'(012), sy-vline, (10) 'Sort'(013), sy-vline, (30) 'Instalment plan'(014), sy-vline, (30) 'New Instalment plan'(015), sy-vline, (73) 'Error Message'(016), sy-vline. WRITE: sy-uline(211). FORMAT COLOR COL_NORMAL. ENDFORM. " DISPLAY_ERROR_HEADINGS