Access Data types or class definition of other programs in your program
HI Guys,
For past few days I was struggling hard to figure out whether we can use data types or class definitions of other programs in our programs or enhancements without using statement include or copy paste entire code in our source code. At last I have found SAP standard program which answers all my questions.
‘DEMO_CREATE_REFERENCE’ is the program which illustrates this. Here is the sample code.
DATA: CLS_NAME TYPE STRING,
REF TYPE REF TO DATA.
CLS_NAME = ‘\PROGRAM=DEMO_CREATE_REFERENCE\CLASS=CLS’.
CREATE OBJECT REF TYPE REF TO (CLS_NAME).
->with the above statement REF will have type CLASS-CLS of program DEMO_CREATE_REFERENCE.
For eg-
CLS_NAME = ‘\FUNCTION-POOL=MEREQ\CLASS=LCL_REQ_ITEM’
CREATE OBJECT REF TYPE REF TO (CLS_NAME).
->here REF will have type class – LCL_REQ_ITEM of Function group – MEREQ.
I know you guys might wonder how this stuff is put in use or will help. I shall explain my case where I have come across a situation where I need to define a variable with the type of a class used in other program.
MY SITUATION AND SOLUTION I HAVE FOUND-
Somehow with my requirement I have ended up in class CL_TABSTRIP_VIEW_MM with a need to access an attribute ‘ITEM’ of class LCL_REQ_ITEM_STATE which is defined in Function group MEREQ.Though I can see all the required attributes in debug mode, I was unable to fetch them in the source code, as classes are defined in include of function group not inSE24.At last I have found myself a solution, below is the approach and situtaion.
DATA: CLS_NAME TYPE STRING,
REF TYPE REF TO DATA,
LC_MODEL TYPE REF TO IF_MODEL_HOLDER_MM,
LC_GET_MODEL TYPE REF TO IF_MODEL_MM.
FIELD-SYMBOLS:
<REF> TYPE ANY,
<MY_STATE> TYPE ANY,
<ITEM> TYPE MEREQ_ITEM.
**–Here Subviews is defined parameters in method PBO of class CL_TABSTRIP_VIEW_MM
READ TABLE SUBVIEWS INTO lS_SUBVIEW INDEX 16.
IF SY-SUBRC = 0.
LC_MODEL ?= lS_SUBVIEW.
IF LC_MODEL IS BOUND.
LC_GET_MODEL = LC_MODEL->GET_MODEL( ).
**–Here LC_GET_MODEL will have data ref to type class LCL_REQ_ITEM of function pool-MEREQ, So, here is the need to **define a varaible with type ref to a class defined in another program/function pool.
CLS_NAME = ‘\FUNCTION-POOL=MEREQ\CLASS=LCL_REQ_ITEM’
CREATE OBJECT REF TYPE REF TO (CLS_NAME).
ASSIGN REF->* TO <REF>
**–Now, Assign Model to its type and access required attributes
<REF> ?= LC_GET_MODEL
IF <REF> IS ASSIGNED.
ASSIGN (‘<REF>-MY_STATE’) TO <ATTR>.
IF <ATTR> IS ASSIGNED.
ASSIGN (‘<ATTR>-ITEM’) TO <ITEM>.
ENDIF. “IF <ATTR> IS ASSIGNED.
ENDIF. “IF <REF> IS ASSIGNED.
ENDIF. “IF LC_MODEL IS BOUND.
ENDIF. “READ TABLE SUBVIEWS INTO lS_SUBVIEW INDEX 16.
<ITEM> will have the required information which we are looking for.
Brilliant!!! Though quite a lot syntax errors exist in the code above, I get some clues to access purchase order line item data in CL_TABSTRIP_VIEW_MM->PBO. Many thanks!!! 🙂
Hello,
With following block of code (from your post), we cannot activate the program:
DATA: CLS_NAMEÂ TYPE STRING,
REFÂ Â Â Â Â Â Â Â Â Â Â Â Â TYPE REF TO DATA.
CLS_NAME = ‘\PROGRAM=DEMO_CREATE_REFERENCE\CLASS=CLS’.
CREATE OBJECT REF TYPE REF TO (CLS_NAME).
The error message is:Â "REF" must be an object reference, an interface reference or TYPE ANY.
The problem is solved when we edit the code like this:
DATA: CLS_NAMEÂ TYPE STRING,
REFÂ Â Â Â Â Â Â Â Â Â Â Â Â TYPE REF TO OBJECT.
CLS_NAME = ‘\PROGRAM=DEMO_CREATE_REFERENCE\CLASS=CLS’.
CREATE OBJECT REF TYPE (CLS_NAME). "Clear REF TO
But after that we still face a problem: we can't call methods which have required parameters (Import, Export...) of the class. Do you know how to solve this?
Thank you in advance for your help. 🙂