Friday, November 11, 2011

Shortcuts in ABAP

Ctrl + Shift + V (Extended Paste): In new abap editor, many times you come up a scenario to copy paste some line of codes from another program. You have to copy a line   
     from Program2 first, then paste into Program1. For second code, again you have to copy and then paste. You can use the Ctrl + Shift + V    
    functionality to paste the recently copied lines. So, no need to switch windows unnecessarily. It's really a time saving stuff.
Ctrl + Alt + Num+ ( plus sign in Num keys)  :  to increase(zoom-in) the text size.
Ctrl + Alt + Num- ( minus sign in Num keys) :  to decrease(zoom-out) the text size.
Ctrl + Alt + /  :  To restore text size to normal.
Ctrl + U         :  The selected text converted into uppercase.
Ctrl + L         :  The selected text converted into lowercase.
Ctrl + J          :  The selected text will become capitalize.
Ctrl + K        :  The case of selected text will be swapped.
Ctrl + /          :  Jumps to the OK code field

Monday, November 7, 2011

Change Table Entries by means of debugging

                If you are not authorised to change table entries or the table has been create without table maintenance allowed (all SAP tables), you can still change entries via data browser (SE16) by means of debugging. The process is as follows:
1. Display record entries for desired table via SE16 or SE11.
2. Select record which requires updating by highlighting it and pressing the display icon (glasses, F7).
3. Type /h in command box and press enter.
4. Now press enter again and you should enter debugging mode
5. Change the value of code from ‘SHOW’ to ‘EDIT’, remember to press the ‘change field contents’
    icon (pencil) in-order to store new value.
6. Press F8 (execute) to leave debug.
7. You should now have change access to the selected record.
8. Hit ‘SAVE’ to save any changes.
9. Remember there is a reason why you can’t do this without debugging it, one reason being that it could
    introduce inconsistencies in the table data.

 

Sample Code for Interactive ALV using OOPS

  *&---------------------------------------------------------------------*
*& Report  ZALV5
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zalv5 NO STANDARD PAGE HEADING.

TABLESvbak,
        vbap.

TYPESBEGIN OF ty_vbak,
       vbeln TYPE vbeln_va,
       erdat TYPE erdat,
       ernam TYPE ernam,
       netwr TYPE netwr_ak,
       END OF ty_vbak,

       BEGIN OF ty_vbap,
       vbeln TYPE vbeln_va,
       posnr TYPE posnr_va,
       matnr TYPE matnr,
       matkl TYPE matkl,
       END OF ty_vbap.

DATAit_vbak TYPE STANDARD TABLE OF ty_vbak,
      wa_vbak TYPE ty_vbak,
      it_vbap TYPE STANDARD TABLE OF ty_vbap,
      wa_vbap TYPE ty_vbap.

DATAit_fcat        TYPE lvc_t_fcat,
      it_fcat2       TYPE lvc_t_fcat,
      wa_fcat        TYPE lvc_s_fcat,
      wa_layo        TYPE lvc_s_layo.

DATAgrid       TYPE REF TO cl_gui_alv_grid,
      grid2      TYPE REF TO cl_gui_alv_grid,
      cust_cont  TYPE REF TO cl_gui_custom_container,
      cust_cont2 TYPE REF TO cl_gui_custom_container.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
  SELECT-OPTIONSs_vbeln for vbak-vbeln.
SELECTION-SCREEN END OF BLOCK b1.

CLASS cl_interactive DEFINITION.
  PUBLIC SECTION.
  METHODShandle_double_click
           FOR EVENT double_click of cl_gui_alv_grid IMPORTING e_row.
ENDCLASS.

CLASS cl_interactive IMPLEMENTATION.
  METHOD handle_double_click.
    READ TABLE it_vbak INDEX e_row-index into wa_vbak.
    PERFORM getvbap.
    CALL SCREEN 104.
  ENDMETHOD.
ENDCLASS.

DATAevent_receiver TYPE REF TO cl_interactive.
INITIALIZATION.

START-OF-SELECTION.
CALL SCREEN 103.
*&---------------------------------------------------------------------*
*&      Module  STATUS_0103  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
module STATUS_0103 output.
  SET PF-STATUS 'STATUS_VBAK'.
  SET TITLEBAR 'TITLE1'.

  IF cust_cont IS INITIAL.
    CREATE OBJECT cust_cont
      EXPORTING
        container_name              'CONTAINER1'.

    CREATE OBJECT grid
      EXPORTING
        i_parent          cust_cont.

    PERFORM getvbak.
    PERFORM layout.
    PERFORM fieldcatalog.
    PERFORM display_vbak.
  ELSE.
    CALL METHOD grid->refresh_table_display.
  ENDIF.

endmodule.                 " STATUS_0103  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0103  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
module USER_COMMAND_0103 input.
  CASE sy-ucomm.
    WHEN 'BACK'.
      LEAVE TO SCREEN 0.
    WHEN 'EXIT'.
      LEAVE PROGRAM.
  ENDCASE.
endmodule.                 " USER_COMMAND_0103  INPUT
*&---------------------------------------------------------------------*
*&      Form  GETVBAK
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form GETVBAK .
  SELECT vbeln
         erdat
         ernam
         netwr FROM vbak
         INTO CORRESPONDING FIELDS OF TABLE it_vbak
         WHERE vbeln in s_vbeln.
endform.                    " GETVBAK
*&---------------------------------------------------------------------*
*&      Form  GETVBAP
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form GETVBAP .
  SELECT *
          FROM vbap
          INTO CORRESPONDING FIELDS OF TABLE it_vbap
          WHERE vbeln wa_vbak-vbeln.
endform.                    " GETVBAP
*&---------------------------------------------------------------------*
*&      Form  LAYOUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form LAYOUT .
  wa_layo-grid_title      'Sales Header Data'.
  wa_layo-cwidth_opt 'X'.
  wa_layo-zebra      'X'.
endform.                    " LAYOUT
*&---------------------------------------------------------------------*
*&      Form  FIELDCATALOG
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form FIELDCATALOG .
  REFRESH it_fcat.
  CLEAR   wa_fcat.

  wa_fcat-fieldname 'VBELN'.
  wa_fcat-col_pos   1.
  wa_fcat-outputlen 15.
  wa_fcat-coltext      'Sales Document'.
  APPEND wa_fcat to it_fcat.

  wa_fcat-fieldname 'ERDAT'.
  wa_fcat-col_pos   2.
  wa_fcat-outputlen 15.
  wa_fcat-coltext      'Date'.
  append wa_fcat to it_fcat.

  wa_fcat-fieldname 'ERNAM'.
  wa_fcat-col_pos   3.
  wa_fcat-outputlen 15.
  wa_fcat-coltext      'Name'.
  append wa_fcat to it_fcat.

  wa_fcat-fieldname 'NETWR'.
  wa_fcat-col_pos   4.
  wa_fcat-outputlen 15.
  wa_fcat-coltext      'Net Value'.
  append wa_fcat to it_fcat.

endform.                    " FIELDCATALOG
*&---------------------------------------------------------------------*
*&      Form  DISPLAY_VBAK
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form DISPLAY_VBAK .
  CALL METHOD grid->set_table_for_first_display
    EXPORTING
      is_layout                     wa_layo
    CHANGING
      it_outtab                     it_vbak
      it_fieldcatalog               it_fcat.

  CREATE OBJECT event_receiver.
  SET HANDLER event_receiver->handle_double_click for grid.

endform.                    " DISPLAY_VBAK
*&---------------------------------------------------------------------*
*&      Module  STATUS_0104  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
module STATUS_0104 output.
  SET PF-STATUS 'STATUS_VBAP'.
  SET TITLEBAR 'TITLE2'.

  IF cust_cont2 IS INITIAL.
    CREATE OBJECT cust_cont2
      EXPORTING
        container_name              'CONTAINER2'.

    CREATE OBJECT grid2
      EXPORTING
        i_parent          cust_cont2.

    PERFORM fieldcatalog_2.
    PERFORM layout_2.
    PERFORM display_2.
  ELSE.
    CALL METHOD grid2->refresh_table_display.

  ENDIF.
endmodule.                 " STATUS_0104  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0104  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
module USER_COMMAND_0104 input.
  CASE sy-ucomm.
    WHEN 'BACK'.
      LEAVE TO SCREEN 103.
    WHEN 'EXIT'.
      LEAVE PROGRAM.
    ENDCASE.
endmodule.                 " USER_COMMAND_0104  INPUT
*&---------------------------------------------------------------------*
*&      Form  FIELDCATALOG_2
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form FIELDCATALOG_2 .
  REFRESH it_fcat2.
  CLEAR   wa_fcat.

  wa_fcat-fieldname 'VBELN'.
  wa_fcat-col_pos   1.
  wa_fcat-outputlen 15.
  wa_fcat-coltext      'Sales Document'.
  APPEND wa_fcat to it_fcat2.

  wa_fcat-fieldname 'POSNR'.
  wa_fcat-col_pos   2.
  wa_fcat-outputlen 15.
  wa_fcat-coltext      'Sales Doc. Item'.
  append wa_fcat to it_fcat2.

  wa_fcat-fieldname 'MATNR'.
  wa_fcat-col_pos   3.
  wa_fcat-outputlen 15.
  wa_fcat-coltext      'Material No.'.
  append wa_fcat to it_fcat2.

  wa_fcat-fieldname 'MATKL'.
  wa_fcat-col_pos   4.
  wa_fcat-outputlen 15.
  wa_fcat-coltext      'Material Group'.
  append wa_fcat to it_fcat2.
endform.                    " FIELDCATALOG_2
*&---------------------------------------------------------------------*
*&      Form  LAYOUT_2
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form LAYOUT_2 .
  wa_layo-grid_title      'Sales Item Data'.
  wa_layo-cwidth_opt 'X'.
  wa_layo-zebra      'X'.
endform.                    " LAYOUT_2
*&---------------------------------------------------------------------*
*&      Form  DISPLAY_2
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form DISPLAY_2 .
  CALL METHOD grid2->set_table_for_first_display
    EXPORTING
      is_layout                     wa_layo
    CHANGING
      it_outtab                     it_vbap
      it_fieldcatalog               it_fcat2.

endform.                    " DISPLAY_2

Output:
After providing the input...

Double clicking Sales Document 179... we get the Sales Item Information



A simple ALV Report using OOPS

  *&---------------------------------------------------------------------*
*& Report  ZALV4
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

report zalv4.

tablesmara.

typesbegin of ty_mara,
       matnr type matnr,
       mtart type mtart,
       mbrsh type mbrsh,
       matkl type matkl,
       end of ty_mara.

datait_mara type standard table of ty_mara,
      wa_mara type ty_mara,
      it_fcat type lvc_t_fcat,
      wa_fcat type lvc_s_fcat,
      wa_layo type lvc_s_layo.

datacust_cont type ref to cl_gui_custom_container,
      grid      type ref to cl_gui_alv_grid.

*FIELD-SYMBOLS: <wa_fcat> TYPE lvc_s_fcat.

select-optionss_matnr for mara-matnr.

initialization.

start-of-selection.

  call screen 102.
*&---------------------------------------------------------------------*
*&      Module  STATUS_0102  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
module status_0102 output.

  set pf-status 'STATUS1'.
  set titlebar 'MATERIALS'.

  if cust_cont is initial.

    create object cust_cont
      exporting
        container_name 'CONTAINER'.

    create object grid
      exporting
        i_parent cust_cont.

    perform getmara.
    if it_mara is not initial.
      perform fieldcatalog.

      perform layout.
      perform display.
    endif.

  endif.

endmodule.                 " STATUS_0102  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0102  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
module user_command_0102 input.
  case sy-ucomm.
    when 'BACK' or 'EXIT'.
      leave program.
  endcase.
endmodule.                 " USER_COMMAND_0102  INPUT
*&---------------------------------------------------------------------*
*&      Form  FIELDCATALOG
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form fieldcatalog .
  refresh it_fcat.
  clear wa_fcat.

  wa_fcat-fieldname 'MATNR'.
  wa_fcat-col_pos   1.
  wa_fcat-coltext   'Material No.'.
  wa_fcat-outputlen 15.
  append wa_fcat to it_fcat.

  wa_fcat-fieldname 'MTART'.
  wa_fcat-col_pos   2.
  wa_fcat-coltext   'Material Type'.
  wa_fcat-outputlen 15.
  append wa_fcat to it_fcat.

  wa_fcat-fieldname 'MBRSH'.
  wa_fcat-col_pos   3.
  wa_fcat-coltext   'Industry Sector'.
  wa_fcat-outputlen 15.
  append wa_fcat to it_fcat.

  wa_fcat-fieldname 'MATKL'.
  wa_fcat-col_pos   4.
  wa_fcat-coltext   'Material Text'.
  wa_fcat-outputlen 15.
  append wa_fcat to it_fcat.

endform.                    " FIELDCATALOG
*&---------------------------------------------------------------------*
*&      Form  LAYOUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form layout .
  wa_layo-cwidth_opt 'X'.
  wa_layo-zebra      'X'.
  wa_layo-grid_title 'Material Data'.
endform.                    " LAYOUT
*&---------------------------------------------------------------------*
*&      Form  DISPLAY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form display.

  call method grid->set_table_for_first_display
    exporting
      is_layout       wa_layo
    changing
      it_outtab       it_mara
      it_fieldcatalog it_fcat.

endform.                    " DISPLAY
*&---------------------------------------------------------------------*
*&      Form  GETMARA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form getmara .
  select matnr
       mtart
       mbrsh
       matkl from mara
       into corresponding fields of table it_mara
       where matnr in s_matnr.
endform.                    " GETMARA

Saturday, November 5, 2011

Sample Program to get first day and last day of a month



REPORT  ZDATE01.
DATA: v_date   TYPE sy-datum,
      v_last   TYPE sy-datum,
      v_first  TYPE sy-datum.

v_date = sy-datum.
CONCATENATE v_date+0(4) v_date+4(2) '01' into v_first.
write:/ 'First day of the month : ', v_first.

IF v_date+6(2) > 15.
  v_date = v_date + 20.
ELSE.
  v_date = v_date + 30.
ENDIF.

CONCATENATE v_date+0(4) v_date+4(2) '01' into v_last.
v_last = v_last - 1.
write:/ 'Last day of the month : ', v_last.