Thursday 20 April 2017

Hierarchical ALV

On selecting the header record, corresponding item record is shown. This has been done using hierarchical ALV:
*&---------------------------------------------------------------------*
*& Report  YHIERARCHY
*&
*&---------------------------------------------------------------------*
*& HIERARCHICAL ALV
*& Help: http://sapabap-4.blogspot.in/2015/11/alv-hierarchical-report.html
*&---------------------------------------------------------------------*
*
REPORT yhierarchy.

TYPE-POOLS: slis.

TYPES: BEGIN OF gty_ekko.
        INCLUDE STRUCTURE ekko.
TYPES: expand.
TYPES: END OF gty_ekko.

DATA: gt_ekko    TYPE TABLE OF gty_ekko,
      gt_ekpo    TYPE TABLE OF ekpo,
      gt_fcat    TYPE slis_t_fieldcat_alv,
      gt_tmp     TYPE slis_t_fieldcat_alv,
      gs_fcat    TYPE slis_fieldcat_alv,
      gs_layout  TYPE slis_layout_alv,
      gs_key     TYPE slis_keyinfo_alv "FOR HIERARCHICAL ALV
      .

START-OF-SELECTION.
  SELECT * FROM ekko INTO TABLE gt_ekko UP TO 10 ROWS.

  SELECT *
  FROM ekpo
  INTO TABLE gt_ekpo
  FOR ALL ENTRIES IN gt_ekko
  WHERE ebeln = gt_ekko-ebeln
  .

  DATA lv_col TYPE i VALUE 0.

  IF gt_ekko IS NOT INITIAL.

    lv_col            = lv_col + 1.
    gs_fcat-col_pos   = lv_col.
    gs_fcat-fieldname = 'EBELN'.
    gs_fcat-tabname   = 'IT_EKKO'.
    gs_fcat-seltext_l = 'Purchase Order'.
    APPEND gs_fcat TO gt_fcat.
    CLEAR gs_fcat.

    lv_col            = lv_col + 1.
    gs_fcat-col_pos   = lv_col.
    gs_fcat-fieldname = 'BUKRS'.
    gs_fcat-tabname   = 'IT_EKKO'.
    gs_fcat-seltext_l = 'Company Code'.
    APPEND gs_fcat TO gt_fcat.
    CLEAR gs_fcat.

    lv_col            = lv_col + 1.
    gs_fcat-col_pos   = lv_col.
    gs_fcat-fieldname = 'AEDAT'.
    gs_fcat-tabname   = 'IT_EKKO'.
    gs_fcat-seltext_l = 'Creation Date'.
    APPEND gs_fcat TO gt_fcat.
    CLEAR gs_fcat.

    lv_col            = lv_col + 1.
    gs_fcat-col_pos   = lv_col.
    gs_fcat-fieldname = 'ERNAM'.
    gs_fcat-tabname   = 'IT_EKKO'.
    gs_fcat-seltext_l = 'Created By'.
    APPEND gs_fcat TO gt_fcat.
    CLEAR gs_fcat.

    lv_col            = lv_col + 1.
    gs_fcat-col_pos   = lv_col.
    gs_fcat-fieldname = 'LIFNR'.
    gs_fcat-tabname   = 'IT_EKKO'.
    gs_fcat-seltext_l = 'Vendor'.
    APPEND gs_fcat TO gt_fcat.
    CLEAR gs_fcat.

    lv_col            = lv_col + 1.
    gs_fcat-col_pos   = lv_col.
    gs_fcat-fieldname = 'BEDAT'.
    gs_fcat-tabname   = 'IT_EKKO'.
    gs_fcat-seltext_l = 'Purchasing Document Date'.
    APPEND gs_fcat TO gt_fcat.
    CLEAR gs_fcat.
  ENDIF.

  IF gt_ekpo IS NOT INITIAL.

    lv_col            = lv_col + 1.
    gs_fcat-col_pos   = lv_col.
    gs_fcat-fieldname = 'EBELN'.
    gs_fcat-tabname   = 'IT_EKPO'.
    gs_fcat-seltext_l = 'Purchase Order'.
    APPEND gs_fcat TO gt_fcat.
    CLEAR gs_fcat.

    lv_col            = lv_col + 1.
    gs_fcat-col_pos   = lv_col.
    gs_fcat-fieldname = 'EBELP'.
    gs_fcat-tabname   = 'IT_EKPO'.
    gs_fcat-seltext_l = 'Item'.
    APPEND gs_fcat TO gt_fcat.
    CLEAR gs_fcat.

    lv_col            = lv_col + 1.
    gs_fcat-col_pos   = lv_col.
    gs_fcat-fieldname = 'MATKL'.
    gs_fcat-tabname   = 'IT_EKPO'.
    gs_fcat-seltext_l = 'Material Group'.
    APPEND gs_fcat TO gt_fcat.
    CLEAR gs_fcat.

    lv_col            = lv_col + 1.
    gs_fcat-col_pos   = lv_col.
    gs_fcat-fieldname = 'AEDAT'.
    gs_fcat-tabname   = 'IT_EKPO'.
    gs_fcat-seltext_l = 'Creation Date'.
    APPEND gs_fcat TO gt_fcat.
    CLEAR gs_fcat.

    lv_col            = lv_col + 1.
    gs_fcat-col_pos   = lv_col.
    gs_fcat-fieldname = 'MENGE'.
    gs_fcat-tabname   = 'IT_EKPO'.
    gs_fcat-seltext_l = 'PO Quantity'.
    APPEND gs_fcat TO gt_fcat.
    CLEAR gs_fcat.

    lv_col            = lv_col + 1.
    gs_fcat-col_pos   = lv_col.
    gs_fcat-fieldname = 'MEINS'.
    gs_fcat-tabname   = 'IT_EKPO'.
    gs_fcat-seltext_l = 'Unit'.
    APPEND gs_fcat TO gt_fcat.
    CLEAR gs_fcat.

    lv_col            = lv_col + 1.
    gs_fcat-col_pos   = lv_col.
    gs_fcat-fieldname = 'NETPR'.
    gs_fcat-tabname   = 'IT_EKPO'.
    gs_fcat-seltext_l = 'Net Price'.
    APPEND gs_fcat TO gt_fcat.
    CLEAR gs_fcat.

    lv_col            = lv_col + 1.
    gs_fcat-col_pos   = lv_col.
    gs_fcat-fieldname = 'PEINH'.
    gs_fcat-tabname   = 'IT_EKPO'.
    gs_fcat-seltext_l = 'Price Unit'.
    APPEND gs_fcat TO gt_fcat.
    CLEAR gs_fcat.
  ENDIF.

  CONSTANTS: lc_expand TYPE slis_fieldname VALUE 'EXPAND'.

  gs_layout-zebra             = 'X'. "Zebra looks
  gs_layout-colwidth_optimize = 'X'. "Column width optimized
  gs_layout-expand_fieldname  = 'EXPAND'. "Expand operation
  gs_layout-window_titlebar   = 'Hierarchical PO Header & Item Display'.

  gs_key-header01 = 'EBELN'. "Purchase Order number
  gs_key-item01   = 'EBELN'. "is the key for header & item table

  CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      is_layout          = gs_layout
      it_fieldcat        = gt_fcat
      i_tabname_header   = 'IT_EKKO'
      i_tabname_item     = 'IT_EKPO'
      is_keyinfo         = gs_key
    TABLES
      t_outtab_header    = gt_ekko
      t_outtab_item      = gt_ekpo
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.
  IF sy-subrc <> 0.
*   Implement suitable error handling here
  ENDIF.

Wednesday 19 April 2017

Change the Description of Released TR

Once a TR has been Released, it cannot be edited in normal way. The released TR cannot be deleted also. Only the Basis team can delete it from OS level. But there are some ways in which you can change the description of the released TR, although it is not recommend.

>> One way of doing this is by changing the entry of table E07T through debugging technique. Click Here

>> The other way is by using the FM:

1) Got o FM 'TRINT_CHECK_REQUEST_CHANGEABLE'
At the line number 111 IF is_request_header-trstatus CA sctsc_states_released, put the break point.
2) Double click the released request which you want to change the description.
3) your EDIT option will be disabled. Just type '/h' in the tcpode area and press enter, your debugging will switched on.
4) Then Press ENTER again, it will open the debugger screen, in the variable change the SY_UCOMM field to 'WB_DISP_EDIT_TOGGLE' . Then press F8.
5) your control will be in the FM 'TRINT_CHECK_REQUEST_CHANGEABLE' line number 111. Try to skip this IF condition by using debug technique.
6) Now you can change the description.Now you cant save this as Save button is disabled.Just press back button, you will get the message for save the changes.Just save it.

Tuesday 18 April 2017

Changing The Entry of Standard Table

Although this is not recommended, but by the following way you can change the entry of any standard table.
  1. Open the table in SE16 and select the record that needs to be changed.
  2. Click on the Display icon or press F7.
  3. Type /H in the tcode area to switch on the Debug Mode.
  4. Again press 'Enter' to turn the debugger on.
  5. Press F7. You will see the code something like this:
    if code = 'show'
  6. Change the value of Code to 'EDIT' and press F8.
    and in case of deleting any entry change the value of Code to 'DELE' and press F8.
  7. Press Save after the operation is done.

Monday 17 April 2017

Excel output with Coloured Cell

The following program has been copied from here. Only a small change has been made for the cell colour so that the background remains transparent and only the heading have a cell colour. The followings are the valid colour indexes:



*&---------------------------------------------------------------------*
*& Report  ZEXCEL
*&
*&---------------------------------------------------------------------*
*&
*& EXCEL OUTPUT WITH CELL COLOURING
*&---------------------------------------------------------------------*

REPORT zexcel.

INCLUDE ole2incl.
FIELD-SYMBOLS: <val> TYPE any.
DATA: row_cnt TYPE i.
TYPES: BEGIN OF t_excel,
  material_info(20),
  sugg_price(20),
  cost(20),
  comments(100),
END OF t_excel.
DATA: r_excel TYPE t_excel.
DATA: i_excel TYPE TABLE OF t_excel WITH HEADER LINE.
CONSTANTS:
  xlcenter TYPE i VALUE '-4108',
  xlbottom TYPE i VALUE '-4107',
  xlleft TYPE i VALUE '-4131',
  xlright TYPE i VALUE '-4152'.
CONSTANTS:
  xlcontinuous TYPE i VALUE '1',
  xlinsidevertical TYPE i VALUE '11',
  xlthin TYPE i VALUE '2',
  xllandscape TYPE i VALUE '2',
  xlportrait TYPE i VALUE '1',
  xlletter TYPE i VALUE '1',
  xllegal TYPE i VALUE '5',
  xlthick TYPE i VALUE '4',
  xlnone TYPE i VALUE '-4142',
  xlautomatic TYPE i VALUE '-4105'.
DATA:
  hexcel TYPE ole2_object, " Excel object
  hworkbooks TYPE ole2_object, " list of workbooks
  hworkbook TYPE ole2_object, " workbook
  hsheet TYPE ole2_object, " worksheet object
  hrange TYPE ole2_object, " range object
  hrange2 TYPE ole2_object, " range object
  hborders TYPE ole2_object, " Border object
  hinterior TYPE ole2_object, " interior object - for coloring
  hcolumn TYPE ole2_object, "column
  hcell TYPE ole2_object, " cell
  hfont TYPE ole2_object, " font
  hselected TYPE ole2_object, " range object
  hpicture TYPE ole2_object, "picture object
  hlogo TYPE ole2_object. "Logo object


SELECTION-SCREEN BEGIN OF BLOCK b1.
SELECTION-SCREEN SKIP 1.
PARAMETER: wraptext AS CHECKBOX.
SELECTION-SCREEN END OF BLOCK b1.


row_cnt = 1.
PERFORM build_dummy_vals.
PERFORM start_excel.
PERFORM build_header_line USING row_cnt.
PERFORM pass_records USING row_cnt.
PERFORM release_excel.
*&---------------------------------------------------------------------*
*&      Form  Build_Dummy_Vals
*&---------------------------------------------------------------------*
FORM build_dummy_vals .
  DATA: matnum(5) TYPE n.
  DATA: baseprice(3) TYPE n.
  DO 5 TIMES.
    matnum = matnum + 50.
    CLEAR r_excel.
    CONCATENATE 'Material ' matnum INTO r_excel-material_info
    SEPARATED BY space.
    r_excel-sugg_price = baseprice * matnum.
    r_excel-cost = ( baseprice * matnum ) / 2.
    CONCATENATE 'Comments for Material ' matnum INTO r_excel-comments
    SEPARATED BY space.
    APPEND r_excel TO i_excel.
  ENDDO.
ENDFORM. " Build_Dummy_Vals
*&---------------------------------------------------------------------*
*&      Form  Start_Excel
*&---------------------------------------------------------------------*
FORM start_excel.
  CREATE OBJECT hexcel 'EXCEL.APPLICATION'.
  PERFORM err_hdl.

*get list of workbooks, initially empty
  CALL METHOD OF
      hexcel
      'Workbooks' = hworkbooks.
  PERFORM err_hdl.

*add a new workbook
  CALL METHOD OF
      hworkbooks
      'Add'      = hworkbook.
  PERFORM err_hdl.

*Get Worksheet object.
  GET PROPERTY OF hworkbook 'ActiveSheet' = hsheet.
ENDFORM.                    "Start_Excel
*&---------------------------------------------------------------------*
*&      Form  Build_Header_Line
*&---------------------------------------------------------------------*
FORM build_header_line USING p_row_cnt.
  DATA: l_range(30).
  DATA: row_start(10).

  PERFORM fill_the_cell USING p_row_cnt 1 1 'Material'.
  PERFORM fill_the_cell USING p_row_cnt 2 1 'Suggested Price'.
  PERFORM fill_the_cell USING p_row_cnt 3 1 'Cost'.
  PERFORM fill_the_cell USING p_row_cnt 4 1 'Comments'.
  PERFORM format_column USING 1 15 xlcenter ' ' xlcenter 0.
  PERFORM format_column USING 2 10 xlcenter ' ' xlcenter 1.
  PERFORM format_column USING 3 35 xlcenter ' ' xlcenter 0.
  IF wraptext = 'X'.
    PERFORM format_column USING 4 35 xlleft ' ' xlcenter 1.
  ELSE.
    PERFORM format_column USING 4 100 xlleft ' ' xlcenter 0.
  ENDIF.

*Build the range object.
  row_start = p_row_cnt.
  CONCATENATE 'A' row_start ':D' row_start INTO l_range.
  CONDENSE l_range NO-GAPS.

*Set row color to yellow.
  CALL METHOD OF
      hexcel
      'RANGE' = hrange
    EXPORTING
      #1      = l_range.
  CALL METHOD OF
      hrange
      'Interior' = hinterior.
  SET PROPERTY OF hinterior 'ColorIndex' = 15. "6. "yellow
  p_row_cnt = p_row_cnt + 1.
ENDFORM. " Build_Header_Line
*&---------------------------------------------------------------------*
*&      Form  Format_Column
*&---------------------------------------------------------------------*
FORM format_column USING  p_colnum
                          p_colwidth
                          p_colhalign
                          p_colformat
                          p_colvalign
                          p_wraptext.
  CALL METHOD OF
      hexcel
      'COLUMNS' = hcolumn
    EXPORTING
      #1        = p_colnum. "column number
  SET PROPERTY OF hcolumn 'HorizontalAlignment' = p_colhalign.
  SET PROPERTY OF hcolumn 'VerticalAlignment' = p_colvalign.
  SET PROPERTY OF hcolumn 'ColumnWidth' = p_colwidth.
  SET PROPERTY OF hcolumn 'WrapText' = p_wraptext.
ENDFORM. " Format_Column
*&---------------------------------------------------------------------*
*&      Form  Pass_Records
*&---------------------------------------------------------------------*
FORM pass_records USING p_row_cnt.
  DATA: col_cnt TYPE i.
  DATA: l_range(30).
  DATA: row_start(10).
  col_cnt = 1.
*Pass the internal table values to the spreadsheet.
  LOOP AT i_excel INTO r_excel.
    DO 4 TIMES.
      ASSIGN COMPONENT sy-index OF STRUCTURE r_excel TO <val>.
      PERFORM fill_the_cell USING p_row_cnt col_cnt 0 <val>.
      col_cnt = col_cnt + 1. "increment column
    ENDDO.

*Build the range object.
    row_start = p_row_cnt.
    CONCATENATE 'A' row_start ':D' row_start INTO l_range.
    CONDENSE l_range NO-GAPS.

*Set row color to yellow.
    CALL METHOD OF
        hexcel
        'RANGE' = hrange
      EXPORTING
        #1      = l_range.
    CALL METHOD OF
        hrange
        'Interior' = hinterior.
*    SET PROPERTY OF hinterior 'ColorIndex' = 2. "7. "yellow
    p_row_cnt = p_row_cnt + 1. "increment row
    col_cnt = 1. "reset column to A (ie. col 1)
  ENDLOOP.
ENDFORM.                    "Pass_Records
*&---------------------------------------------------------------------*
*&      Form  Release_Excel
*&---------------------------------------------------------------------*
FORM release_excel .
  SET PROPERTY OF hexcel 'Visible' = 1.
  FREE OBJECT hexcel.
  PERFORM err_hdl.
ENDFORM. " Release_Excel
*---------------------------------------------------------------------
*FORM Fill_The_Cell
*---------------------------------------------------------------------
*
*Sets cell at coordinates i,j to value val boldtype bold *
*
*BOLD --> 1 = true, set bold ON 0 = false, set bold OFF
*---------------------------------------------------------------------
FORM fill_the_cell USING i j bold thevalue.
  CALL METHOD OF
      hexcel
      'Cells' = hcell
    EXPORTING
      #1      = i
      #2      = j.
  PERFORM err_hdl.
  SET PROPERTY OF hcell 'Value' = thevalue.
  PERFORM err_hdl.
  GET PROPERTY OF hcell 'Font' = hfont.
  PERFORM err_hdl.
  SET PROPERTY OF hfont 'Bold' = bold.
  PERFORM err_hdl.
ENDFORM.                    "Fill_The_Cell
*&---------------------------------------------------------------------*
*&      Form  ERR_HDL
*&---------------------------------------------------------------------*
FORM err_hdl.
  IF sy-subrc <> 0.
    MESSAGE i000(zz) WITH 'OLE Automation error: ' sy-subrc.
    EXIT.
  ENDIF.
ENDFORM. " ERR_HDL

Using Memory ID To Transfer Value

With the help of MEMORY ID we can transfer the value from one report to another. Following is a simple example to show how this works:

Source System:
*&---------------------------------------------------------------------*
*& Report  ZSOURCE
*&
*&---------------------------------------------------------------------*
*&
*& SOURCE SYSTEM: LISTING THE PLANT AGAINST A PARTICULAR EBELN
*&---------------------------------------------------------------------*

REPORT zsource.
TABLES: ekko, ekpo.

SELECT-OPTIONS: s_ebeln FOR ekko-ebeln.

START-OF-SELECTION.
  DATA: lt_ekko TYPE TABLE OF ekko,
        lt_res TYPE TABLE OF ekpo,
        ls_res TYPE ekpo
        .
  CLEAR: ls_res.
  REFRESH: lt_ekko, lt_res.
  FREE MEMORY ID 'TEMP'.

  SUBMIT zdest WITH s_ebeln IN s_ebeln AND RETURN.

  IMPORT lt_ekpo TO lt_res FROM MEMORY ID 'TEMP'.

  LOOP AT lt_res INTO ls_res.
    AT NEW ebeln.
      FORMAT INTENSIFIED ON.
      WRITE: / 'Plant: ',ls_res-ebeln.
      FORMAT INTENSIFIED OFF.
    ENDAT.

    WRITE: / ls_res-werks.
  ENDLOOP.

Destination System:
*&---------------------------------------------------------------------*
*& Report  ZDEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT zdest.
TABLES: ekko, ekpo.

SELECTION-SCREEN: BEGIN OF BLOCK b1.
SELECT-OPTIONS: s_ebeln FOR ekko-ebeln.
SELECTION-SCREEN: END OF BLOCK b1.

START-OF-SELECTION.

  DATA: lt_ekpo TYPE TABLE OF ekpo.

  SELECT *
  FROM ekpo
  INTO TABLE lt_ekpo
  WHERE ebeln IN s_ebeln.

  EXPORT lt_ekpo TO MEMORY ID 'TEMP'.

Thursday 13 April 2017

Background Job Processing

Following is a simple example to show how to perform background job processing:

*&---------------------------------------------------------------------*
*& Report  ZTEST
*&
*&---------------------------------------------------------------------*
*& Background Job Processing
*&---------------------------------------------------------------------*

REPORT ztest.

SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME.
PARAMETERS: p_for RADIOBUTTON GROUP gr1 DEFAULT 'X' USER-COMMAND f1,
            p_back RADIOBUTTON GROUP gr1.
PARAMETERS: p_name TYPE char10.
SELECTION-SCREEN: END OF BLOCK b1.

START-OF-SELECTION.
  IF p_for = 'X'.
    PERFORM disp USING p_name.
  ELSE.
    PERFORM batch_job USING p_name.
  ENDIF.

*&---------------------------------------------------------------------*
*&      Form  batch_job
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM batch_job USING p_name TYPE char10.
  DATA: lv_jobcount TYPE tbtcjob-jobcount,
        lv_jobname  TYPE tbtcjob-jobname VALUE 'ZTEST',
        lv_text     TYPE char200.

  DATA: lv_timestamp TYPE timestampl ##needed,
        lv_time      TYPE char50.

  CONSTANTS: lc_jobclass TYPE tbtcjob-jobclass VALUE 'A',
             lc_sep2     TYPE char3            VALUE  '_'.


  GET TIME STAMP FIELD lv_timestamp.
  lv_time = sy-uzeit.

  CONCATENATE lv_jobname lc_sep2 sy-datum lc_sep2 lv_time
         INTO lv_jobname .


* JOB OPEN
  CALL FUNCTION 'JOB_OPEN'
    EXPORTING
      jobname          = lv_jobname
      jobclass         = lc_jobclass
    IMPORTING
      jobcount         = lv_jobcount
    EXCEPTIONS
      cant_create_job  = 1
      invalid_job_data = 2
      jobname_missing  = 3
      OTHERS           = 4.
  IF sy-subrc <> 0.
    MESSAGE 'Error' TYPE 'S' DISPLAY LIKE 'E'.
    RETURN.
  ELSE.
*    CALLING THE SAME PROGRAM BY PASSING THE INPUT PARAMTERS
    SUBMIT ztest
    WITH p_name EQ p_name
    VIA JOB lv_jobname
    NUMBER lv_jobcount
    AND RETURN.

    IF sy-subrc = 0.
* JOB CLOSE
      CALL FUNCTION 'JOB_CLOSE'
        EXPORTING
          jobcount             = lv_jobcount
          jobname              = lv_jobname
          strtimmed            = 'X'
        EXCEPTIONS
          cant_start_immediate = 1
          invalid_startdate    = 2
          jobname_missing      = 3
          job_close_failed     = 4
          job_nosteps          = 5
          job_notex            = 6
          lock_failed          = 7
          invalid_target       = 8
          OTHERS               = 9.
      IF sy-subrc = 0.
        MESSAGE 'Success' TYPE 'S'.
      ELSE.
        MESSAGE 'Error' TYPE 'S' DISPLAY LIKE 'E'.
        RETURN.
      ENDIF.
    ENDIF.
  ENDIF.
ENDFORM.                    "batch_job

*&---------------------------------------------------------------------*
*&      Form  DISP
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM disp USING p_name TYPE char10.
  WRITE: 'Hello', p_name.
ENDFORM.                    " DISP

NOTE: You can view the background job in SM37. Click on execute and then you can view the spool.

Report to find CDS view of Standard Table

A small change has been made to the original program ( SAP YARD Article ) so that it can also display the common CDS used by multiple table...