Application Development and Automation 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: 
Read only

Dynamic method

Former Member
0 Likes
1,264

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

1 ACCEPTED SOLUTION
Read only

MarcinPciak
Active Contributor
0 Likes
1,125

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

8 REPLIES 8
Read only

Former Member
0 Likes
1,125

try this w_method = 'detail->CONTACT_DATA_OUT'.

assign <fs_1> to w_method.

call <fs_1>

exporting

importing

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,125

>

> 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 ...

Read only

0 Likes
1,125

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

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,125

My bad ... I should have checked before proposing, anyways i think Marcin has provided you the best solution.

Read only

MarcinPciak
Active Contributor
0 Likes
1,126

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

Read only

0 Likes
1,125

Hi,

Thanks for your suggestion...

Its giving a dumb as METHOD_NAME not in class.

call method detail->('METHOD_NAME').

Thanks,

Krishna

Read only

0 Likes
1,125

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

Read only

Former Member
0 Likes
1,125

Hi,

CALL METHOD detail->(Method_NAME) "Method name

EXPORTING

output = req.

Thanks,

Krishna