cancel
Showing results for 
Search instead for 
Did you mean: 

BADI: How to get the value of a property ?

romuald_colletaz
Participant
0 Kudos

Hi all,

I have a BADI where for each rwo of data I need to retrieve a property if the ENTITY (for example CURRENCY property). How can I do this?

thanks,

Romuald

Accepted Solutions (1)

Accepted Solutions (1)

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Here is some sample code showing how to read master data of a dimension. In your case, you would do this code only once at the start of your BAdI. Then you would use a READ statement in your LOOP of CT_DATA, to read the specific record from the master data table, then check the value of the property.

DATA: lo_dataref TYPE REF TO data.
DATA: lo_dim_data TYPE REF TO if_uja_dim_data.
DATA: lo_reader TYPE REF TO if_uja_md_reader.
DATA: lo_query  TYPE REF TO cl_uja_md_query_opt.

FIELD-SYMBOLS:   <lt_md_data> TYPE STANDARD TABLE.

PARAMETERS: p_appset TYPE uja_appset_info-appset_id.
PARAMETERS: p_dim TYPE uja_dimension-dimension.

TRY.

    lo_dim_data = cl_uja_admin_mgr=>create_dim_ref(
                            i_appset_id = p_appset
                            i_dim_name =  p_dim ).

    CREATE OBJECT lo_query
      EXPORTING
        io_dim = lo_dim_data.
    lo_query->select_all_attr( EXPORTING if_inc_non_display = abap_true
                                         if_inc_slt         = abap_true
                                         if_inc_txt         = abap_true
                                         if_inc_generate    = abap_true ).
    lo_query->select_all_hier( ).
    lo_reader = lo_dim_data->get_md_reader( ).
    lo_reader->read( EXPORTING
                         io_read_opt = lo_query
                     IMPORTING
                         er_data = lo_dataref  ).

    ASSIGN lo_dataref->* TO <lt_md_data>.
  CATCH cx_uja_admin_error.
ENDTRY.

Cheers,

Rich Heilman

romuald_colletaz
Participant
0 Kudos

Hi Rich,

thanks it seems tha tI will retrieve all the content of the dimension. Is it possible to use a method whre we define the name of attribue to retrieve and the code of the entity member. Then I will retrieve one unique value.

regards,

Romuald

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

The READ statement inside your LOOP would then be something like this.

field-symbols: <lv_data_entity> type any.
  field-symbols: <lv_md_currency> type any.
  field-symbols: <lth_md_data>   type hashed table.
  field-symbols: <ls_md_data>    type any.

* create work area for master data table and hash table.
* Reading from hash table in the LOOP will be much faster
  create data lo_dataref like line of <lt_md_data>.
  assign lo_dataref->* to <ls_md_data>.
  create data lo_dataref like hashed table of <ls_md_data> with unique key id.
  assign lo_dataref->* to <lth_md_data>.
  <lth_md_data> = <lt_md_data>.

  loop at ct_data assigning <ls_data>.

* Set symbols
    assign component 'ENTITY' of structure <ls_data> to <lv_data_entity>.

* Read master data
    read table <lth_md_data> assigning <ls_md_data> with table key ('ID') = <lv_data_entity>.
    if sy-subrc = 0.
      assign component 'CURRENCY' of structure <ls_md_data> to <lv_md_currency>.
    endif.

....

Endloop.

Hope this helps.

Cheers,

Rich Heilman

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Here is the example again, showing how to select certain attributes, and certain members as well.

DATA: lo_dataref TYPE REF TO data.
DATA: lo_dim_data TYPE REF TO if_uja_dim_data.
DATA: lo_reader TYPE REF TO if_uja_md_reader.
DATA: lo_query  TYPE REF TO cl_uja_md_query_opt.

DATA: lt_attr TYPE uja_t_attr_name.
DATA: ls_attr LIKE LINE OF lt_attr.

DATA: lt_sel TYPE uj0_t_sel.
DATA: ls_sel TYPE uj0_s_sel.

FIELD-SYMBOLS:   <lt_md_data> TYPE STANDARD TABLE.

PARAMETERS: p_appset TYPE uja_appset_info-appset_id.
PARAMETERS: p_dim TYPE uja_dimension-dimension.

TRY.

    lo_dim_data = cl_uja_admin_mgr=>create_dim_ref(
                            i_appset_id = p_appset
                            i_dim_name =  p_dim ).

    CREATE OBJECT lo_query
      EXPORTING
        io_dim = lo_dim_data.


*    lo_query->select_all_attr( EXPORTING if_inc_non_display = abap_true
*                                         if_inc_slt         = abap_true
*                                         if_inc_txt         = abap_true
*                                         if_inc_generate    = abap_true ).

    ls_attr = 'PERIOD'.   " Specify the attribute name
    APPEND ls_attr TO lt_attr.

    lo_query->set_sel_attr(
                EXPORTING
                    it_attr = lt_attr ).


    lo_query->select_all_hier( ).

* Select only specific members.
    CLEAR ls_sel.
    ls_sel-dimension = 'TIME'.
    ls_sel-attribute = 'ID'.
    ls_sel-sign   = 'I'.
    ls_sel-option = 'EQ'.
    ls_sel-low    = '2009.JAN'.
    APPEND ls_sel TO lt_sel.

    lo_query->set_sel(
               EXPORTING
                   it_sel = lt_sel ).

    lo_reader = lo_dim_data->get_md_reader( ).
    lo_reader->read( EXPORTING
                         io_read_opt = lo_query
                     IMPORTING
                         er_data = lo_dataref  ).

    ASSIGN lo_dataref->* TO <lt_md_data>.
  CATCH cx_uja_admin_error.
ENDTRY.

Cheers,

Rich Heilman

Edited by: Rich Heilman on Feb 10, 2012 11:05 AM

romuald_colletaz
Participant
0 Kudos

Hi Rich,

excellent, thany ou so much.

I imagine that it is better to create an internal table with all members (and to restrict to used attributes like you did in your last post) instead of using the code of your last post for each entity in the loop.

kind regards,

Romuald

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Oh yes. You do NOT want to read a specific member from the DB each time through the loop. That's why I said to only read all the master data once into internal table at the beginning of your BAdI method, and then do READ statement against the exact member in the hash table while inside your LOOP.

Cheers,

Rich Heilman

Answers (0)