Skip to Content
author's profile photo Former Member
Former Member

ABAP OO, Creating object dynamically

Hi everybody,

I’m currently working on some Abap OO examples. Everything is working ok except for the following: Creating objects dynamically.

This is what I’d like to do:

Lets say we obtain a bunch of sales orders using a simple select statement and place them in an internal table. For each entry in our internal table, I’d like to generate a new sales order object. This salesorder object is a simple class with a constructor with an Id and some other attributes.

When this is done, I’d like to append every object to a new object, say basket, so that this basket contains something like an internal table within each record a reference to the salesorder. Is this easy to do? I’m using the example that is described in the sap guide (create object pointer type (class_name) but it looks to me that you still have to declare the reference variables before you can create the object.

Hmm, I think Java is better suited for a job like this.

Hope you can help,

Cheers and Best wishes

Laurens Steffers

The Netherlands.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Jan 06, 2005 at 11:57 AM

    Hi,

    Its not all that difficult in ABAP as well. The following code is based on Chapter 5, Listing 5.20 from book ABAP Objects.

    What you need is an internal table of type <your sales object> e.g.

    DATA: BEGIN OF obj_sales_order
            sales_order_no LIKE <sales_order_no_type>,
            <...>,
            objref TYPE REF TO <SALES_ORDER_CLASS>
          END OF obj_sales_order,
          
          reftab LIKE SORTED TABLES OF obj_sales_order
                      WITH UNIQUE KEY sales_order_no 
                                      <other key attributes>.
    

    Now all you need to do is to do a SELECT...ENDSELECT loop, and:

    SELECT ...
    obj_sales_order-sales_order_no = <selection result>
    obj_sales_order-<other key attr> = <selection result>
    
    READ TABLE reftab INTO obj_sales_order
                      FROM obj_sales_order.
    
    IF sy-subrc <> 0.
      CREATE OBJECT obj_sales_order-objref
             EXPORTING ...
             EXCEPTIONS ...
      IF sy-subrc <> 0.
        MESSAGE ...
        CONTINUE.
      ELSE.
        INSERT obj_sales_order INTO TABLE reftab.
      ENDIF.
    ENDIF.
    CALL METHOD obj_sales_order-objref-><method>.
    ENDSELECT.
    

    Hope this help.

    Regards

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 06, 2005 at 11:24 AM

    If you are working on WAS 6.4, then this link would be very useful for you:

    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/documents/a1-8-4/advanced and generic programming in abap.pdf

    Regards,

    Subramanian V.

    P.S. - This question has been asked previously as well

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hello Subramanian,

      thank you for the link. Funny thing is that, with the example of dynamic tokens, I didn't use the uppercase in the following statement.

      CALL METHOD: field->create_object EXPORTING class_name = 'airplane'

      IMPORTING pointer = r .

      Changing it to AIRPLANE, the creation went ok. I think I'll go from here using the whitepaper you've sent me.

      I was not aware of the previous posting. I did a search but looking at all the 1400 messages... 😉

      Thanks again!

      Laurens

  • Posted on Jan 06, 2005 at 11:43 AM

    Hi,

    Below is a sample code. You may create an internal table for your object and append the objects for salesorders to it.

    data:

    begin of itabobjects,

    salesorder type ref to cl_sales_order,

    end of itab.

    loop at itabsales.

    create object itabobjects-salesorder

    importing vbeln = itabsales-vbeln.

    append itabobjects.

    endloop.

    -


    Please, give points if it helps.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.