Thursday, February 7, 2013

Run-time Error in ZTABLE

Below Run-time Error comes when you have not defined Primary key in ZTABLE.  SO first Define Primary Key in ZTABEL





Runtime Errors         SAPSQL_ARRAY_INSERT_DUPREC
Except.                CX_SY_OPEN_SQL_DB
Date and Time          08.02.2013 12:15:59
----------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------
|Short text                                                                                        |
|    The ABAP/4 Open SQL array insert results in duplicate database records.                       |
----------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------
|What happened?                                                                                    |
|    Error in the ABAP Application Program                                                         |
|                                                                                                  |
|    The current ABAP program "SAPMZ_GATE_ENTRY" had to be terminated because it has               |
|    come across a statement that unfortunately cannot be executed.                                |
----------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------
|Error analysis                                                                                    |
|    An exception occurred that is explained in detail below.                                      |
|    The exception, which is assigned to class 'CX_SY_OPEN_SQL_DB', was not caught                 |
|     and                                                                                          |
|    therefore caused a runtime error.                                                             |
|    The reason for the exception is:                                                              |
|    If you use an ABAP/4 Open SQL array insert to insert a record in                              |
|    the database and that record already exists with the same key,                                |
|    this results in a termination.                                                                |
|                                                                                                  |
|    (With an ABAP/4 Open SQL single record insert in the same error                               |
|    situation, processing does not terminate, but SY-SUBRC is set to 4.)                          |
----------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------
|Missing Handling of System Exception                                                              |
|    Program                                 SAPMZ_GATE_ENTRY                                      |
----------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------
|Trigger Location of Exception                                                                     |
|    Program                                 SAPMZ_GATE_ENTRY                                      |
|    Include                                 MZ_GATE_ENTRY_USER_COMMAND_I01                        |
|    Row                                     94                                                    |
|    Module type                             (MODULE PAI)                                          |
|    Module Name                             INSERT_DATA_9000                                      |
----------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------
|Source Code Extract                                                                               |
----------------------------------------------------------------------------------------------------
|Line |SourceCde                                                                                   |
----------------------------------------------------------------------------------------------------
|   64| IMPORTING                                                                                  |
|   65|   SELECTED_TIME       = WA_GATE-TOUT                                                       |
|   66|          .                                                                                 |
|   67|ENDMODULE.                 " F4_TIME_OUT  INPUT                                             |
|   68|*&---------------------------------------------------------------------*                    |
|   69|*&      Module  INSERT_DATA_9000  INPUT                                                     |
|   70|*&---------------------------------------------------------------------*                    |
|   71|*       text                                                                                |
|   72|*----------------------------------------------------------------------*                    |
|   73|MODULE INSERT_DATA_9000 INPUT.                                                              |
|   74|  BREAK 10115.                                                                              |
|   75|  CASE sy-ucomm.                                                                            |
|   76|  WHEN 'SAVE'.                                                                              |
|   77|*    WA_INSERT-CNO = WA_GATE-CNO.                                                           |
|   78|*    WA_INSERT-TIN = WA_GATE-TIN.                                                           |
|   79|*    WA_INSERT-TOUT = WA_GATE-TOUT.                                                         |
|   80|*    WA_INSERT-NOA = WA_GATE-NOA.                                                           |
|   81|*    WA_INSERT-NOP = WA_GATE-NOP.                                                           |
|   82|*    WA_INSERT-VNO = WA_GATE-VNO.                                                           |
|   83|*    WA_INSERT-NOP1 = WA_GATE-NOP1.                                                         |
|   84|*    WA_INSERT-REMARKS = WA_GATE-REMARKS.                                                   |
|   85|    MOVE-CORRESPONDING wa_gate to  wa_insert.                                               |
|   86|    APPEND WA_INSERT TO IT_INSERT.                                                          |
|   87|    CLEAR WA_INSERT.                                                                        |
|   88|SELECT cno FROM zgateentry INTO wa_temp                                                     |
|   89|        WHERE cno = wa_gate-cno.                                                            |
|   90|        ENDSELECT.                                                                          |
|   91|if wa_temp-cno is INITIAL.                                                                  |
|   92|*    MOVE-CORRESPONDING WA_GATE TO WA_INSERT.                                               |
|   93|*    INSERT INTO ZGATEENTRY VALUES it_INSERT.                                               |
|>>>>>|    insert zgateentry from table it_insert.                                                 |
|   95|    ELSE.                                                                                   |
|   96|    MODIFY ZGATEENTRY FROM WA_INSERT.                                                       |
|   97|    ENDIF.                                                                                  |
|   98|*endif.                                                                                     |
|   99|*CLEAR wa_temp.                                                                             |
|  100|  IF sy-subrc = 0.                                                                          |
|  101|    MESSAGE 'Records created succesfully' TYPE 'S'.                                         |
|  102|  ENDIF.                                                                                    |
|  103|ENDCASE.                                                                                    |
|  104|*ENDIF.                                                                                     |
|  105|*CASE sy-ucomm.                                                                             |
|  106|                                                                                            |
|  107|*ENDCASE.                                                                                   |

TABLE CONTROL

PROCESS BEFORE OUTPUT.
*&SPWIZARD: PBO FLOW LOGIC FOR TABLECONTROL 'TC100'
  MODULE TC100_CHANGE_TC_ATTR.
*&SPWIZARD: MODULE TC100_CHANGE_COL_ATTR.
  LOOP AT   IT_TC1
       INTO WA_TC1
       WITH CONTROL TC100
       CURSOR TC100-CURRENT_LINE.
    MODULE TC100_GET_LINES.
*&SPWIZARD:   MODULE TC100_CHANGE_FIELD_ATTR
  ENDLOOP.


 MODULE STATUS_9000.
*
PROCESS AFTER INPUT.
*BREAK 10115.
*&SPWIZARD: PAI FLOW LOGIC FOR TABLECONTROL 'TC100'
  LOOP AT IT_TC1.
    CHAIN.
      FIELD WA_TC1-PART_CODE.
      FIELD WA_TC1-QTY.
      FIELD WA_TC1-UOM.
    endchain.
  ENDLOOP.
  MODULE TC100_USER_COMMAND.
*&SPWIZARD: MODULE TC100_CHANGE_TC_ATTR.
*&SPWIZARD: MODULE TC100_CHANGE_COL_ATTR.


 MODULE USER_COMMAND_9000.
 MODULE INSERT_DATA_9000.

PROCESS ON VALUE-REQUEST.
  FIELD WA_GATE-DATE_IN MODULE F4_DATE.
  FIELD WA_GATE-TIN MODULE F4_TIME.
  FIELD WA_GATE-TOUT MODULE F4_TIME_OUT.

MOdule Pool Problem

*----------------------------------------------------------------------*
***INCLUDE MZ_GATE_ENTRY_USER_COMMAND_I01 .
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_9000  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_9000 INPUT.
CASE sy-ucomm.
  WHEN 'EXIT' or 'CANCEL' or 'BACK'  .
      LEAVE PROGRAM.
ENDCASE.
ENDMODULE.                 " USER_COMMAND_9000  INPUT
*&---------------------------------------------------------------------*
*&      Module  F4_DATE  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE F4_DATE INPUT.
CALL FUNCTION 'F4_DATE'
 EXPORTING
   DATE_FOR_FIRST_MONTH               SY-DATUM
   DISPLAY                            ' '

 IMPORTING
   SELECT_DATE                        WA_GATE-DATE_IN

 EXCEPTIONS
   CALENDAR_BUFFER_NOT_LOADABLE       1
   DATE_AFTER_RANGE                   2
   DATE_BEFORE_RANGE                  3
   DATE_INVALID                       4
   FACTORY_CALENDAR_NOT_FOUND         5
   HOLIDAY_CALENDAR_NOT_FOUND         6
   PARAMETER_CONFLICT                 7
   OTHERS                             8.
ENDMODULE.                 " F4_DATE  INPUT
*&---------------------------------------------------------------------*
*&      Module  F4_TIME  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE F4_TIME INPUT.
CALL FUNCTION 'F4_CLOCK'
 EXPORTING
   START_TIME          SY-UZEIT
   DISPLAY             ' '
 IMPORTING
   SELECTED_TIME       WA_GATE-TIN
          .

ENDMODULE.                 " F4_TIME  INPUT
*&---------------------------------------------------------------------*
*&      Module  F4_TIME_OUT  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE F4_TIME_OUT INPUT.
CALL FUNCTION 'F4_CLOCK'
 EXPORTING
   START_TIME          SY-UZEIT
   DISPLAY             ' '
 IMPORTING
   SELECTED_TIME       WA_GATE-TOUT
          .
ENDMODULE.                 " F4_TIME_OUT  INPUT
*&---------------------------------------------------------------------*
*&      Module  INSERT_DATA_9000  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE INSERT_DATA_9000 INPUT.
  BREAK 10115.
  CASE sy-ucomm.
  WHEN 'SAVE'.
*    WA_INSERT-CNO = WA_GATE-CNO.
*    WA_INSERT-TIN = WA_GATE-TIN.
*    WA_INSERT-TOUT = WA_GATE-TOUT.
*    WA_INSERT-NOA = WA_GATE-NOA.
*    WA_INSERT-NOP = WA_GATE-NOP.
*    WA_INSERT-VNO = WA_GATE-VNO.
*    WA_INSERT-NOP1 = WA_GATE-NOP1.
*    WA_INSERT-REMARKS = WA_GATE-REMARKS.
    MOVE-CORRESPONDING wa_gate to  wa_insert.
    APPEND WA_INSERT TO IT_INSERT.
    CLEAR WA_INSERT.
SELECT cno FROM zgateentry INTO wa_temp
        WHERE cno wa_gate-cno.
        ENDSELECT.
if wa_temp-cno is INITIAL.
*    MOVE-CORRESPONDING WA_GATE TO WA_INSERT.
*    INSERT INTO ZGATEENTRY VALUES it_INSERT.
    insert zgateentry from table it_insert.
    ELSE.
    UPDATE ZGATEENTRY FROM WA_INSERT.
    ENDIF.
*endif.
*CLEAR wa_temp.
  IF sy-subrc 0.
    MESSAGE 'Records created succesfully' TYPE 'S'.
  ENDIF.
ENDCASE.
*ENDIF.
*CASE sy-ucomm.

*ENDCASE.







ENDMODULE.                 " INSERT_DATA_9000  INPUT
*&---------------------------------------------------------------------*
*&      Module  MODIFY_DATA_9000  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE MODIFY_DATA_9000 INPUT.
*  BREAK 10115.
*  CASE  SY-UCOMM.
*
*  ENDCASE.
ENDMODULE.                 " MODIFY_DATA_9000  INPUT

Friday, February 1, 2013

ME38_BDC

*&---------------------------------------------------------------------*
*& Report Name : ZME38_BDC_RPT
*& Transaction : ZMM_ME38
*& Description : BDC Report on ME38 to change the scheduling qty of line items
*& Date of Creation : 29/1/2013
*&---------------------------------------------------------------------*
*& Date       | Transport No |  Reason for Modification                *
*&
*&---------------------------------------------------------------------*

REPORT  zme38_bdc_rpt.

TYPES BEGIN OF ty_final,
          ebeln     TYPE ebeln,
          ebelp(5)  TYPE c,
          rowid(2)  TYPE c,
          menge(17TYPE c,
        END OF ty_final.

DATA it_final   TYPE STANDARD TABLE OF ty_final,
       it_bdcdata TYPE STANDARD TABLE OF bdcdata,
       it_error   TYPE STANDARD TABLE OF bdcmsgcoll,
       it_temp    TYPE STANDARD TABLE OF ty_final,
       it_eket    TYPE STANDARD TABLE OF ty_final,
       wa_error   TYPE                   bdcmsgcoll,
       wa_final   TYPE                   ty_final,
       wa_bdcdata TYPE                   bdcdata,
       wa_temp    TYPE ty_final,
       name_of_file LIKE ibipparms-path.

PARAMETERS p_file TYPE localfile.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

  PERFORM get_file.

START-OF-SELECTION.

  PERFORM refresh_data.

  PERFORM fetch_bdc_data.

*&---------------------------------------------------------------------*
*&      Form  BDC_DYNPRO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PROGRAM    text
*      -->DYNPRO     text
*----------------------------------------------------------------------*
FORM bdc_dynpro USING program dynpro.
  CLEAR wa_bdcdata.
  wa_bdcdata-program  program.
  wa_bdcdata-dynpro   dynpro.
  wa_bdcdata-dynbegin 'X'.
  APPEND wa_bdcdata TO it_bdcdata.
ENDFORM.                    "BDC_DYNPRO

*&---------------------------------------------------------------------*
*&      Form  BDC_FIELD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->FNAM       text
*      -->FVAL       text
*----------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
*  IF FVAL <> NODATA.
  CLEAR wa_bdcdata.
  wa_bdcdata-fnam fnam.
  wa_bdcdata-fval fval.
  APPEND wa_bdcdata TO it_bdcdata.
*  ENDIF.
ENDFORM.                    "BDC_FIELD
*&---------------------------------------------------------------------*
*&      Form  REFRESH_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM refresh_data .

  REFRESH it_final[]it_bdcdata[]it_error[],it_temp[]it_eket[].
  CLEAR wa_errorwa_finalwa_bdcdatawa_tempname_of_file.

ENDFORM.                    " REFRESH_DATA
*&---------------------------------------------------------------------*
*&      Form  GET_FILE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_file .

  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      program_name  syst-cprog
      dynpro_number syst-dynnr
      field_name    'P_FILE'
    IMPORTING
      file_name     name_of_file.

  p_file name_of_file.

ENDFORM.                    " GET_FILE
*&---------------------------------------------------------------------*
*&      Form  FETCH_BDC_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM fetch_bdc_data .

  DATA it_msg      TYPE STANDARD TABLE OF bdcmsgcoll,
         lv_count(2TYPE n,
         wa_eket     TYPE ty_final,
         lv_input    TYPE char50,
         lv_num      TYPE char2,
         wa_msg      TYPE bdcmsgcoll,
         msg(255)    TYPE c,
         lv_mode     TYPE c,
         lv_update   TYPE char1 VALUE 'S',
         lv_row(3)   TYPE n,
         lv_flg      TYPE c,
         lv_rowc     TYPE char3.

  CALL FUNCTION 'WS_UPLOAD'
   EXPORTING
*   CODEPAGE                      = ' '
     filename                      p_file
     filetype                      'DAT'
*   HEADLEN                       = ' '
*   LINE_EXIT                     = ' '
*   TRUNCLEN                      = ' '
*   USER_FORM                     = ' '
*   USER_PROG                     = ' '
*   DAT_D_FORMAT                  = ' '
* IMPORTING
*   FILELENGTH                    =
    TABLES
      data_tab                      it_final
   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
     no_authority                  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.


  REFRESH it_msg[].
  CLEAR wa_eketwa_msglv_inputlv_countmsg.

  IF it_final IS NOT INITIAL.

    it_temp it_final.
    SORT it_temp BY ebeln ebelp.
    DELETE ADJACENT DUPLICATES FROM it_temp COMPARING ebeln ebelp.

  ENDIF.
  SORT it_final BY ebeln ebelp.

  CLEAR wa_tempwa_finalwa_eket.
  LOOP AT it_temp INTO wa_temp.

    PERFORM bdc_dynpro      USING 'SAPMM06E' '0205'.
*perform bdc_field       using 'BDC_CURSOR'
*                              'RM06E-EVRTN'.
    PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '/00'.
    PERFORM bdc_field       USING 'RM06E-EVRTN'
                                  wa_temp-ebeln.

    LOOP AT it_final INTO wa_final WHERE ebeln wa_temp-ebeln AND ebelp wa_temp-ebelp.


      wa_eket wa_final.

*      AT NEW ebelp.
*        CLEAR: lv_row, lv_rowc.
*        lv_row = wa_temp-ebelp.
*        DIVIDE lv_row BY 10.
*        lv_rowc = lv_row.
*        lv_count = 01.
*
*        PERFORM bdc_dynpro      USING 'SAPMM06E' '0222'.
*
*        PERFORM bdc_field       USING 'BDC_OKCODE'
*                                      '=ET'.
*        CLEAR lv_input.
**        CONCATENATE 'RM06E-TCSELFLAG(' wa_eket-rowid ')' INTO lv_input.
**        CONCATENATE 'RM06E-TCSELFLAG(' lv_rowc ')' INTO lv_input.
*        CONCATENATE 'RM06E-TCSELFLAG(' lv_row ')' INTO lv_input.
*        PERFORM bdc_field       USING lv_input
*                                      'X'.
*        PERFORM bdc_dynpro      USING 'SAPMM06E' '1117'.
*
*      ENDAT.
************************************************************
      AT NEW ebelp.
        CLEARlv_rowlv_rowclv_flg.
        lv_row wa_temp-ebelp.
        lv_rowc lv_row.
        lv_flg 'X'.
        SHIFT lv_rowc LEFT DELETING LEADING '0'.
        PERFORM bdc_dynpro      USING 'SAPMM06E' '0222'.
        PERFORM bdc_field       USING 'BDC_CURSOR'
                                      'RM06E-EBELP'.
        PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '/00'.
        PERFORM bdc_field       USING 'RM06E-EBELP'
                                      lv_rowc.


        PERFORM bdc_dynpro      USING 'SAPMM06E' '0222'.
        PERFORM bdc_field       USING 'BDC_CURSOR'
                                      'RM06E-EVRTP(01)'.
        PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=ET'.
        PERFORM bdc_field       USING 'RM06E-EBELP'
                                      lv_rowc.
      ENDAT.


*---> Begin of insertion DEVK903470
**  Added page up logic when control comes to screen 1117 for first time
      IF lv_flg 'X'.
        CLEAR lv_flg.
        PERFORM bdc_dynpro      USING 'SAPMM06E' '1117'.
        PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=P-'.
        PERFORM bdc_dynpro      USING 'SAPMM06E' '1117'.
        PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=P-'.
      ENDIF.
*<--- End of insertion DEVK903470
      PERFORM bdc_dynpro      USING 'SAPMM06E' '1117'.

      PERFORM bdc_field       USING 'BDC_CURSOR'
                                    'EKET-MENGE(01)'.
      PERFORM bdc_field       USING 'BDC_OKCODE'
                                '/00'.

*      PERFORM bdc_field       USING 'RM06E-ETNR1'
*                                wa_eket-rowid.
*
*      PERFORM bdc_field       USING 'BDC_OKCODE'
*                                '/00'.

      CLEARlv_numlv_input.
*---> Begin of insertion DEVK903470
      lv_num wa_eket-rowid.
      SHIFT lv_num RIGHT DELETING TRAILING space.
      OVERLAY lv_num WITH '00'.
      CONCATENATE 'EKET-MENGE(' lv_num ')' INTO lv_input.
*<--- End of insertion DEVK903470
*---> Begin of deletion DEVK903470
*      CONCATENATE 'EKET-MENGE(' wa_eket-rowid ')' INTO lv_input.
*<--- End of deletion DEVK903470

      PERFORM bdc_field       USING lv_input
                                wa_eket-menge.
      AT END OF ebelp.
        PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=BACK'.
      ENDAT.
************************************************************
*---> Commented code begin
*      IF lv_count = 01.
*        PERFORM bdc_field       USING 'BDC_OKCODE'
*                                  '=BACK'.
**    perform bdc_field       using 'RM06E-ETNR1'
**                              '   1'.
*      ENDIF.
*
*      CLEAR lv_input.
**      CONCATENATE 'EKET-MENGE(' lv_count ')' INTO lv_input.
*      CONCATENATE 'EKET-MENGE(' wa_eket-rowid ')' INTO lv_input.
*      PERFORM bdc_field       USING lv_input
*                                  wa_eket-menge.
*      lv_count = lv_count + 1.
*      CLEAR : wa_eket, wa_final.
*<---- Commented end

    ENDLOOP.

    PERFORM bdc_dynpro      USING 'SAPMM06E' '0222'.
*perform bdc_field       using 'BDC_CURSOR'
*                              'RM06E-EVRTP(01)'.
    PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=BU'.

    PERFORM bdc_dynpro      USING 'SAPLSPO1' '0300'.
    PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=YES'.

    CLEARlv_modewa_temp.
    lv_mode 'N'.

    CALL TRANSACTION 'ME38' USING it_bdcdata MODE lv_mode UPDATE lv_update MESSAGES INTO it_msg.
    REFRESH it_bdcdata[].

    LOOP AT it_msg INTO wa_msg WHERE msgtyp NE 'W'.

      CALL FUNCTION 'FORMAT_MESSAGE'
       EXPORTING
         id              wa_msg-msgid
*   LANG            = '-D'
         no              wa_msg-msgnr
         v1              wa_msg-msgv1
         v2              wa_msg-msgv2
         v3              wa_msg-msgv3
         v4              wa_msg-msgv4
       IMPORTING
         msg             msg
       EXCEPTIONS
         not_found       1
         OTHERS          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.

      WRITE:/ msg.

    ENDLOOP.

    REFRESH it_msg[].

  ENDLOOP.

ENDFORM.                    " FETCH_BDC_DATA