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: 

ABAP OO, Creating object dynamically

Former Member
0 Kudos

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.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

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

5 REPLIES 5

Former Member
0 Kudos

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

0 Kudos

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

Former Member
0 Kudos

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.

Former Member
0 Kudos

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

0 Kudos

Example worked perfectly (when adjusted to my needs of course )

Cheers and thanks!

Laurens