Call transaction when double clicked on ALV line - abap

I currently displaying a table as an ALV as follows:
DATA: alv TYPE REF TO cl_salv_table,
output_table TYPE TABLE OF output_table.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = alv
CHANGING
t_table = output_table ).
CATCH cx_salv_msg INTO DATA(msg).
cl_demo_output=>display( msg ).
ENDTRY.
alv->display( ).
My output table contains a material number as well as the plant.
Example:
Material
Plant
...
123456789
0001
...
999999999
0002
...
I want that transaction MM03 (with material and plant and view Accounting 1) is called when clicked on one line in the ALV table.
I've found some solutions on the internet but those are not quite working for me.
Do you have some clues on how to proceed with this topic?
What I tried was:
DATA(o_alv) = cl_salv_gui_table_ida=>create( iv_table_name = 'SFLIGHT' ).
o_alv->display_options( )->enable_double_click( ).
SET HANDLER lcl_events=>on_double_click FOR o_alv->display_options( ).
[...]
but my table is not an internal table of SAP.

Before you create your ALV, make sure to define your event handler class.
Assuming your output_table is global and has the fields MATNR and WERKS, it could look like this:
* define class for event handling
CLASS lcl_events DEFINITION.
PUBLIC SECTION.
CLASS-METHODS: on_double_click FOR EVENT double_click OF cl_salv_events_table
IMPORTING
row
column
sender.
ENDCLASS.
CLASS lcl_events IMPLEMENTATION.
METHOD on_double_click.
" Open Accounting 1 view in MM03
SET PARAMETER ID 'MAT' FIELD output_table[ row ]-matnr .
SET PARAMETER ID 'WRK' FIELD output_table[ row ]-werks .
SET PARAMETER ID 'MXX' FIELD 'B' .
CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN .
ENDMETHOD.
ENDCLASS.
After calling cl_salv_table=>factory( ), register your event handler for double click:
SET HANDLER lcl_events=>on_double_click FOR alv->get_event( ).

Related

Display only certain fields in ALV

My table has around 300 columns and I want to display only 10 out of them with specifying which ones. I am working with CL_SALV_TABLE.
Can anybody help me with this problem or give me a hint? Thank you very much in advance!
You need to use the method SET_VISIBLE of the Column object (class CL_SALV_COLUMN). For more information, see the chapter "Set the Visibility of the Column" of page "Columns (General)".
This Minimal reproducible example shows only the columns SPRAS and LAISO from the table T002, all other ones are hidden:
DATA: t002_lines TYPE TABLE OF t002,
salv TYPE REF TO cl_salv_table,
columns TYPE salv_t_column_ref.
FIELD-SYMBOLS <column> TYPE salv_s_column_ref.
SELECT * FROM t002 INTO TABLE t002_lines.
CALL METHOD cl_salv_table=>factory
IMPORTING
r_salv_table = salv
CHANGING
t_table = t002_lines.
LOOP AT salv->get_columns( )->get( ) ASSIGNING <column>.
CASE <column>-columnname.
WHEN 'SPRAS' OR 'LAISO'.
<column>-r_column->set_visible( if_salv_c_bool_sap=>true ).
WHEN OTHERS.
<column>-r_column->set_visible( if_salv_c_bool_sap=>false ).
ENDCASE.
ENDLOOP.
salv->display( ).

Order of ALV columns (with CL_SALV_TABLE)

I have a program which displays a simple table based on the class CL_SALV_TABLE, and I'd like to position the column CURRCODE at the fifth position, instead of 4 currently (default order defined in database table SCARR), as shown here:
How can I do that?
Here is the current source code (compiles with ABAP version 7.40) :
SELECT * FROM scarr INTO TABLE #DATA(scarr_s).
cl_salv_table=>factory(
IMPORTING
r_salv_table = DATA(salv)
CHANGING
t_table = scarr_s ).
salv->display( ).
NB: if you want to reproduce and the table SCARR is empty, execute the program SAPBC_DATA_GENERATOR to fill it.
You have to do it in two steps, before the call of the method display :
Call the method GET_COLUMNS of the ALV instance (of class CL_SALV_TABLE), to get the instance of the class CL_SALV_COLUMNS_TABLE corresponding to all columns.
The latter class has a method SET_COLUMN_POSITION to change the position of a given column whose name is passed as an argument.
Here is the source code:
SELECT * FROM scarr INTO TABLE #DATA(scarr_s).
cl_salv_table=>factory(
IMPORTING
r_salv_table = DATA(salv)
CHANGING
t_table = scarr_s ).
salv->get_columns( )->set_column_position( columnname = 'CURRCODE' position = 5 ). " <== ADD THIS LINE
salv->get_columns( )->set_optimize( ).
salv->display( ).
Result:
NB: I also use the method SET_OPTIMIZE so that the width of all columns is automatically adjusted to their contents (but it's not related to the question).

Display an internal table?

I have to read some data from a table and display it. The program starts but I don't know how to display any of the data I've selected. I want to put it out as a table.
I honestly don't even know if the following code is correct.
REPORT ZT_THIEMANN_TEST.
types : begin of ts_output,
object_id type CRMD_ORDERADM_H-object_id,
created_by type CRMD_ORDERADM_H-created_by,
end of ts_output,
tt_output type table of ts_output.
PARAMETERS Mel_Nr TYPE CRMD_ORDERADM_H-Object_ID obligatory.
data gt_output type tt_output.
START-OF-SELECTION.
SELECT cm~object_id cm~created_by
from CRMD_ORDERADM_H as cm
into corresponding fields of table gt_output
where cm~object_id like Mel_Nr.
As Sandra said, you can check if your code/the select works by using the debugger.
You can output data different ways, but the easiest is using the class CL_SALV_TABLE. Without adding any additional features (such as a title, toolbar buttons, sorting, hotspots, etc.), the below code is how you can display your data using the oo alv grid.
...
DATA: go_alv TYPE REF TO cl_salv_table,
gx_salv_msg TYPE REF TO cx_salv_msg.
...
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = go_alv
CHANGING
t_table = gt_output ).
CATCH cx_salv_msg INTO gx_salv_msg.
MESSAGE 'error' TYPE 'E'.
ENDTRY.
go_alv->display( ).
If you need a real one-liner, just use ABAP demo output standard class cl_demo_output that can handle any type including internal tables:
SELECT *
FROM scarr
INTO TABLE #DATA(carriers).
cl_demo_output=>display( carriers ).

Create dynamic ABAP internal table

On selection screen, the user needs to insert a table name, and I need to get first 3 fields from that table and display them in an ALV for the output. What I understand from reading tutorials is that I need to call method cl_alv_table_create=>create_dynamic_table, but I don't know how to create the fieldcatalog.
DATA: t_newtable TYPE REF TO data,
t_fldcat TYPE lvc_t_fcat,
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = t_fldcat
IMPORTING
ep_table = t_newtable.
I assume that the table name which user enters is a data dictionary table (like SFLIGHT). If yes, then you can generate the field catalog as follows.
data : it_tabdescr type abap_compdescr_tab,
wa_tabdescr type abap_compdescr.
data : ref_table_descr type ref to cl_abap_structdescr.
ref_table_descr ?= cl_abap_typedescr=>describe_by_name( p_table ).
it_tabdescr[] = ref_table_descr->components[].
loop at it_tabdescr into wa_tabdescr.
clear wa_fieldcat.
wa_fieldcat-fieldname = wa_tabdescr-name .
wa_fieldcat-datatype = wa_tabdescr-type_kind.
wa_fieldcat-inttype = wa_tabdescr-type_kind.
wa_fieldcat-intlen = wa_tabdescr-length.
wa_fieldcat-decimals = wa_tabdescr-decimals.
append wa_fieldcat to it_fieldcat.
endloop.
Here, "p_table" is the selection screen parameter containing the table
name.

ABAP Display field symbol dynamic in alv

I am pasting this program for example but i will never know the type of the table (here vbap and vbak).
My goals is to display my field symbol without knowing the types.
Is it possible ?
Here is my code :
REPORT ZTEST_FME_FOL.
type-pools slis .
FIELD-SYMBOLS : <mytable> TYPE ANY TABLE.
DATA : lv_alv_table TYPE REF TO cl_salv_table,
lv_funct TYPE REF TO cl_salv_functions,
lv_columns TYPE REF TO cl_salv_columns_table,
lv_column TYPE REF TO CL_SALV_COLUMN_table.
SELECT * from vbap INNER JOIN VBAK ON vbap~vbeln = vbak~vbeln UP TO 10 ROWS INTO TABLE <mytable>.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = lv_alv_table
CHANGING
t_table = <mytable> ).
CATCH cx_salv_msg .
ENDTRY.
lv_funct = lv_alv_table->get_functions( ).
lv_funct->set_all( Abap_True ).
lv_columns = lv_alv_table->get_columns( ).
lv_alv_table->display( ).
Thanks in advance !
Depending on what you 're trying to do there's going to be more validation required than what I've done, but in essence this is what you need.
Using (dynamic) joins may be particularly tricky.
report zevw_test_dynamic_alv.
parameters: p_table type string obligatory.
field-symbols: <gt_table> type standard table.
data: gt_data type ref to data.
start-of-selection.
create data gt_data type table of (p_table).
assign gt_data->* to <gt_table>.
select * from (p_table) up to 10 rows
into table <gt_table>.
perform display_results using <gt_table>. "Your ALV stuff will be in here
You may even have to build the fieldcat manually and then use
call method cl_alv_table_create=>create_dynamic_table
exporting
it_fieldcatalog = gt_fieldcat[]
importing
ep_table = gt_data.
to get the data reference