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: 

Instance of a sub class

Former Member
0 Kudos

Dear OOPS gurus,

I am trying to instantiate a sub class and getting an error You cannot create an instance of the class "CL_DOC_CRM_ORDER_H" within

the class itself or one of its subclasses. CL_DOC_CRM_ORDER_H is sub class of CL_DOC_CRM_ORDER and when I use create object CL_DOC_CRM_ORDER_H I get above error. Any idea how to instantiate the Sub class?

Thanks

Prasad

12 REPLIES 12

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

CL_DOC_CRM_ORDER_H may be "Create Protected".

Regards,

RIch Heilman

uwe_schieferstein
Active Contributor
0 Kudos

Hello Prasad

If you cannot create the instance using CREATE OBJECT then it is mostly likely that the instantiation of the class is, as Rich mentioned, protected or even private.

In this case there must be a factory method or factory class for creating instances. Thus, simply use the <i>Where-Used-List</i> for class CL_DOC_CRM_ORDER_H and check for (static) methods like <b>CREATE</b> or CREATE_FROM_INTERFACE or similar ones. Perhaps there is a factory class available which usually begin with <b>CF_...</b>

Regards

Uwe

uwe_schieferstein
Active Contributor
0 Kudos

Hello Prasad

I assume that you class implements a similarly named <b>interface</b> (e.g. IF_DOC_CRM_H). If this is true then you have to search the <i>Where-Used-List</i> for this interface because the factory method will most likely return this interface reference.

Regards

Uwe

0 Kudos

Sorry but there is no any Static method in Class CL_DOC_CRM_ORDER_H and yes the class is protected. And also there isn't any interface which has static method?

Do I have to copy this class and change to Public and add a static method create? Any further help is appreciated.

Regards

Prasad

0 Kudos

Hello Prasad

There MUST be a static method which returns instances of this class. If the class itself does not have any static methods then go to SE24, enter the class name and search the <i>Where-Used-List</i> for classes only. Check all displayed methods whether they return an instance of the class.

Regards

Uwe

0 Kudos

I checked that too. Unfortunately there aint any methods which returns Instance class in this one. Neither it exists in its super class. That's the tricky part.

Former Member
0 Kudos

Hi,

Check its access type, if it private and protected u can create instance from outside of the classs.

Regareds

Nagaraju

Former Member
0 Kudos

Hi Prasad,

Looks like from the error message that you are trying to instantiate class <b>CL_DOC_CRM_ORDER_H</b> from within this class. What is the requirement to instantiate it from within? If required, you may, instead, use the <b>me</b> reference.

Hope this helps

Thanks

Sanjeev

0 Kudos

I am using that in the program.

Data : lv_obj type ref to cl_doc_crm_order_h.

When I say

Create object lv_obj.

I get this Error. How do I instantiate the above object.

Thanks

Prasad

Former Member
0 Kudos

Hi Prasad,

First define and implement the subclass CL_DOC_CRM_ORDER_H. Then for creating an instance/object for this subclass we need a reference variable of the type subclass.

so define the reference variable as below.

data: obj1 type ref to CL_DOC_CRM_ORDER_H.

then U can create an instace / object to the class U need.

CREATE OBJECT obj1.

Reward if U find this Useful.

uwe_schieferstein
Active Contributor
0 Kudos

Hello Prasad

You did not mention that you are dealing with <b>persistence</b> classes. In this case it is clear how to instantiate class CL_DOC_CRM_ORDER_H:

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_CL_DOC_CRM_ORDER_H                                  *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT  zus_sdn_cl_doc_crm_order_h                                  .



DATA:
  gd_guid                TYPE crmt_object_guid,
*  go_crm_doc_agent       TYPE REF TO ca_doc_crm_order_h,
  go_crm_doc             TYPE REF TO cl_doc_crm_order_h.


START-OF-SELECTION.


  CALL FUNCTION 'GUID_CREATE'
    IMPORTING
      ev_guid_16       = gd_guid
*     EV_GUID_22       =
*     EV_GUID_32       =
            .


* Create a new document
  TRY.
      CALL METHOD ca_doc_crm_order_h=>agent->create_persistent
        EXPORTING
          i_crm_obj_guid = gd_guid
        RECEIVING
          result         = go_crm_doc.
    CATCH cx_os_object_existing .
  ENDTRY.


* Read an existing document
  TRY.
      CALL METHOD ca_doc_crm_order_h=>agent->get_persistent
        EXPORTING
          i_crm_obj_guid = '<existing guid>'
        RECEIVING
          result         = go_crm_doc.
    CATCH cx_os_object_not_found .
  ENDTRY.


END-OF-SELECTION.

Regards

Uwe

Former Member
0 Kudos

I havent tried this. But will check and let you know.