Make your own free website on Tripod.com
SAP ABAP Code Shrikant Naidu

Home
Site Map
What is SAP ?
SAP LSMW Explained with example
User Exits & Enhancements
Best Of SAP Links
BDC Code
SAP SmartForms Step by Step
SAP ABAP ALV Grid Explained with Example
Useful Tips
ABAP System Fields
SAP Script
SAP Script Graphics
SAPScript Print Program
ABAP ListViewer
Dialog Programming
SAP Tables
SAP ALE and IDOC
Recommended SAP ABAP Coding guidelines
SAP General ABAP
SAP Books For consultants
About Me
Contact Me
My Resume
BDC Code

This is the actual BDC  CODE used in the production system .

 

REPORT zmmat_mast

       NO STANDARD PAGE HEADING LINE-SIZE 200.

*---------------------------------------------------------------------*

*Programmer :  Shrikant Naidu                                            *

*Website       :  http://naidu.shrikant.tripod.com/                *

*Email           :  naidu.shrikant@rediffmail.com                       *

*---------------------------------------------------------------------*

 

 

TABLES :mara, marc.

*---------------------------------------------------------------------*

*            INTERNAL TABLES                                          *

*---------------------------------------------------------------------*

 

DATA: BEGIN OF bdcdata OCCURS 0.

        INCLUDE STRUCTURE bdcdata.

DATA: END OF bdcdata.

 

DATA:BEGIN OF itab_basic OCCURS 0,

   matnr LIKE mara-matnr,

   zzmatnr LIKE mara-matnr,

END OF itab_basic.

 

DATA:BEGIN OF itab_mrp OCCURS 0,

   matnr LIKE mara-matnr,

   werks LIKE marc-werks,

   zzgpind LIKE marc-zzgpind,

   zzdate(10) type C ,

END OF itab_mrp.

 

DATA: BEGIN OF tab_mara OCCURS 0.

        INCLUDE STRUCTURE mara.

DATA: END OF tab_mara.

 

DATA: BEGIN OF tab_marc OCCURS 0.

        INCLUDE STRUCTURE marc.

DATA: END OF tab_marc.

 

DATA: ertab_basic LIKE itab_basic OCCURS 0 WITH HEADER LINE.

DATA: ertab_mrp LIKE itab_mrp OCCURS 0 WITH HEADER LINE.

 

DATA : v_ctr TYPE  i VALUE 1.  "Ctr for no orecords

DATA : v_group(12) .    "BDC Group Name

DATA : v_group_ctr(3) TYPE n . "BDC Group Counter

 

DATA : v_no_lines TYPE i.

DATA:  v_grpname(10) TYPE c.

 

SELECTION-SCREEN BEGIN OF BLOCK dt-entry

               WITH FRAME TITLE text-002.

selection-screen  begin of line .

selection-screen comment 01(49) text-101.

parameters : p_basic  radiobutton group r1.

selection-screen end of line.

selection-screen begin of line .

selection-screen comment  01(49) text-102.

parameters : p_mrp2  radiobutton group r1.

selection-screen end of line.

 

 

*

*PARAMETERS :

*       p_basic  RADIOBUTTON  GROUP r1,

*       p_mrp2  RADIOBUTTON GROUP r1.

SELECTION-SCREEN END   OF BLOCK dt-entry.

 

 

SELECTION-SCREEN SKIP 1.

 

SELECTION-SCREEN BEGIN OF BLOCK fl-info

                 WITH FRAME TITLE text-003.

PARAMETER : p_dlhead AS CHECKBOX . "DEFAULT 'X'.

SELECTION-SCREEN END   OF BLOCK fl-info.

*

SELECTION-SCREEN SKIP 1.

 

PARAMETER : p_max TYPE i DEFAULT '5000' . "No of rec / session

 

 

 

SET PF-STATUS 'UPLOAD'.

 

AT USER-COMMAND .

  IF sy-ucomm = 'UPLOAD' .

    IF p_basic = 'X'.

      IF NOT itab_basic[] IS INITIAL .

        PERFORM bdc_upload_basic.

      ENDIF.

 

      WRITE:/10 'Please execute the session in SM35 to Upload the data'.

      FORMAT INTENSIFIED OFF COLOR OFF .

 

      REFRESH itab_basic . CLEAR itab_basic .

      SET PF-STATUS space.

    ENDIF.

    IF p_mrp2 = 'X'.

      IF NOT itab_mrp[] IS INITIAL .

        PERFORM bdc_upload_mrp.

      ENDIF..

      WRITE:/10 'Please execute the session in SM35 to Upload the data'.

 

      REFRESH itab_mrp . CLEAR itab_mrp .

      SET PF-STATUS space.

    ENDIF.

 

  ENDIF.

 

 

START-OF-SELECTION.

 

  IF p_basic = 'X'.

    CALL FUNCTION 'UPLOAD'

                 EXPORTING

*                 CODEPAGE                      = ' '

               filename                 = 'C:\mm02_basic.txt '

 

                   filetype                      = 'DAT'

                  TABLES

                    data_tab                      = itab_basic.

 

    IF p_dlhead EQ 'X'.

      READ TABLE itab_basic INDEX 1.

      DELETE itab_basic INDEX 1.

      CLEAR itab_basic.

    ENDIF.

    PERFORM blank_data_validation.

 

  ENDIF.

 

  IF p_mrp2 = 'X'.

* v_grpname = 'MRP2'.

    CALL FUNCTION 'UPLOAD'

                   EXPORTING

*                 CODEPAGE                      = ' '

                 filename                 =    'C:\mm02_mrp.txt '

 

                     filetype                      = 'DAT'

                    TABLES

                      data_tab                      = itab_mrp.

 

 

    IF p_dlhead EQ 'X'.

      READ TABLE itab_mrp INDEX 1.

      DELETE itab_mrp INDEX 1.

      CLEAR itab_mrp.

    ENDIF.

    PERFORM valid_plant_check.

 

 

 

  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.

 

  PERFORM display_data.

 

END-OF-SELECTION.

 

*----------------------------------------------------------------------

*

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

  CLEAR bdcdata.

  bdcdata-fnam = fnam.

  bdcdata-fval = fval.

  APPEND bdcdata.

ENDFORM.

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

*&      Form  blank_data_validation

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

*       text

*----------------------------------------------------------------------*

*  -->  p1        text

*  <--  p2        text

*----------------------------------------------------------------------*

FORM blank_data_validation.

 

  LOOP AT itab_basic.

    IF itab_basic-matnr IS INITIAL OR itab_basic-zzmatnr IS INITIAL.

      MOVE-CORRESPONDING itab_basic TO ertab_basic .

      APPEND ertab_basic . CLEAR ertab_basic .

      DELETE itab_basic.

      CLEAR itab_basic.

      CONTINUE.

    ENDIF.

 

 SELECT SINGLE * INTO tab_mara FROM mara WHERE matnr = itab_basic-matnr

           .

    IF sy-subrc <> 0.

      MOVE-CORRESPONDING itab_basic TO ertab_basic .

      APPEND ertab_basic . CLEAR ertab_basic .

      DELETE itab_basic.

      CLEAR itab_basic.

      CONTINUE.

    ENDIF.

*

 

  ENDLOOP.

ENDFORM.                    " dt_qty_validation

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

*&      Form  CALL_BDC_CLOSE_GROUP

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

*       text

*----------------------------------------------------------------------*

*  -->  p1        text

*  <--  p2        text

*----------------------------------------------------------------------*

FORM call_bdc_close_group.

  CALL FUNCTION 'BDC_CLOSE_GROUP'

       EXCEPTIONS

            not_open    = 1

            queue_error = 2.

 

  IF sy-subrc NE 0.

    WRITE: /5 'BDC CLOSE GROUP FAILED, return code = ', sy-subrc.

    EXIT.

 

  ENDIF.

 

ENDFORM.                    " CALL_BDC_CLOSE_GROUP

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

*&      Form  CALL_BDC_INSERT

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

*       text

*----------------------------------------------------------------------*

*      -->P_0545   text

*----------------------------------------------------------------------*

FORM call_bdc_insert USING  v_tran_code.

 

  DATA: xmode VALUE 'E'.

  CALL FUNCTION 'BDC_INSERT'

       EXPORTING

            tcode          = v_tran_code

       TABLES

            dynprotab      = bdcdata

       EXCEPTIONS

            internal_error = 1

            not_open       = 2

            queue_error    = 3.

  IF sy-subrc NE 0.

    WRITE: /5 'BDC insert GROUP FAILED, return code = ', sy-subrc.

    EXIT.

  ENDIF.

 

ENDFORM.                    " CALL_BDC_INSERT

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

*&      Form  display_data

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

*       text

*----------------------------------------------------------------------*

*  -->  p1        text

*  <--  p2        text

*----------------------------------------------------------------------*

FORM display_data.

 

  WRITE:/ 'Press','''Upload''',

       'to create session with the following records'.

  IF p_basic = 'X'.

    PERFORM display_header.

    FORMAT COLOR 2.

    LOOP AT itab_basic.

 

      WRITE :/2 itab_basic-matnr,'|',

              23 itab_basic-zzmatnr,

              54 '|'.

ENDIF.

    ENDLOOP.

    NEW-LINE.

    ULINE (54).

  ENDIF.

 

  IF p_mrp2 = 'X'.

    PERFORM display_header_mrp.

    FORMAT COLOR 2.

    LOOP AT itab_mrp.

 

      WRITE :/2 itab_mrp-matnr,'|',

              23 itab_mrp-werks,

              33 '|',

              37 itab_mrp-zzgpind,

              49 '|',

              52 itab_mrp-ZZDATE,

              65 '|'.

 

    ENDLOOP.

    NEW-LINE.

    ULINE (65).

    FORMAT COLOR OFF.

    SKIP 4.

 

    WRITE :/ 'Records with Errors will not be Included in Session' .

    PERFORM display_header_mrp.

    FORMAT COLOR 2.

 

    LOOP AT ertab_mrp.

      IF NOT ertab_mrp IS INITIAL OR NOT ertab_mrp-matnr IS INITIAL.

        WRITE :/2 ertab_mrp-matnr ,'|',

             23 ertab_mrp-werks,

             33 '|',

             37 ertab_mrp-zzgpind,

             49 '|' ,

             52 ertab_mrp-zzdate,

             65 '|'.

 

      ENDIF.

    ENDLOOP.

    NEW-LINE.

    ULINE (65).

  ENDIF.

 

 

 

  FORMAT COLOR OFF.

ENDFORM.                    " display_data

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

*&      Form  BDC_UPLOAD_basic

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

*       text

*------------------------------------------------------------------------*

*  -->  p1        text

*  <--  p2        text

*------------------------------------------------------------------------*

FORM bdc_upload_basic.

 

*CLEAR itab_basic. REFRESH itab_basic.

  CLEAR : v_ctr , v_no_lines .

  IF NOT itab_basic[] IS INITIAL .

    v_group_ctr = '1' .

    CONCATENATE 'BASIC_'  v_group_ctr  INTO v_group .

    PERFORM open_group USING v_group .

 

    LOOP AT itab_basic .

 

      IF v_ctr GE p_max .

        PERFORM call_bdc_close_group .

        v_group_ctr = v_group_ctr + 1 .

        CLEAR v_group .

        CONCATENATE 'BASIC_'  v_group_ctr  INTO v_group .

        PERFORM open_group USING v_group .

        v_ctr = 0 .

      ENDIF.

 

      PERFORM bdc_dynpro      USING 'SAPLMGMM' '0060'.

      PERFORM bdc_field       USING 'BDC_CURSOR'

                                    'RMMG1-MATNR'.

      PERFORM bdc_field       USING 'BDC_OKCODE'

                                    '/00'.

      PERFORM bdc_field       USING 'RMMG1-MATNR'

                                     itab_basic-matnr.

*                              'DEEPAKTESTMAT'.

      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'

                                    '=BU'.

      PERFORM bdc_field       USING 'BDC_CURSOR'

                                    'MARA-ZZMATNR'.

      PERFORM bdc_field       USING 'MARA-ZZMATNR'

                                      itab_basic-zzmatnr.

*                              'ADV#AM29F010-70JI'.

 

      PERFORM call_bdc_insert USING 'MM02'.

      CLEAR bdcdata . REFRESH bdcdata .

      v_no_lines  = v_no_lines  + 1  .

      v_ctr = v_ctr + 1.

 

    ENDLOOP.

 

    PERFORM call_bdc_close_group.

    SKIP 10.

    WRITE :/10 'Session by name BASIC* created' .

    WRITE :/10 'Total No of records uploaded - ' , v_no_lines .

 

  ENDIF.

ENDFORM.                    " BDC_UPLOAD

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

*&      Form  display_header

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

*       text

*-----------------------------------------------------------------------*

*  -->  p1        text

*  <--  p2        text

*-----------------------------------------------------------------------*

FORM display_header.

  NEW-LINE.

* set left scroll-boundary column 48.

  FORMAT COLOR 1 INTENSIFIED ON.

  ULINE (54).

  WRITE :/' Avnet Material',

  21'|' ,

  23 'Green Product '

  , 54 '|'.

  NEW-LINE.

  ULINE (54).

  FORMAT COLOR OFF INTENSIFIED OFF.

ENDFORM.

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

*&      Form  bdc_upload_mrp

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

*       text

*------------------------------------------------------------------------*

*  -->  p1        text

*  <--  p2        text

*------------------------------------------------------------------------*

FORM bdc_upload_mrp.

 

*  PERFORM call_bdc_open_group2.

 

  CLEAR : v_ctr , v_no_lines .

  IF NOT itab_mrp[] IS INITIAL .

    v_group_ctr = '1' .

    CONCATENATE 'MRP_'  v_group_ctr  INTO v_group .

    PERFORM open_group USING v_group .

 

    LOOP AT itab_mrp .

 

      IF v_ctr GE p_max .

        PERFORM call_bdc_close_group .

        v_group_ctr = v_group_ctr + 1 .

        CLEAR v_group .

        CONCATENATE 'MRP2_'  v_group_ctr  INTO v_group .

        PERFORM open_group USING v_group .

        v_ctr = 0 .

      ENDIF.

      PERFORM bdc_dynpro      USING 'SAPLMGMM' '0060'.

      PERFORM bdc_field       USING 'BDC_CURSOR'

                                    'RMMG1-MATNR'.

      PERFORM bdc_field       USING 'BDC_OKCODE'

                                    '/00'.

      PERFORM bdc_field       USING 'RMMG1-MATNR'

                                    itab_mrp-matnr.

*                              'DEEPAKTESTMAT'.

      PERFORM bdc_dynpro      USING 'SAPLMGMM' '0070'.

      PERFORM bdc_field       USING 'BDC_CURSOR'

                                    'MSICHTAUSW-DYTXT(13)'.

      PERFORM bdc_field       USING 'BDC_OKCODE'

                                    '=ENTR'.

      PERFORM bdc_field       USING 'MSICHTAUSW-KZSEL(13)'

                                    'X'.

      PERFORM bdc_dynpro      USING 'SAPLMGMM' '0080'.

      PERFORM bdc_field       USING 'BDC_CURSOR'

                                    'RMMG1-WERKS'.

      PERFORM bdc_field       USING 'BDC_OKCODE'

                                    '=ENTR'.

      PERFORM bdc_field       USING 'RMMG1-WERKS'

                                     itab_mrp-werks.

*                              'sg11'.

      PERFORM bdc_dynpro      USING 'SAPLMGMM' '4000'.

      PERFORM bdc_field       USING 'BDC_OKCODE'

                                    '/00'.

      PERFORM bdc_field       USING 'BDC_CURSOR'

                                    'MARC-ZZGPIND'.

      PERFORM bdc_field       USING 'MARC-ZZGPIND'

                                    itab_mrp-zzgpind.

*    'G'.

      PERFORM bdc_field       USING 'BDC_CURSOR'

                                    'MARC-ZZDATE'.

      PERFORM bdc_field       USING 'MARC-ZZDATE'

                                    itab_mrp-zzdate.

*    'G'.

      PERFORM bdc_dynpro      USING 'SAPLSPO1' '0300'.

      PERFORM bdc_field       USING 'BDC_OKCODE'

                                    '=YES'.

      PERFORM call_bdc_insert USING 'MM02'.

      CLEAR bdcdata . REFRESH bdcdata .

      v_no_lines  = v_no_lines  + 1  .

      v_ctr = v_ctr + 1.

 

    ENDLOOP.

 

    PERFORM call_bdc_close_group.

    SKIP 10.

    WRITE :/10 'Session by name MRP2* created' .

    WRITE :/10 'Total No of records uploaded - ' , v_no_lines .

  ENDIF.

ENDFORM.                    " bdc_upload_mrp

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

*&      Form  display_header_mrp

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

*       text

*-----------------------------------------------------------------------*

*  -->  p1        text

*  <--  p2        text

*-----------------------------------------------------------------------*

FORM display_header_mrp.

  NEW-LINE.

* set left scroll-boundary column 48.

  FORMAT COLOR 1 INTENSIFIED ON.

  ULINE (65).

  WRITE :/' Avnet Material',

  21'|' ,

  23 'Plant '  ,

  33 '|',

  35 'Indicator',

  49 '|',

  52 'Date',

  65 '|' .

  NEW-LINE.

  ULINE (65).

  FORMAT COLOR OFF INTENSIFIED OFF.

 

ENDFORM.                    " display_header_mrp

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

*&      Form  valid_plant_check

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

*       text

*-----------------------------------------------------------------------*

*  -->  p1        text

*  <--  p2        text

*-----------------------------------------------------------------------*

FORM valid_plant_check.

  LOOP AT itab_mrp.

    IF itab_mrp-matnr IS INITIAL OR itab_mrp-werks IS INITIAL OR

itab_mrp-zzgpind IS INITIAL.

      MOVE-CORRESPONDING itab_mrp TO ertab_mrp .

      APPEND ertab_mrp . CLEAR ertab_mrp .

      DELETE itab_mrp.

      CLEAR itab_mrp.

      CONTINUE.

    ENDIF.

 

   SELECT SINGLE * INTO tab_marc FROM marc WHERE matnr = tab_marc-matnr

     AND werks = itab_mrp-werks .     .

    IF sy-subrc = 0.

      MOVE-CORRESPONDING itab_mrp TO ertab_mrp .

      APPEND ertab_mrp . CLEAR ertab_mrp .

      DELETE itab_mrp.

      CLEAR itab_mrp.

      CONTINUE.

    ENDIF.

 

  ENDLOOP.

ENDFORM.                    " valid_plant_check

 

 

*---------------------------------------------------------------------*

*       FORM open_group                                                  *

*---------------------------------------------------------------------*

*       ........                                                                             *

*---------------------------------------------------------------------*

FORM open_group USING p_grp .

* open batchinput group

  CALL FUNCTION 'BDC_OPEN_GROUP'

       EXPORTING

            client = sy-mandt

            group  = p_grp

            user   = sy-uname

            keep   = 'X'.

ENDFORM.

 

*---------------  This ends the BDC program--  -----------------------------------------*

*Web Site   http://naidu.shrikant.tripod.com/------------------------------  *

*SAP Forum   http://sapshrikant.freeforumsite.com------------------------------------ *

  

 

Shrikant Naidu SAP ABAP Developer (INDIA)