Wednesday, October 30, 2013

SAP ABAP how to create a custom conversion exit

Hi Folks,
creating conversion exits is quite simple. I just want to write down the procedure i use in the hope that it helps.

Suppose you have a field "NATSL" you want to create a conversion exit for.

1) Create an empty function group from se80

2) From se37 look up a couple of search exit functions that resemble what you need. In my case i find
CONVERSION_EXIT_GESCH_INPUT and CONVERSION_EXIT_GESCH_OUTPUT

3) Copy them to your function group changing the name to suite your conversion exit name. So you copy
CONVERSION_EXIT_GESCH_INPUT  to CONVERSION_EXIT_NATSL_INPUT and
 CONVERSION_EXIT_GESCH_OUTPUT to CONVERSION_EXIT_NATSL_OUTPUT

4) Adapt the coding ( you find it below). Basically the Input is always the same. The output module ( roughly speaking) receives the"code" and has to look up the "description". In this coding i do a select every time. Don't do this at home: do avoid it by buffering because these conversion exits are called very often.

One remark is that conversion exits have to be in the SAP namespace. you get a warning and you can save it.
If in the future SAP decides to release a conversion exit with the same name as yours the your work will be lost.
At the moment the only " solution " is to use really fancy names (ZZNAT? :-) )

I hope this helps.
Cheers

FUNCTION CONVERSION_EXIT_NATSL_OUTPUT .
*"--------------------------------------------------------------------
*"*"Interfaccia locale:
*"  IMPORTING
*"     VALUE(INPUT)
*"  EXPORTING
*"     VALUE(OUTPUT)
*"  EXCEPTIONS
*"      NOT_FOUND
*"--------------------------------------------------------------------

  output = input.

    data ls_t005t type t005t.
    data lv_land1 type land1.

  if input <> space.
     lv_land1 = input.
     select single * from t005t into ls_t005t
                         where spras = sy-langu AND
                               land1 = lv_land1.

    if sy-subrc <> 0.
      clear output.
    else.
      output = ls_t005t-natio.
    endif.

  endif.


endfunction.


FUNCTION CONVERSION_EXIT_NATSL_INPUT .
*"--------------------------------------------------------------------
*"*"Interfaccia locale:
*"  IMPORTING
*"     VALUE(INPUT)
*"  EXPORTING
*"     VALUE(OUTPUT)
*"--------------------------------------------------------------------

 output = input.

ENDFUNCTION.

SAP ABAP F4 Help examples with drop down list box and standard F4

Hi Folks,
yesterday i did some complete F4 with dropdown and with standard F4.
I did it on two trivial fields with a domain that has a value table ( T005T)
One field is nationality of a person ( FANAT), the other is the land of birth ( FGBLD).

There are 6 things i learned that i want to share
1) You always need a conversion exit if you want the description displayed instead of the "code" in both casese with and without drop down.
2) In case of normal F4 you need the edit mask too (  wa_fcat-edit_mask = '==NATSL'  )
3) Referencing the fields to the DDIC does not mean that they are automatically displayed as in dynpros. neither you do have the possibilities that the dynpro "painter" gives you.
4) In order for the dropdown to work with the DRAL (drop down alias table ) you need to set the following flag in the fieldcatalogue    wa_fcat-drdn_alias = 'X'.
5) In ALV almost everything is coding and cryptic fields in the fieldcatalogue, mainly undocumented.
6) there are no ready to use conversion exits for the fields you need, but only for the field you do not need.( This is a corollary of murphy's law)

Below you find the main coding. and here is the result in the pictures.
In the first picture you see the line of the grid. the first field is the Drop down. The second is the Field with F4.

The drop down on the firs field look like this




the F4 help on nationality looks like this:

And here is the coding. Have fun and beware of grid controls!
Cheers

        WHEN 'FGBLD'.

          wa_fcat-edit = 'X'.
          wa_fcat-f4availabl = 'X'.
          wa_fcat-drdn_alias = 'X'.
 

          CALL METHOD set_fgbld_ddlb
            CHANGING
              ps_fcat     = wa_fcat
              pt_f4       = lt_f4
              pt_dropdown = lt_dropdown
              pt_dral     = lt_dral.
 

        WHEN 'FANAT'.
 

          wa_fcat-edit = 'X'.
          wa_fcat-f4availabl = 'X'.
          wa_fcat-ref_field  = 'FANAT'.
          wa_fcat-ref_table = 'P0021'.
          wa_fcat-edit_mask = '==NATSL'.
          wa_fcat-convexit = 'NATSL'.
          wa_fcat-outputlen = 15.
 

          CALL METHOD enable_f4
            CHANGING
              ps_fcat = wa_fcat
              pt_f4   = lt_f4.



  METHOD: set_fgbld_ddlb.

    DATA :ls_dropdown TYPE lvc_s_dral.

    DATA ls_t005t TYPE t005t.

    DATA: ls_f4 TYPE lvc_s_f4.

    ps_fcat-drdn_hndl = '2'.
    ps_fcat-outputlen = 15.
    ps_fcat-f4availabl = 'X'.
    ps_fcat-convexit = 'LAND1'.

*    REFRESH lt_f4.

    ls_f4-fieldname  = ps_fcat-fieldname.
    ls_f4-register   = 'X'.
    ls_f4-getbefore  = 'X'.
    ls_f4-chngeafter = 'X'.
    INSERT ls_f4 INTO TABLE pt_f4.

    sort at_t005t by landx.
    LOOP AT at_t005t INTO ls_t005t.

      ls_dropdown-handle = '2'.
      ls_dropdown-value = ls_t005t-landx.
      ls_dropdown-int_value = ls_t005t-land1.
      APPEND ls_dropdown TO pt_dral.

    ENDLOOP.

*
  ENDMETHOD.

Monday, October 28, 2013

F4 help callback method howto for F4IF_FIELD_VALUE_REQUEST in SAP ABAP ALV GRID

Hi folks,

my F4 help implementations are going on. Slowly.  In a previous post i have shown how to enable F4 help for a field in your ALV grid.
Here is the implementation of the F4 help in coding using function F4IF_FIELD_VALUE_REQUEST.
To pass the values to the search help the easiest way in OO programming is to use the parameter "callback_method"

Doing so  is tricky if you use a local class design. this is because the callback method of function F4IF_FIELD_VALUE_REQUEST is called from a function pool program. Local classes of your program do not exist in the function pool.
This means that if you pass a reference to your local class to the funcion pool when it is dereferenced you get a dump.

This means you have to implement a global class (se24) in order to catch the callback method.

The callback_method parameter is acutally a reference to an interface object of the type  IF_F4CALLBACK_VALUE_REQUEST

The steps to implement it therefore are:
1) Declare the interface in your global class class

2) Implement the interface method in your global class


3) Call the F4
F4IF_FIELD_VALUE_REQUEST function specifying the callback "method" ( reference object) in your F4 callback method. In my case this is the method called from the event handler object.

  METHOD : on_f4.

    DATA lt_ret TYPE TABLE OF ddshretval.

    CASE e_fieldname.

      WHEN 'FAMSA'.


     data lr_f4_cb type ref to Z_CL_F4_CALLBACK.
     create object lr_f4_cb.
     lr_f4_cb->a_pernr = a_pernr.

        CALL FUNCTION 'F4IF_FIELD_VALUE_REQUEST'
          EXPORTING
            tabname             = 'P0021'
            fieldname           = 'FAMSA'
*            callback_program    = sy-repid
*            callback_form       = 'CALLB_VALUE_REQUEST'
    callback_method           = lr_f4_cb
*   SELECTION_SCREEN          = ' '
* IMPORTING
*   USER_RESET                =
        TABLES
          return_tab                = lt_ret
        EXCEPTIONS

          OTHERS                    = 5     .
        IF sy-subrc <> 0.
* Implement suitable error handling here
        ENDIF.

        er_event_data->m_event_handled = 'X'.
      WHEN OTHERS.
    ENDCASE.

  ENDMETHOD.                    ":

Thursday, October 24, 2013

how to enable F4 Help in alv grid step by step SAP ABAP

Hi Folks,

In order to enable f4 help in you ALV grid a few simple steps are required

1)   Declare and implement method on_f4 in your event handler like this:
1a) In the public section of your event receiver declare

methods:    on_f4 for event onf4 of cl_gui_alv_grid
          importing sender
                 e_fieldname
                 e_fieldvalue
                 es_row_no
                 er_event_data
                 et_bad_cells
                 e_display.


1b) Implement the method
   METHOD on_f4.

    CALL METHOD ar_my_grid->on_my_f4
      EXPORTING
        e_fieldname
            =
        e_fieldname
        e_fieldvalue  = e_fieldvalue  
        es_row_no        = es_row_no  
        er_event_data = er_event_data
        et_bad_cells  = et_bad_cells  
        e_display        = e_display.

   ENDMETHOD.                    "on_f4


2) In your ALV register the event handle for this method
    SET HANDLER:
    a_event_receiver->on_F4                 FOR me.

3 )In your ALV implement a callback method to be called from the event handler

     methods: on_my_f4
          importing           E_FIELDNAME     Type    LVC_FNAME
                              E_FIELDVALUE    Type    LVC_VALUE
                              ES_ROW_NO           Type    LVC_S_ROID
                              ER_EVENT_DATA    Type Ref To    CL_ALV_EVENT_DATA
                              ET_BAD_CELLS    Type    LVC_T_MODI
                              E_DISPLAY           Type    CHAR01.

3) Activate and set a break point in the on_my_f4 emthod implementation.

4) to activate F4 for columns of your ALV you need to set the field f4availabl = 'X'  in the fieldcatalogue
5)  fill table  lvc_t_f4 with a line for your field ( see utility form below)
6) call method register_f4_for_fields

Hope this helps. You find a complete example in standard report  BCALV_TEST_GRID_F4_HELP
In the next article i'll show how to implement F4 for one of your fields in edit mode.
Cheers


ps. here a little tiny method useful to be called from my build_fieldcatalogue method
METHOD : enable_f4."  changing     ps_fcat     type lvc_s_fcat
    "              pt_f4       TYPE lvc_t_f4.

    DATA: ls_f4 TYPE lvc_s_f4.

    ps_fcat-f4availabl = 'X'.

    ls_f4-fieldname  = ps_fcat-fieldname.
    ls_f4-register   = 'X'.
    ls_f4-getbefore  = 'X'.
    ls_f4-chngeafter = 'X'.
    APPEND ls_f4 TO pt_f4.

  ENDMETHOD.                    ":

SAP ABAP ALV GRID drop down list box step by step part 2

Hi folks,
 in part 1 of this post i showed how to create a Dropdown list box step by step in an ALV grid.
As i pointed out in my conclusions it seemed a bit complicated to me for such a little result.

Now i realized that it is more complicated than that.

Assume you want a search help with drop down list box for a simple field like the sex of a person
There is a standard domain GESCH (Meaning Geschlecht = Sex in german ) with fixed values 1 and 2 meaning Male Female.

Now in my alv the values 1 and 2 show up.

In order to have the texts shown i have to use table type "lvc_t_dral" instead of "lvc_t_drop"
This table contains a field int_value containing the code ( 1 and 2 in my case) and the field values containing the text "Male and Female".

This yields a column with sex values of 1 and 2 ( really ugly ) with the text values "Male and Female" popping up in the drop down list box with no connection to the codes 1 and 2

In order to have the automatic conversion of the values one more step is needed:
the creation of a conversion_exit
In my case i am lucky because one exists already: it is called GESCH ( functions
CONVERSION_EXIT_GESCH_OUTPUT and CONVERSION_EXIT_GESCH_INPUT)
The conversion exit must be entered into field "CONVEXIT" of the fieldcatalogue
like this:    ps_fcat-convexit = 'GESCH'.

Then it works just fine.
HTH
Enjoy! :-)


  METHOD: set_fasex_ddlb.

    DATA :ls_dropdown TYPE lvc_s_dral.

    DATA: ls_f4 TYPE lvc_s_f4.

    ps_fcat-drdn_hndl = '1'.
    ps_fcat-outputlen = 10.
    ps_fcat-f4availabl = 'X'.
    ps_fcat-convexit = 'GESCH'.

    ls_f4-fieldname  = ps_fcat-fieldname.
    ls_f4-register   = 'X'.
    ls_f4-getbefore  = 'X'.
    ls_f4-chngeafter = 'X'.
    APPEND ls_f4 TO pt_f4.

    ls_dropdown-handle = '1'.
    ls_dropdown-value = 'Female'.
    ls_dropdown-int_value = '2'.
    APPEND ls_dropdown TO pt_dral.

    ls_dropdown-handle = '1'.
    ls_dropdown-value = 'Male'.
    ls_dropdown-int_value = '1'.
    APPEND ls_dropdown TO pt_dral.
*
  ENDMETHOD.                    "set_fasex_ddlb


Wednesday, October 23, 2013

SAP ABAP ALV GRID drop down list box step by step part 1



Hi folks,
today i'll try to implement the F4 help in my ALV grid using a drop down list box (DDLB).

Here the steps
1) In the fieldcatalogue of the field  set      f4availabl  = 'X'
2) Fill the F4 table     lt_f4 TYPE lvc_t_f4 at least setting "register = 'X'
3) Register the F4 table calling method register_f4_for_fields
4) Fill the dropdown table :lt_dropdown TYPE lvc_t_drop
5) call method set_drop_down_table

No need to register the on_f4 callback. 

Of course you have to make sure that the dropdown handle you pass in the fieldcatalogue for the fields corresponds to the entries you want in the dropdown table.
In my case it is the handle "1". Just look at the sample it is clear enough.

In the real world  this useless intricacy of the handle and triple registration might be hidden behind an interface method that accepts the table with the values and takes care of generating the handles and collecting the table entries in order to avoid mismatches and mistakes that might happen when multple developers hackon the same piece grid.

Here the result in the grid

Here the coding of the method to enable the DDLB as shown

  METHOD: set_famsa_ddlb.

    DATA :lt_dropdown TYPE lvc_t_drop.
    DATA :ls_dropdown TYPE lvc_s_drop.

    DATA: ls_f4 TYPE lvc_s_f4,
          lt_f4 TYPE lvc_t_f4.

    ps_fcat-drdn_hndl = '1'.
    ps_fcat-outputlen = 20.
    ps_fcat-f4availabl = 'X'.

    REFRESH lt_f4.

    ls_f4-fieldname  = ps_fcat-fieldname.
    ls_f4-register   = 'X'.
    ls_f4-getbefore  = 'X'.
    ls_f4-chngeafter = 'X'.
    APPEND ls_f4 TO lt_f4.
*These methods shall be called outside. The purpose is test only
    CALL METHOD register_f4_for_fields
      EXPORTING
        it_f4 = lt_f4.

*Shall read t591a or use search help... at least in real life
    ls_dropdown-handle = '1'.
    ls_dropdown-value = 'Son'.
    APPEND ls_dropdown TO lt_dropdown.
    ls_dropdown-handle = '1'.
    ls_dropdown-value = 'Wife'.
    APPEND ls_dropdown TO lt_dropdown.
    ls_dropdown-handle = '1'.
    ls_dropdown-value = 'Granny'.
    APPEND ls_dropdown TO lt_dropdown.
    ls_dropdown-handle = '1'.
    ls_dropdown-value = 'Sister'.
    APPEND ls_dropdown TO lt_dropdown.
    ls_dropdown-handle = '1'.
    ls_dropdown-value = 'Brother'.
    APPEND ls_dropdown TO lt_dropdown.

*These methods shall be called outside. The purpose is test only
    CALL METHOD me->set_drop_down_table
      EXPORTING
        it_drop_down = lt_dropdown[].

  ENDMETHOD.                    "set_famsa_ddlb

A lot of coding for a little tiny result IMHO. Anyway hope you enjoy.
Cheers

Tuesday, October 22, 2013

How to use set_delta_cells in sap abap alv grid oo or validating unchanged data



Hi folks,
this post is about a common problem when using alv grid.
The normal change validation system in the alv operates only on data the user has changed.

Often it happens that you need to do changes on data that the user has not changed in that moment.
For example you copy a line. At the beginning it is necessarily a duplicate of the original
and you disable checks in on data changed to give the user time to change the line to its purpose.
But this is the only moment where the alv recognizes the new cells as changed.
Later on they are not "changed" anymore.

I solved it using a "confim data" pushbutton that appears after the copy button is pressed until the editing is complete.
At this time in the pushbutton callback i
1) Build a modi table with "modified" cells i want to check and their values
2) call the method "set_delta_cells"
3) call the method "check_changed_data".

The first method  marks the cells as if they had been edited.
The second method causes the data validation to be triggered including the "on_data_changed" event.

You find sample coding below.

Interestingly enough it is also possibile to raise the event directly with the raise event statement. ( you find the correspongin coding commented below) 
The problem is that the "er_data_changed" is empty so basically change validation cannot issue any message without some havoc.

Hope this helps.
Cheers


Method pushbutton_callback.


* More coding here to read the new line assigning it to the generic field symbol  
*......
*....

    DATA lt_modi TYPE lvc_t_modi.
    DATA ls_modi TYPE lvc_s_modi.
    FIELD-SYMBOLS: TYPE any.
    DATA ls_fcat TYPE lvc_s_fcat.

    LOOP AT at_fldcat INTO ls_fcat.

      CLEAR ls_modi.
      ASSIGN COMPONENT ls_fcat-fieldname OF STRUCTURE TO .

      ls_modi-row_id = a_created_line_idx.
      ls_modi-fieldname = ls_fcat-fieldname.
      ls_modi-value = .
      APPEND ls_modi TO lt_modi.


    ENDLOOP.

    CALL METHOD me->set_delta_cells
      EXPORTING
        it_delta_cells  = lt_modi
        i_modified      = 'X'
*       i_frontend_only =
      .

    DATA l_valid TYPE char01.

    CALL METHOD check_changed_data
      IMPORTING
        e_valid = l_valid.

**6) data changed
*    RAISE EVENT data_changed EXPORTING er_data_changed = ar_data_changed
*                                       e_onf4          = space
*                                       e_onf4_before   = space
*                                       e_onf4_after    = space
*                                       e_ucomm         = m_ucomm.
*
**Raise event data changed finished. 
*    DATA lt_c TYPE lvc_t_modi.
*    DATA l_modi TYPE char01 VALUE 'X'.
*
*    RAISE EVENT data_changed_finished
*                EXPORTING e_modified    = l_modi
*    et_good_cells = lt_c.


  ENDMETHOD.                    "pushbutton_callback

Friday, October 18, 2013

SAP ABAP how to pass search help parameters in F4IF_FIELD_VALUE_REQUEST via callback form



Hi folks,
i am not a search help expert, but i think this little finding can be interesting for many of you.

You call a search help ( or the f4 of a table field that has a search help) and somehow you dont get the set parameter to work in order to pass the parameters to the search help.

I found out you can specify a callback routine where you can individually set each value for the parameters of the search help.
You have to use parameters callback_program and callback_form.
 The interface of the form is the following
FORM CALLB_value_request  TABLES   record_tab STRUCTURE seahlpres
                 CHANGING shlp TYPE shlp_descr_t   callcontrol LIKE ddshf4ctrl.

You find the sample report below. In particular here i am trying to use the standard IT0021 FAMSA search help.
This search help (H_PAD_T591A_SUBTY) has the pernr as a parameter. From the pernr it determines the molga and via a really weird table (T591A) with really ugly customizing it is determined if a subtype of IT0021 is valid for this pernr.
I do not want to program this whole thing from scratch so i want to use the search help. Here is the right way to call it.
Enjoy!



REPORT zzz.
START-OF-SELECTION.

  DATA lt_ret TYPE TABLE OF ddshretval.

  CALL FUNCTION 'F4IF_FIELD_VALUE_REQUEST'
    EXPORTING
      tabname             = 'P0021'
      fieldname           = 'FAMSA'
*     SEARCHHELP          = ' '
*     SHLPPARAM           = ' '
*     DYNPPROG            = ' '
*     DYNPNR              = ' '
*     DYNPROFIELD         = ' '
*     STEPL               = 0
*     VALUE               = ' '
*     MULTIPLE_CHOICE     = ' '
*     DISPLAY             = ' '
*     SUPPRESS_RECORDLIST = ' '
      callback_program    = sy-repid
      callback_form       = 'CALLB_VALUE_REQUEST'
*   CALLBACK_METHOD           =
*   SELECTION_SCREEN          = ' '
* IMPORTING
*   USER_RESET                =
  TABLES
    return_tab                = lt_ret
  EXCEPTIONS
*   FIELD_NOT_FOUND           = 1
*   NO_HELP_FOR_FIELD         = 2
*   INCONSISTENT_HELP         = 3
*   NO_VALUES_FOUND           = 4
    OTHERS                    = 5     .
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.


*&---------------------------------------------------------------------*
*&      Form
*&---------------------------------------------------------------------*
FORM CALLB_value_request  TABLES   record_tab STRUCTURE seahlpres
                 CHANGING shlp TYPE shlp_descr_t   callcontrol LIKE ddshf4ctrl.

  DATA: ls_interface TYPE ddshiface.

  LOOP AT shlp-interface INTO ls_interface.

    IF ls_interface-shlpfield = 'PERNR'.

      ls_interface-value = '00373737.

      MODIFY shlp-interface FROM ls_interface.

    ENDIF.

  ENDLOOP.
ENDFORM.

SAP Abap how to delete a directory from the presentation server (front end)




Hi folks,

here a little program to remove e directory on the presentation server.
Enjoy!

REPORT ZDIR.

parameters: p_dir type rlgrap-filename .

at selection-screen on value-request for p_dir .

  CALL FUNCTION 'F4_FILENAME'
  EXPORTING
   PROGRAM_NAME = SYST-CPROG
   DYNPRO_NUMBER = SYST-DYNNR
   FIELD_NAME = 'P_DIR'
  IMPORTING
   FILE_NAME = p_dir
.
start-of-selection .

  CALL FUNCTION 'GUI_REMOVE_DIRECTORY'
   EXPORTING
   DIRNAME = p_dir
  EXCEPTIONS
   FAILED = 1
   OTHERS = 2.

if sy-subrc eq 0.
  write:/ 'directory: ' , p_dir , ' removed.'.
endif.

SAP ABAP how to delete a file from the frontend

+
Hi Folks,
here an easy one: delete a file from the front end  (typically the PC of the user using the sapgui).
The function that does this is: GUI_DELETE_FILE

Enjoy it!

REPORT ZDEL

parameters: p_file type rlgrap-filename .
at selection-screen on value-request for p_file .

  CALL FUNCTION 'F4_FILENAME'
  EXPORTING
   PROGRAM_NAME = SYST-CPROG
   DYNPRO_NUMBER = SYST-DYNNR
   FIELD_NAME = 'P_FILE'
  IMPORTING
   FILE_NAME = p_file
.
start-of-selection .

  CALL FUNCTION 'GUI_DELETE_FILE'
   EXPORTING
   FILE_NAME = p_file
  EXCEPTIONS
   FAILED = 1
   OTHERS = 2
.
IF SY-SUBRC <> 0.
  write:/ 'file:' , p_file , ' not removed'.
ENDIF.

write:/ 'file:' , p_file , 'removed'.

Tuesday, October 8, 2013

How to clear ucomm in ALV grid SAP ABAP

Hi Folks,


The user command is stored in CL_GUI_ALV_GRID->M_UCOMM
So basically it is enough to clear m_ucomm

There is a method that can be called named SET_USER_COMMAND
It can be called like this
my_Grid->SET_USER_COMMAND
  exporting     i_ucomm = space.

It happens that you have an error condition triggered in your on_data_changed event handler.
If the error is red (blocking) no further processing can happen until the error is resolved.
Not even an undo button press is allowed.
So one can try to solve this situation by checking for a certain ucomm in the on data changed method.
But this does not work because once the ucomm is set, it stays set until another ucomm is selected, even throughout data changes in the ALV that have nothing to do with that ucomm.

To be honest the internal undo ucomm works even if there is an error, but you have to press it twice. Somewhat weird isn't it!?

Slowly i am realizing that the complexity of a non trivial data editing program with non trivial checks on the data ( like time constraints on time dependent data) based on ALV can be quite high, and that the ALV control itself does not help mastering this complexity: it adds more and more complexity with its weird control-flow structure.

How to create a control on a dynpro without a container using screen0 SAP ABAP


Hi Folks, 
here an easy nice tip: if you have only one alv or component on your dynpro you can disply it on the full dynpro just using the whole dynpro without using a control container.

You need to create a completely empty dynpro and that's it.


Here is an example:

DATA  lr_alv      TYPE REF TO   cl_gui_alv_grid.
DATA  lt_table  TYPE TABLE OF t529a.

SELECT * FROM t529a INTO TABLE lt_table.


CREATE OBJECT lr_alv
  EXPORTING
    i_parent = cl_gui_container=>screen0.

CALL METHOD lr_alv->set_table_for_first_display
  EXPORTING
    i_structure_name = 'T529A'
  CHANGING
    it_outtab        = lt_t529a

CALL SCREEN 100

Wednesday, October 2, 2013

the Class constructor and singleton in abap



Hi Folks,
i am hacking on an ALV oo program and needed or wanted to implement the singleton pattern.

A singleton is a class of which only one instance exists and some means is create to access the only instance without making it global of course...
Think of it as a global variable hidden behind a puzzling coding interface. At the very end it is just a single global instance of a class, but far more cool and  good looking :-)

Apart from jokes it makes sense. Obviously not in 10 lines programs. But in complex applications having a global in the scope of a whole project can be problematic... the singleton can be anice way to control this "global" and access it in a discplined and controlled manner

In my case i have program that runs on one person. So my "app" object is bound to the person and there is only one throuout the processing.

So i need a means to have a central object instance which can be accessed by all classes that need it.
In abap it can be done like in the following coding.

The class-method is a method that is run per "class" and not per "object" ( the instance of the class)
The class constructor is run only once per program before the class is called for the first time.

So the class construcor instantiates the object ( the only existing instance )
The class method gives access just to this one and only existing oblect of the given singleton class.

So in my program i can call it like this
...
DATA lr_my_app TYPE REF TO zcl_my_app.
  lr_my_app =  zcl_my_app=>get_singleton( ).
  CALL METHOD gr_my_app->init
....

Here the class example.


CLASS zcl_my_app DEFINITION.
  PUBLIC SECTION.
    class-METHODS: class_constructor.

    METHODS: init .

    CLASS-METHODS: get_ref RETURNING value(r_ref) TYPE REF TO zcl_my_app.

  PRIVATE SECTION.
    CLASS-DATA ar_singleton TYPE REF TO zcl_my_app.


CLASS zcl_my_app IMPLEMENTATION.

  METHOD: class_constructor.
    CREATE OBJECT ar_singleton.
  ENDMETHOD.                    "constructor

  METHOD: init.
*Do whatever
  ENDMETHOD.                    "init

  METHOD: get_ref.
    r_ref = ar_singleton.
  ENDMETHOD.                    "get_ref


One last word: i have seen implementations in the internet like this:

method GET_OBJECT.
" returning  value(R_OBJECT) type ref to ZCL_blabla .
*
  if zcl_blabla=>r_singleton is not bound.
    create object zcl_blabla=>r_singleton.
  endif.
*
  r_object = zcl_sblabla=>r_singleton .
endmethod.
 

Can i say that it is the same but at the same time it really sucks :-) LOL ?