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: 

Retrieve instance( object ) from table

Former Member
0 Kudos

Hi All,

I have a table m_models which has two fields, model_id and instance. Model id is a string and instance is an instance of a class, it can be either workitem class - ZWorkitem, or invoice classs zinvoice class.

Now both of above classes are inherited from cl_bsp_controller2, and some of their data is different.

Now I want to retrieve instance of the class from table when model id is invoice, but i am not able to speficy the class type for the object that should store the instance, if i use cl_bsp_controller2 i miss some data and when i use zinvoice it gives me another error.

Can you please advise?

I can provide more details of the issue if you want.

1 REPLY 1

naimesh_patel
Active Contributor
0 Kudos

You can try like:


data: lo_cont type ref to cl_bsp_controller2,
      lo_inv type ref to zcl_invoice.
DATA: lo_type TYPE REF TO cl_abap_typedescr,
      lo_cast_error TYPE REF TO cx_sy_move_cast_error.

loop at t_models into la_models.
* Get the absolute name from object ref.
  lo_cont = la_models-object.
  lo_type = cl_abap_classdescr=>describe_by_object_ref( lo_cont ).

* attribute ABSOLUTE_NAME of the LO_TYPE will give you the exact type
* Use this attribute to get to know your actual class.
  try.
      lo_inv ?= lo_cont.
   CATCH cx_sy_move_cast_error INTO lo_cast_error.
  entry.
endloop.  

Regards,

Naimesh Patel