Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Insert Workarea Into Table with Parameters - Problem

0 Kudos

Hey,

My sample Code

Workarea/Table (Data)

DATA: go_salv     TYPE REF TO cl_salv_table,
      go_function TYPE REF TO cl_salv_functions_list,
      
      BEGIN OF wa,
        number   TYPE string,
        id       TYPE string,
        location TYPE string,
        password TYPE string,
        user     TYPE string,
      END OF wa,
      lt_data LIKE TABLE OF wa.


Insert some sample data

wa-number = '1234'.
wa-id = 'Test 1'.
wa-location = 'Test Location 1'.
wa-password = 'Test Password 1'.
wa-user = 'Test User 1'.

INSERT wa INTO TABLE lt_data.


wa-number = '5678'.
wa-id = 'Test 2'.
wa-location = 'Test Location 2'.
wa-password = 'Test Password 2'.
wa-user = 'Test User 2'.

INSERT wa INTO TABLE lt_data.


wa-number = '9999'.
wa-id = 'Test 3'.
wa-location = 'Test Location 3'.
wa-password = 'Test Password 3'.
wa-user = 'Test User 3'.

INSERT wa INTO TABLE lt_data.


Parameter (Selection-Screen)

SELECTION-SCREEN BEGIN OF BLOCK b1.
PARAMETERS: bu_numb TYPE string.
SELECTION-SCREEN END OF BLOCK b1.


Display ALV Grid

IF wa-number = bu_numb.

* Displaying ALV Grid
  TRY.
      CALL METHOD cl_salv_table=>factory(
        IMPORTING
          r_salv_table = go_salv
        CHANGING
          t_table      = lt_data
      ).

      go_function = go_salv->get_functions( ).
      go_function->set_default( abap_true ).
      go_salv->display( ).

    CATCH cx_salv_msg.

  ENDTRY.
ELSE.
  MESSAGE 'Not found' TYPE 'E'.
ENDIF.


My Problem: 1234 and 5678 are showing Not found Error message, and 9999 is showing all three entries, but i want to show each entry if it matches the number.

If there is another way instead of writing
wa-number = '1234'.
wa-id = 'Test 1'.
wa-location = 'Test Location 1'.
wa-password = 'Test Password 1'.
wa-user = 'Test User 1'.

INSERT wa INTO TABLE lt_data. 
three times, I would also be happy if someone has an idea. Thanks in advance ; )
1 ACCEPTED SOLUTION

michael_piesche
Active Contributor

This coding will help you solve your problem based on your requirement description.

There might of course be more and better solutions, but for that all requirements need to be known.

"IF wa-number = bu_numb. " => You are only comparing against the wa value
                         "    which contains data of the last record
                         "    this way, you do not check against all values of lt_data

" the following would work based on your requirement description
CLEAR lt_data2.
LOOP AT lt_data INTO wa WHERE number = bu_numb.
  INSERT wa INTO TABLE lt_data2. " lt_data2 is like lt_data, but only will contain data of bu_numb numbers
ENDLOOP.

IF lt_data2[] IS NOT INITIAL. " display of mapping records
* Displaying ALV Grid
  TRY.
      CALL METHOD cl_salv_table=>factory(
        IMPORTING
          r_salv_table = go_salv
        CHANGING
          t_table      = lt_data2
      ).

      go_function = go_salv->get_functions( ).
      go_function->set_default( abap_true ).
      go_salv->display( ).

    CATCH cx_salv_msg.

  ENDTRY.
ELSE.
  MESSAGE 'Not found' TYPE 'E'.
ENDIF.
5 REPLIES 5

michael_piesche
Active Contributor

This coding will help you solve your problem based on your requirement description.

There might of course be more and better solutions, but for that all requirements need to be known.

"IF wa-number = bu_numb. " => You are only comparing against the wa value
                         "    which contains data of the last record
                         "    this way, you do not check against all values of lt_data

" the following would work based on your requirement description
CLEAR lt_data2.
LOOP AT lt_data INTO wa WHERE number = bu_numb.
  INSERT wa INTO TABLE lt_data2. " lt_data2 is like lt_data, but only will contain data of bu_numb numbers
ENDLOOP.

IF lt_data2[] IS NOT INITIAL. " display of mapping records
* Displaying ALV Grid
  TRY.
      CALL METHOD cl_salv_table=>factory(
        IMPORTING
          r_salv_table = go_salv
        CHANGING
          t_table      = lt_data2
      ).

      go_function = go_salv->get_functions( ).
      go_function->set_default( abap_true ).
      go_salv->display( ).

    CATCH cx_salv_msg.

  ENDTRY.
ELSE.
  MESSAGE 'Not found' TYPE 'E'.
ENDIF.

0 Kudos

Thanks, man : )

Just another quick question.

Is there an easier way instead of writing

wa-number = '1234'.
wa-id = 'Test X'.
wa-location = 'Test Location X'.
wa-password = 'Test Password X'.
wa-user = 'Test User X'.

INSERT wa INTO TABLE lt_data. 

three times, e. g. in ABAP 7.40 ??

0 Kudos

newuser1206, no, not in ABAP 7.40. In ABAP 7.5x there are further options to reduce the amount of code lines for tasks like this.

new user This code is possible since 7.40: (search Constructor Expressions/Constructor Operators for more information):

lt_data = VALUE #(
( number = '1234' id = 'Test X' location = 'Test Location X' password = 'Test Password X' user = 'Test User X' )
( number = '5678' id = 'Test 2' location = 'Test Location 2' password = 'Test Password 2' user = 'Test User 2' )
( number = '9999' id = 'Test 3' location = 'Test Location 3' password = 'Test Password 3' user = 'Test User 3' )
).

0 Kudos

Sandra Rossi, thank you for the update. You are right, the VALUE operator as well as NEW, LET, CONV, CAST where indeed introduced with ABAP 7.40. So yes new user, you can already use the VALUE operator in your system, in order to 'simplify' the insert of rows into the table.