‎2010 May 04 10:41 AM
Hi SAP Gurus,
Is it is possible to pass the name of method during run time, actually I have 10 methods with same import, export
&
I need to make one single report which will get the name of method during run time.
CALL METHOD detail->CONTACT_DATA_OUT "this name should be changed during run time.
EXPORTING
output = req.
Thanks,
Krishna
‎2010 May 04 11:10 AM
To check if method exists:
DATA: r_classdescr TYPE REF TO cl_abap_classdescr.
r_classdescr ?= cl_abap_typedescr=>describe_by_data( detail ).
READ TABLE r_classdescr->methods WITH KEY name = 'METHOD_NAME' TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
WRITE: 'Method', pa_meth, 'exists'.
ELSE.
WRITE: 'Method', pa_meth, 'doesn''t exist'.
ENDIF
To dynamicaly call a method
call method detail->('METHOD_NAME').
Regards
Marcin
‎2010 May 04 10:44 AM
try this w_method = 'detail->CONTACT_DATA_OUT'.
assign <fs_1> to w_method.
call <fs_1>
exporting
importing
‎2010 May 04 10:47 AM
>
> try this w_method = 'detail->CONTACT_DATA_OUT'.
>
> assign <fs_1> to w_method.
>
> call <fs_1>
> exporting
> importing
Why do you need the ASSIGN statement ?
You can :
w_method = `detail->CONTACT_DATA_OUT`.
call method w_method
exporting ...
importing ...
‎2010 May 04 11:10 AM
Hi,
I have done that before, but the same error is coming.
The syntax for a method specification is "objref->method" or
"class=>method". "class=>method". "class=>method". "class=>method".
Thanks,
Krishna
‎2010 May 04 11:24 AM
My bad ... I should have checked before proposing, anyways i think Marcin has provided you the best solution.
‎2010 May 04 11:10 AM
To check if method exists:
DATA: r_classdescr TYPE REF TO cl_abap_classdescr.
r_classdescr ?= cl_abap_typedescr=>describe_by_data( detail ).
READ TABLE r_classdescr->methods WITH KEY name = 'METHOD_NAME' TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
WRITE: 'Method', pa_meth, 'exists'.
ELSE.
WRITE: 'Method', pa_meth, 'doesn''t exist'.
ENDIF
To dynamicaly call a method
call method detail->('METHOD_NAME').
Regards
Marcin
‎2010 May 04 11:54 AM
Hi,
Thanks for your suggestion...
Its giving a dumb as METHOD_NAME not in class.
call method detail->('METHOD_NAME').
Thanks,
Krishna
‎2010 May 04 12:09 PM
Sorry for ridiculous question, but did you pass your method name or just used that one literally : 'METHOD_NAME' ?
Refer this snippet:
PARAMETERS pa_meth(61) TYPE c.
CLASS lcl_test DEFINITION.
PUBLIC SECTION.
METHODS: start, end.
ENDCLASS.
CLASS lcl_test IMPLEMENTATION.
METHOD start.
WRITE: 'This is the START method called dynamically'.
ENDMETHOD. "start
METHOD end.
WRITE: 'This is the END method called dynamically'.
ENDMETHOD. "end
ENDCLASS.
START-OF-SELECTION.
DATA: r_test TYPE REF TO lcl_test,
r_classdescr TYPE REF TO cl_abap_classdescr.
r_classdescr ?= cl_abap_typedescr=>describe_by_name( 'LCL_TEST' ).
READ TABLE r_classdescr->methods WITH KEY name = pa_meth TRANSPORTING NO FIELDS.
IF sy-subrc <> 0.
WRITE: 'Method', pa_meth, 'doesn''t exist'.
ELSE.
CREATE OBJECT r_test.
CALL METHOD r_test->(pa_meth).
ENDIF.
Regards
Marcin
‎2011 Aug 16 1:50 PM
Hi,
CALL METHOD detail->(Method_NAME) "Method name
EXPORTING
output = req.
Thanks,
Krishna