Sunday, June 23, 2013

example of ABAP FIELD SYMBOLS


REPORT ZFIELDSYMBLOS.

DATABEGIN OF t_vbak OCCURS 0,
         vbeln 
TYPE vbak-vbeln,
         netwr 
TYPE vbak-netwr,
      
END OF t_vbak.

FIELD-SYMBOLS: <FS> LIKE LINE OF t_vbak.

      
SELECT
        vbeln
        netwr
        
FROM vbak
        
into CORRESPONDING FIELDS OF TABLE t_vbak
        
WHERE erdat = '20100522'.   

LOOP AT t_vbak ASSIGNING <FS>.
  
WRITE: / <FS>-vbeln, <FS>-netwr.
ENDLOOP.

What is ABAP Field Symbols.

One of the most useful of Field Symbols in ABAP is Dynamic assign statement, meaning if you don't know the name of field to assign to the field symbols during writing your program, you can use this :


REPORT Zfieldsymbols. 

PERFORM form2.
FORM form2.
  DATA name(20) TYPE c VALUE 'Print Field Symbols'.
FIELD-SYMBOLS <fs> TYPE ANY.
  ASSIGN name TO <fs>.
    WRITE / <fs>.

ENDFORM.

The output looks something like this: Print Field Symbols

Monday, June 17, 2013

How to create dropdown list in selection screen?

Dropdown list is a user interface element which displays a list of values from which a user can select one value. Follow the below steps to create a dropdown list in SAP ABAP selection screen.
  • First build the list in the INITIALIZATION event.
  • Capture the value selected by the user from the list in the AT SELECTION-SCREEN event.
  • Use the selected value for further processing.
TYPE-POOLS: vrm.

DATA: gt_list     TYPE vrm_values.
DATA: gwa_list    TYPE vrm_value.
DATA: gt_values   TYPE TABLE OF dynpread,
      gwa_values  TYPE dynpread.

DATA: gv_selected_value(10) TYPE c.
*--------------------------------------------------------------*
*Selection-Screen
*--------------------------------------------------------------*
PARAMETERS: list TYPE c AS LISTBOX VISIBLE LENGTH 20.
*--------------------------------------------------------------*
*At Selection Screen
*--------------------------------------------------------------*
AT SELECTION-SCREEN ON list.
  CLEAR: gwa_values, gt_values.
  REFRESH gt_values.
  gwa_values-fieldname = 'LIST'.
  APPEND gwa_values TO gt_values.
  CALL FUNCTION 'DYNP_VALUES_READ'
    EXPORTING
      dyname             = sy-cprog
      dynumb             = sy-dynnr
      translate_to_upper = 'X'
    TABLES
      dynpfields         = gt_values.

  READ TABLE gt_values INDEX 1 INTO gwa_values.
  IF sy-subrc = 0 AND gwa_values-fieldvalue IS NOT INITIAL.
    READ TABLE gt_list INTO gwa_list
                      WITH KEY key = gwa_values-fieldvalue.
    IF sy-subrc = 0.
      gv_selected_value = gwa_list-text.
    ENDIF.
  ENDIF.
*--------------------------------------------------------------*
*Initialization
*--------------------------------------------------------------*
INITIALIZATION.
  gwa_list-key = '1'.
  gwa_list-text = 'Product'.
  APPEND gwa_list TO gt_list.
  gwa_list-key = '2'.
  gwa_list-text = 'Collection'.
  APPEND gwa_list TO gt_list.
  gwa_list-key = '3'.
  gwa_list-text = 'Color'.
  APPEND gwa_list TO gt_list.
  gwa_list-key = '4'.
  gwa_list-text = 'Count'.
  APPEND gwa_list TO gt_list.

  CALL FUNCTION 'VRM_SET_VALUES'
    EXPORTING
      id              = 'LIST'
      values          = gt_list
    EXCEPTIONS
      id_illegal_name = 1
      OTHERS          = 2.
*--------------------------------------------------------------*
*Start of Selection
*--------------------------------------------------------------*
START-OF-SELECTION.
  WRITE:/ gv_selected_value.
Selection Screen Output
list-box

Pushbutton on ABAP Selection Screen

TABLES sscrfields.
*--------------------------------------------------------------*
*Selection-Screen
*--------------------------------------------------------------*
SELECTION-SCREEN:
      PUSHBUTTON /2(40) button1 USER-COMMAND but1,
      PUSHBUTTON /2(40) button2 USER-COMMAND but2.
*--------------------------------------------------------------*
*At Selection-Screen
*--------------------------------------------------------------*
AT SELECTION-SCREEN.
  CASE sscrfields.
    WHEN 'BUT1'.
      MESSAGE 'Button 1 was clicked' TYPE 'I'.
    WHEN 'BUT2'.
      MESSAGE 'Button 2 was clicked' TYPE 'I'.
  ENDCASE.
*--------------------------------------------------------------*
*Initialization
*--------------------------------------------------------------*
INITIALIZATION.
  button1 = 'Button 1'.
  button2 = 'Button 2'.
Selection Screen Output
Pushbutton-selection-screen-1
If Button 1 is clicked then we get the following popup.
Pushbutton-selection-screen-2
Even we can add icons to the pushbuttons on selection screen.
TYPE-POOLS: icon.
TABLES sscrfields.
*--------------------------------------------------------------*
*Selection-Screen
*--------------------------------------------------------------*
SELECTION-SCREEN:
      PUSHBUTTON /2(40) button1 USER-COMMAND but1,
      PUSHBUTTON /2(40) button2 USER-COMMAND but2.
*--------------------------------------------------------------*
*At Selection-Screen
*--------------------------------------------------------------*
AT SELECTION-SCREEN.
  CASE sscrfields.
    WHEN 'BUT1'.
      MESSAGE 'Button 1 was clicked' TYPE 'I'.
    WHEN 'BUT2'.
      MESSAGE 'Button 2 was clicked' TYPE 'I'.
  ENDCASE.
*--------------------------------------------------------------*
*Initialization
*--------------------------------------------------------------*
INITIALIZATION.
  button1 = 'Button 1'.
  button2 = 'Button 2'.

  CALL FUNCTION 'ICON_CREATE'
    EXPORTING
      name   = icon_okay
      text   = 'Continue'
      info   = 'Click to Continue'
    IMPORTING
      RESULT = button1
    EXCEPTIONS
      OTHERS = 0.

  CALL FUNCTION 'ICON_CREATE'
    EXPORTING
      name   = icon_cancel
      text   = 'Exit'
      info   = 'Click to Exit'
    IMPORTING
      RESULT = button2
    EXCEPTIONS
      OTHERS = 0.
Selection Screen Output
Pushbutton-selection-screen-3

Wednesday, June 12, 2013

Create PDF from Print Preview of Smartform

To convert a smartform to a PDF file, just display the the print preview of the smartform.smartforms-print-preview-pdf-1Enter ‘PDF!’ in command field and press enter.smartforms-print-preview-pdf-2The smartform will be displayed as PDF in the PDF preview window. Use the save button to save it as a PDF file.

How to View Old Transport Requests/Tasks of Smartform Layout

To display old transport requests/tasks of smartform layout, go to Transport Organizer Tools (SE03).
find-smartform-transport-requests-1
Double click on "Search for Objects in Requests/Tasks" under "Objects in Requests".
find-smartform-transport-requests-2
Enter the object type as ‘SSFO’ (Smartform), check the checkbox, enter the name of the smartform layout name for which you want to display old transport requests/tasks and execute.
find-smartform-transport-requests-3
All the transport requests/Tasks will be displayed as shown above.

Useful Transaction codes for SAP ABAP Developers

Below table summarizes the useful tcodes for SAP ABAP developers.


TCODEDESCRIPTION
AL11SAP Directories
BAPIBAPI Explorer
CMODProject Management of SAP Enhancements
OAORBusiness Document Navigator
SA38ABAP : Execute Program
SALEIDoc and ALE Development
SCDOChange Document Objects
SCICode Inspector
SCOTSAPConnect : Administration
SE01Transport Organizer (extended view)
SE03Transport Organizer Tools
SE09Transport Organizer
SE11ABAP Dictionary
SE14Database Utility
SE16Data Browser
SE18Business Add-ins(BADI’s): Definition
SE19Business Add-ins(BADI’s): Implementation
SE24Class Builder
SE30ABAP Runtime Analysis
SE36Logical Database Builder
SE37Function Builder
SE38ABAP Editor
SE39ABAP Splitscreen Editor
SE41Menu Painter
SE43NArea Menus
SE51Screen Painter
SE71Form painter
SE75SAPScript Settings
SE80Object Navigator
SE91Message Maintenance
SE93Maintain Transaction
SHD0Transaction & Screen Variants
SHDBTransaction Recorder
SLINABAP program extended syntax check
SM04User List
SM12Lock Entries
SM13Update Requests
SM30Maintain Table Views
SM35Batch Input : Session Overview
SM36Define Background Job
SM37Simple Job Selection
SM49External Operating System Commands
SM50Process Overview
SM59RFC Destinations
SMARTFORMSSAP Smart Forms
SMARTSTYLESSmart Styles
SMODSAP Enhancements
SNOTESAP Note Assistant
SO10Standard Text
SP01Spool List
SPADSpool Administration
SPROSAP Customizing
SQ01SAP Query
ST05SQL Trace
STMSTransport Management System
ST22Dump Analysis
SU21Authorization Objects
SU01User Maintenance
SU53Display Authorization Data

How to download IDoc as XML file?

IDocs are sent to the Java Connector(JCo) as an IDoc-XML string. If you want to see how IDoc-XML look like, follow the below steps to download the IDoc to frontend as XML file.
  1. First create a IDoc object for a given idoc number.
  2. Get IDoc XML data as a string.
  3. Download the XML string to frontend.
PARAMETERS: p_idoc TYPE edidd-docnum.

DATA: o_idoc_xml TYPE REF TO cl_idoc_xml1.
DATA: gv_string  TYPE string.
DATA: gt_string  TYPE TABLE OF string.

*Create IDoc object
CREATE OBJECT o_idoc_xml
EXPORTING
docnum = p_idoc
EXCEPTIONS
OTHERS = 1.
IF sy-subrc NE 0.
  WRITE: /'Error creating idoc object'.
  EXIT.
ENDIF.

*Get IDoc data as string
CALL METHOD o_idoc_xml->get_xmldata_as_string
  IMPORTING
    data_string = gv_string.

APPEND gv_string TO gt_string.

IF sy-subrc NE 0 OR o_idoc_xml IS INITIAL.
  WRITE: /'Error getting xml data as string'.
  EXIT.
ENDIF.

*Download IDoc data as XML file
CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
    filename = 'C:\idoc.xml'
  TABLES
    data_tab = gt_string.
Downloaded XML file
ALE-IDoc-1

Tuesday, June 11, 2013

Display Traffic Lights in SAP ALV

Use below steps to disable icons in ABAP ALV toolbar.
  1. Add a column to the internal table to hold the traffic lights value(i.e. 1 for RED, 2 for YELLOW and 3 for GREEN).
  2. Fetch data from database table and build field catalog.
  3. Assign different traffic lights to each row based on condition.
  4. To display traffic lights in the ALV, fill the lights field name in the LIGHTS_FIELDNAME of ALV layout.
  5. Pass field catalog to function module ‘REUSE_ALV_GRID_DISPLAY’.
TYPE-POOLS: slis.  " SLIS contains all the ALV data types
*&---------------------------------------------------------------------*
*& Data Types
*&---------------------------------------------------------------------*
TYPES: BEGIN OF ty_sbook.
        INCLUDE STRUCTURE sbook.
TYPES: icon TYPE c,  " Add field to hold traffic light value
       END OF ty_sbook.
*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*
DATA: it_sbook     TYPE TABLE OF ty_sbook.
DATA: wa_sbook     TYPE ty_sbook.
DATA: it_fieldcat  TYPE slis_t_fieldcat_alv,
      wa_fieldcat  TYPE slis_fieldcat_alv.
DATA: is_layout    TYPE slis_layout_alv.
DATA: g_repid      TYPE sy-repid.
*&---------------------------------------------------------------------*
*& START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.
  g_repid = sy-repid.
*Fetch data from the database
  SELECT * UP TO 20 ROWS FROM sbook INTO TABLE it_sbook.

*Assign different traffic lights to each row based on condition
  LOOP AT it_sbook INTO wa_sbook.
    IF wa_sbook-luggweight LE 0.
      wa_sbook-icon = 1.  " Red Traffic Light
    ELSEIF wa_sbook-luggweight LE 10.
      wa_sbook-icon = 2.  " Yellow Traffic Light
    ELSE.
      wa_sbook-icon = 3.  " Green Traffic Light
    ENDIF.
    MODIFY it_sbook FROM wa_sbook TRANSPORTING icon.
    CLEAR: wa_sbook.
  ENDLOOP.

*Build field catalog
  wa_fieldcat-fieldname  = 'CARRID'.    " Fieldname in the data table
  wa_fieldcat-seltext_m  = 'Airline'.   " Column description in the output
  APPEND wa_fieldcat TO it_fieldcat.
  CLEAR wa_fieldcat.

  wa_fieldcat-fieldname  = 'CONNID'.
  wa_fieldcat-seltext_m  = 'Con. No.'.
  APPEND wa_fieldcat TO it_fieldcat.
  CLEAR wa_fieldcat.

  wa_fieldcat-fieldname  = 'FLDATE'.
  wa_fieldcat-seltext_m  = 'Date'.
  APPEND wa_fieldcat TO it_fieldcat.
  CLEAR wa_fieldcat.

  wa_fieldcat-fieldname  = 'BOOKID'.
  wa_fieldcat-seltext_m  = 'Book. ID'.
  APPEND wa_fieldcat TO it_fieldcat.
  CLEAR wa_fieldcat.

  wa_fieldcat-fieldname  = 'FORCURAM'.
  wa_fieldcat-seltext_m  = 'Price'.
  APPEND wa_fieldcat TO it_fieldcat.
  CLEAR wa_fieldcat.

  wa_fieldcat-fieldname  = 'FORCURKEY'.
  wa_fieldcat-seltext_m  = 'Currency'.
  APPEND wa_fieldcat TO it_fieldcat.
  CLEAR wa_fieldcat.

  wa_fieldcat-fieldname  = 'LUGGWEIGHT'.
  wa_fieldcat-seltext_m  = 'Weight'.
  APPEND wa_fieldcat TO it_fieldcat.
  CLEAR wa_fieldcat.

  wa_fieldcat-fieldname  = 'WUNIT'.
  wa_fieldcat-seltext_m  = 'Unit'.
  APPEND wa_fieldcat TO it_fieldcat.
  CLEAR wa_fieldcat.

*Fill layout info.
*Fill traffic lights field name in the ALV layout
  is_layout-lights_fieldname = 'ICON'.

*Pass data and field catalog to ALV function module to display ALV list
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = g_repid
      is_layout          = is_layout
      it_fieldcat        = it_fieldcat
    TABLES
      t_outtab           = it_sbook
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.
Output
traffic-lights-in-abap-alv

Display Header in ABAP ALV Grid

To display header in ALV grid use the following steps.
  1. Build the ALV header table.
  2. Pass the TOP-OF-EVENT subroutine name and callback program name to I_CALLBACK_TOP_OF_PAGE and I_CALLBACK_PROGRAM parameters of REUSE_ALV_GRID_DISPLAY function module.
  3. Display the ALV header by passing the ALV header table built in   step one to function module ‘REUSE_ALV_COMMENTARY_WRITE’ inside TOP-OF-EVENT subroutine.
TYPE-POOLS: slis.
*----------------------------------------------------------------------*
*     Data Decalaration
*----------------------------------------------------------------------*
DATA: it_spfli TYPE TABLE OF spfli.
DATA: g_repid TYPE sy-repid.
DATA: it_listheader TYPE slis_t_listheader,
      wa_listheader TYPE slis_listheader.
*----------------------------------------------------------------------*
*     START-OF-SELECTION
*----------------------------------------------------------------------*
START-OF-SELECTION.
  g_repid = sy-repid.

  SELECT * FROM spfli INTO TABLE it_spfli.

  PERFORM build_alv_header.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program     = g_repid
      i_callback_top_of_page = 'TOP_OF_PAGE'
      i_structure_name       = 'SPFLI'
    TABLES
      t_outtab               = it_spfli.

*&---------------------------------------------------------------------*
*&      Form  BUILD_ALV_HEADER
*&---------------------------------------------------------------------*
FORM build_alv_header .

*  Type H is used to display headers i.e. big font
  wa_listheader-typ  = 'H'.
  wa_listheader-info ='Flight Details'.
  APPEND wa_listheader TO it_listheader.
  CLEAR wa_listheader.

*  Type S is used to display key and value pairs
  wa_listheader-typ = 'S'.
  wa_listheader-key = 'Date :' .
  CONCATENATE  sy-datum+6(2)
               sy-datum+4(2)
               sy-datum(4)
               INTO wa_listheader-info
               SEPARATED BY '/'.
  APPEND wa_listheader TO it_listheader.
  CLEAR wa_listheader.

*  Type A is used to display italic font
  wa_listheader-typ = 'A'.
  wa_listheader-info ='SAP ALV Report'.
  APPEND wa_listheader TO it_listheader.
  CLEAR wa_listheader.
ENDFORM.                    " BUILD_ALV_HEADER
*&---------------------------------------------------------------------*
*&      Form  top_of_page
*&---------------------------------------------------------------------*
FORM top_of_page.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = it_listheader.

ENDFORM.                    "top_of_page
Output
abap-alv-header

Currency wise and Quantity wise Totals in SAP ALV

Use below steps to disable icons in ABAP ALV toolbar.
  1. Fetch data from database table.
  2. Build field catalog.
  3. To get the currency wise totals of price, fill the CFIELDNAME field of field catalog with the currency column name and set do_sum to ‘X’.
  4. To get the quantity wise totals of luggage, fill the QFIELDNAME field of field catalog with the weight unit column name and set do_sum to ‘X’.
  5. Pass field catalog to function module ‘REUSE_ALV_GRID_DISPLAY’.
TYPE-POOLS: slis.  " SLIS contains all the ALV data types
*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*
DATA: it_sbook     TYPE TABLE OF sbook.
DATA: it_fieldcat  TYPE slis_t_fieldcat_alv,
      wa_fieldcat  TYPE slis_fieldcat_alv.
DATA: g_repid      TYPE sy-repid.
*&---------------------------------------------------------------------*
*& START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.
  g_repid = sy-repid.
*Fetch data from the database
  SELECT * UP TO 20 ROWS FROM sbook INTO TABLE it_sbook.

*Build field catalog
  wa_fieldcat-fieldname  = 'CARRID'.    " Fieldname in the data table
  wa_fieldcat-seltext_m  = 'Airline'.   " Column description in the output
  APPEND wa_fieldcat TO it_fieldcat.
  CLEAR wa_fieldcat.

  wa_fieldcat-fieldname  = 'CONNID'.
  wa_fieldcat-seltext_m  = 'Con. No.'.
  APPEND wa_fieldcat TO it_fieldcat.
  CLEAR wa_fieldcat.

  wa_fieldcat-fieldname  = 'FLDATE'.
  wa_fieldcat-seltext_m  = 'Date'.
  APPEND wa_fieldcat TO it_fieldcat.
  CLEAR wa_fieldcat.

  wa_fieldcat-fieldname  = 'BOOKID'.
  wa_fieldcat-seltext_m  = 'Book. ID'.
  APPEND wa_fieldcat TO it_fieldcat.
  CLEAR wa_fieldcat.

  wa_fieldcat-fieldname  = 'FORCURAM'.
  wa_fieldcat-seltext_m  = 'Price'.
  wa_fieldcat-do_sum     = 'X'.
  wa_fieldcat-cfieldname = 'FORCURKEY'.
  APPEND wa_fieldcat TO it_fieldcat.
  CLEAR wa_fieldcat.

  wa_fieldcat-fieldname  = 'FORCURKEY'.
  wa_fieldcat-seltext_m  = 'Currency'.
  APPEND wa_fieldcat TO it_fieldcat.
  CLEAR wa_fieldcat.

  wa_fieldcat-fieldname  = 'LUGGWEIGHT'.
  wa_fieldcat-seltext_m  = 'Weight'.
  wa_fieldcat-do_sum     = 'X'.
  wa_fieldcat-qfieldname = 'WUNIT'.
  APPEND wa_fieldcat TO it_fieldcat.
  CLEAR wa_fieldcat.

  wa_fieldcat-fieldname  = 'WUNIT'.
  wa_fieldcat-seltext_m  = 'Unit'.
  APPEND wa_fieldcat TO it_fieldcat.
  CLEAR wa_fieldcat.

*Pass data and field catalog to ALV function module to display ALV list
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = g_repid
      it_fieldcat        = it_fieldcat
    TABLES
      t_outtab           = it_sbook
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.
Output
currency-wise-totals-sap-alv

How to add subtotal in ALV report

ZADDSUBTOTALEX.

TYPE-POOLS: slis.  " SLIS contains all the ALV data types

*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*
DATA: it_sbook     TYPE TABLE OF sbook.
DATA: it_fieldcat  TYPE slis_t_fieldcat_alv,
      wa_fieldcat  TYPE slis_fieldcat_alv.

 DATA: it_sort      TYPE slis_t_sortinfo_alv,
      wa_sort      TYPE slis_sortinfo_alv.
*&---------------------------------------------------------------------*
*& START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.

*Fetch data from the database
  SELECT * FROM sbook INTO TABLE it_sbook.

*Build field catalog
  wa_fieldcat-fieldname  = 'CARRID'.    " Fieldname in the data table
  wa_fieldcat-seltext_m  = 'Airline'.   " Column description in the output
  APPEND wa_fieldcat TO it_fieldcat.

  wa_fieldcat-fieldname  = 'CONNID'.
  wa_fieldcat-seltext_m  = 'Con. No.'.
  APPEND wa_fieldcat TO it_fieldcat.

  wa_fieldcat-fieldname  = 'FLDATE'.
  wa_fieldcat-seltext_m  = 'Date'.
  APPEND wa_fieldcat TO it_fieldcat.

  wa_fieldcat-fieldname  = 'BOOKID'.
  wa_fieldcat-seltext_m  = 'Book. ID'.
  APPEND wa_fieldcat TO it_fieldcat.

  wa_fieldcat-fieldname  = 'PASSNAME'.
  wa_fieldcat-seltext_m  = 'Passenger Name'.
  APPEND wa_fieldcat TO it_fieldcat.

  wa_fieldcat-fieldname  = 'LOCCURAM'.
  wa_fieldcat-seltext_m  = 'Price'.
*Calculate Total for Price
  wa_fieldcat-do_sum     = 'X'.
  wa_fieldcat-datatype = 'QUAN'.
  APPEND wa_fieldcat TO it_fieldcat.

  wa_sort-spos      = 1.
  wa_sort-fieldname = 'CARRID'.
  wa_sort-up        = 'X'.
  wa_sort-subtot    = 'X'.
  APPEND wa_sort TO it_sort.

*Pass data and field catalog to ALV function module to display ALV list
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      it_fieldcat   = it_fieldcat
      it_sort       = it_sort
    TABLES
      t_outtab      = it_sbook
    EXCEPTIONS
      program_error = 1
      OTHERS        = 2.

Output