Thursday, October 14, 2010

Creating dynamic tables in R/3 4.6C

Creating dynamic tables in R/3 4.6C



Looking for a way to create a dynamic internal table? In release WAS 620, you can, by using :


CREATE DATA new_line TYPE table of (p_tab).


This syntax was not allowed in earlier releases. The FM Y_CREATE_DYNAMIC_TABLE can help you to achieve this goal.


FUNCTION y_create_dynamic_table.
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     REFERENCE(I_STRUCT_NAME) TYPE  DD02L-TABNAME
*"     REFERENCE(I_ALV_CLEAR_KEY) TYPE  FLAG OPTIONAL
*"  EXPORTING
*"     REFERENCE(E_TOTAL_LENGTH) TYPE  I
*"  TABLES
*"      CTBL_ALV_FIELDCAT TYPE  SLIS_T_FIELDCAT_ALV OPTIONAL
*"      CTBL_DYN_CATALOG STRUCTURE  LVC_S_FCAT OPTIONAL
*"  CHANGING
*"     REFERENCE(DYN_WA) TYPE REF TO  DATA
*"     REFERENCE(DYN_TABLE) TYPE REF TO  DATA
*"----------------------------------------------------------------------


  FIELD-SYMBOLS :   &lttbl_output> TYPE STANDARD TABLE,
                    &ltwa_output>  TYPE ANY.

  DATA: new_line  TYPE REF TO data,  " dereference &ltfs>
        new_table TYPE REF TO data.

* Dynamic field description of a structure
  DATA: ref_descr TYPE REF TO cl_abap_structdescr,
        wa_comp TYPE abap_compdescr.

* cl_alv_table_create=>create_dynamic_table METHOD
  DATA: it_fieldcat TYPE lvc_t_fcat,
        is_fieldcat LIKE LINE OF it_fieldcat.
* REUSE_ALV FM
  DATA: ct_fieldcat TYPE  slis_t_fieldcat_alv,
        wa_fc LIKE LINE OF ct_fieldcat.


** create working area according to selection
  CREATE DATA new_line TYPE (i_struct_name).
  ASSIGN new_line->* TO &ltwa_output>.

* create table-> fill catalog
  ref_descr ?= cl_abap_typedescr=>describe_by_data( &ltwa_output> ).

  LOOP AT ref_descr->components INTO wa_comp.

    MOVE:
       wa_comp-name  TO is_fieldcat-fieldname,
       wa_comp-name  TO is_fieldcat-ref_field,
       i_struct_name TO is_fieldcat-ref_table.
    APPEND is_fieldcat TO it_fieldcat.
    ADD wa_comp-length TO e_total_length.
*     Catalog for REUSE_ALV_GRID_DISPLAY
    MOVE-CORRESPONDING is_fieldcat TO wa_fc.

    wa_fc-fieldname   = is_fieldcat-fieldname.
    wa_fc-fieldname   = is_fieldcat-ref_field.
    wa_fc-ref_tabname = is_fieldcat-ref_table.
*   If the key is cleared, the order of columns are not changed
    IF NOT i_alv_clear_key IS INITIAL.
      CLEAR wa_fc-key.
    ENDIF.
    APPEND wa_fc TO ct_fieldcat.
  ENDLOOP.


  CALL METHOD cl_alv_table_create=>create_dynamic_table
   EXPORTING
     it_fieldcatalog = it_fieldcat
   IMPORTING
     ep_table        = new_table.

  ASSIGN new_table->* TO &lttbl_output>.

* Return
  ctbl_alv_fieldcat[] = ct_fieldcat[].   " ALV use
  ctbl_dyn_catalog[]  = it_fieldcat[].   " create_dynamic_table reuse
* dyn_table[] = &lttbl_output>.  " generates a dump, &ltfs> not assigned
* dyn_wa = &ltwa_output>.        " when calling the FM
  dyn_table = new_table.
  dyn_wa = new_line.


ENDFUNCTION.



In the calling program:

*------------
* F I E L D – S Y M B O L S
*------------
FIELD-SYMBOLS : &lttbl> TYPE STANDARD TABLE,
                &ltwa>  TYPE ANY.

*--------------
* D A T A
*--------------
DATA: new_line  TYPE REF TO data,  " dereference &ltfs>
      new_table TYPE REF TO data.

    CALL FUNCTION 'Y_CREATE_DYNAMIC_TABLE'
         EXPORTING
              i_struct_name     = p_tab
              i_alv_clear_key   = 'X'
         TABLES
              ctbl_alv_fieldcat = ct_fieldcat
         CHANGING
              dyn_wa            = new_line
              dyn_table         = new_table.




* Dereference to field symbol

  ASSIGN new_table->* TO &lttb>.
  ASSIGN new_line->* TO &ltwa>.
 

Article source: 
http://searchsap.techtarget.com/tip/Creating-dynamic-tables-in-R-3-46C

Wednesday, October 13, 2010

Difference between Abstract class and interface in layman's terms

A very good explanation of the difference between Abstract class and Interface that I found posted on MSDN Social Forum and a nice image explaining the same concept in different way.


A good way to distinguish between a case for the one or the other for me has always been the following:

1. Are there many classes that can be "grouped together" and described by one noun? If so, have an abstract class by the name of this noun, and inherit the classes from it. (A key decider is that these classes share functionality, and you would never instantiate just an Animal... you would always instantiate a certain kind of Animal: an implementation of your Animal base class)
Example: Cat and Dog can both inherit from abstract class Animal, and this abstract base class will implement a method void Breathe() which all animals will thus do in exactly the same fashion. (I might make this method virtual so that I can override it for certain animals, like Fish, which does not breath the same as most animals).

2. What kinds of verbs can be applied to my class, that might in general also be applied to others? Create an interface for each of these verbs.
Example: All animals can be fed, so I will create an interface called IFeedable and have Animal implement that. Only Dog and Horse are nice enough though to implement ILikeable - I will not implement this on the base class, since this does not apply to Cat.

As said by someone else's reply: the main difference is where you want your implementation. By creating an interface, you can move your implementation to any class that implements your interface.
By creating an abstract class, you can share implementation for all derived classes in one central place, and avoid lots of bad things like code duplication.


Source:  

http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd




Image source: http://www.codeproject.com/KB/architecture/Abstract_Interface/Logical1.jpg  

Monday, October 4, 2010

Program using FUPAREF FM

TYPE-POOLS : slis.

PARAMETERS: p_func LIKE fupararef-funcname. " Name of Function Module

DATA : BEGIN OF i_tab OCCURS 0,
funcname LIKE fupararef-funcname, " Name of Function Module
paramtype LIKE fupararef-paramtype, " Parameter type
pposition LIKE fupararef-pposition, " Internal Table, Current Line Index
optional LIKE fupararef-optional, " Optional parameters
parameter LIKE fupararef-parameter, " Parameter name
defaultval LIKE fupararef-defaultval, " Default value for import parameter
structure LIKE fupararef-structure, " Associated Type of an Interface Parameter
stext LIKE funct-stext, " Short text
END OF i_tab.

DATA: BEGIN OF mtab_new_prog OCCURS 0,
line(172) TYPE c,
END OF mtab_new_prog.

DATA: funcdesc LIKE tftit-stext, " Short text for function module
mylen TYPE i,
myrc TYPE i.

CONSTANTS: myhats(40) VALUE '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'.

TRANSLATE p_func TO UPPER CASE.

SELECT SINGLE
tftit~stext " Short text for function module
INTO funcdesc
FROM tftit
WHERE tftit~funcname = p_func
AND tftit~spras = sy-langu.

TRANSLATE p_func TO LOWER CASE.

CONCATENATE `CALL FUNCTION ` p_func ` " ` funcdesc INTO mtab_new_prog-line.
APPEND mtab_new_prog.

TRANSLATE p_func TO UPPER CASE.

SELECT
fupararef~funcname " Name of Function Module
fupararef~paramtype " Parameter type
fupararef~pposition " Internal Table, Current Line Index
fupararef~optional " Optional parameters
fupararef~parameter " Parameter name
fupararef~defaultval " Default value for import parameter
fupararef~structure " Associated Type of an Interface Parameter
funct~stext " Short text
INTO TABLE i_tab
FROM fupararef
INNER JOIN funct
ON fupararef~funcname = funct~funcname
AND fupararef~parameter = funct~parameter
AND funct~spras = sy-langu
WHERE fupararef~funcname = p_func
AND fupararef~r3state = 'A'
ORDER BY fupararef~paramtype
fupararef~pposition.


LOOP AT i_tab.
AT NEW paramtype.
CASE i_tab-paramtype.
WHEN 'C'.
MOVE ' CHANGING' TO mtab_new_prog-line.
WHEN 'E'.
MOVE ' IMPORTING' TO mtab_new_prog-line.
WHEN 'I'.
MOVE ' EXPORTING' TO mtab_new_prog-line.
WHEN 'T'.
MOVE ' TABLES' TO mtab_new_prog-line.
WHEN 'X'.
MOVE ' EXCEPTIONS' TO mtab_new_prog-line.
ENDCASE.
APPEND mtab_new_prog.
ENDAT.

IF i_tab-optional = 'X'.
mtab_new_prog-line = `*^^^`.
ELSE.
mtab_new_prog-line = `^^^^`.
ENDIF.

IF i_tab-paramtype = 'X'.
MOVE i_tab-pposition TO i_tab-defaultval.
CONDENSE i_tab-defaultval.
ELSE.
TRANSLATE i_tab-parameter TO LOWER CASE.
ENDIF.

CONCATENATE mtab_new_prog-line i_tab-parameter '^=^' INTO mtab_new_prog-line.

IF i_tab-defaultval IS NOT INITIAL.
CONCATENATE mtab_new_prog-line i_tab-defaultval INTO mtab_new_prog-line.
ENDIF.

mylen = STRLEN( mtab_new_prog-line ).

IF mylen < 31.
COMPUTE mylen = 31 - mylen.
ELSE.
MOVE 1 TO mylen.
ENDIF.

TRANSLATE i_tab-structure TO LOWER CASE.

CONCATENATE mtab_new_prog-line myhats+0(mylen) ` " ` i_tab-structure INTO mtab_new_prog-line.

mylen = STRLEN( mtab_new_prog-line ).

IF mylen < 47.
COMPUTE mylen = 47 - mylen.
ELSE.
MOVE 1 TO mylen.
ENDIF.

CONCATENATE mtab_new_prog-line myhats+0(mylen) ` ` i_tab-stext INTO mtab_new_prog-line.

APPEND mtab_new_prog.
ENDLOOP. " LOOP AT I_TAB

CONCATENATE ` . " ` p_func INTO mtab_new_prog-line.
APPEND mtab_new_prog.

LOOP AT mtab_new_prog.
TRANSLATE mtab_new_prog-line USING `^ `.
MODIFY mtab_new_prog.

IF mtab_new_prog = space.
SKIP 1.
ENDIF.
WRITE: / mtab_new_prog.
ENDLOOP. " LOOP AT MTAB_NEW_PROG

* Write the beautiful program code to ClipBoard from internal table
CALL METHOD cl_gui_frontend_services=>clipboard_export
IMPORTING
data = mtab_new_prog[]
CHANGING
rc = myrc.

FM to fetch parameter list of other Function modules

Objective: Dynamically call function modules with the relevant parameters from an input "flat" file 

FM List:

FUPARAREF
F4_FUNCTION_PARAMETER
FUNCTION_IMPORT_DOKU