Application Development and Automation 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: 
Read only

Creating Objects

sap_cohort
Active Contributor
0 Likes
462

Hi, I have a small table of about 10 materials and I'd like to create 10 abap objects with the names that are stored in the table. I'm not sure how to do this..

Thanks for any help...

3 REPLIES 3
Read only

uwe_schieferstein
Active Contributor
0 Likes
440

Hello Kenneth

You could use class CL_IBASE_R3_MATERIAL to generate your material objects.

Sample coding could look like:

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_CREATE_MATERIAL                                     *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT  zus_sdn_create_material                                     .



TYPES: BEGIN OF ty_s_material.
TYPES:   matnr    TYPE matnr.
TYPES:   obj      TYPE REF TO cl_ibase_r3_material.
TYPES: END OF ty_s_material.
TYPES: ty_t_material  TYPE STANDARD TABLE OF ty_s_material
                      WITH KEY matnr.


DATA:
  gs_material     TYPE ty_s_material,
  gto_material    TYPE ty_t_material.



START-OF-SELECTION.

* Your itab containing the materials
  LOOP AT itab INTO struc.
    CLEAR: gs_material.

    gs_material-matnr = struc-matnr.

*   Create object instances for each material
    CREATE OBJECT gs_material-obj
      EXPORTING
        i_matnr = struc-matnr.

    APPEND gs_material TO gto_material.
  ENDLOOP.


END-OF-SELECTION.

This class has some interesting methods:

- GET_BUSINESS_KEY

- GET_ICON

- GET_MAKT

- GET_MARA

- GET_STANDARD_TEXT

and many others.

Regards

Uwe

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
440

If you don't have that class in your system, you can always use a local class.




report zrich_0001 .

*---------------------------------------------------------------------*
*       CLASS lcl_materials DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_materials definition.

  public section.

    data: matnr type mara-matnr.

    methods: constructor importing im_matnr type mara-matnr.

endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_materials IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_materials implementation.

  method constructor.
    matnr = im_matnr.
  endmethod.

endclass.


data: a_material type ref to lcl_materials.
data: a_material_list type table of ref to lcl_materials.

start-of-selection.

  create object a_material
         exporting im_matnr = 'ABCD'.
  append a_material to a_material_list.

  create object a_material
       exporting im_matnr = 'WXYZ'.
  append a_material to a_material_list.

  loop at a_material_list into a_material.
    write:/ a_material->matnr.
  endloop.

Regards,

RIch Heilman

Read only

sap_cohort
Active Contributor
0 Likes
440

Solution provided partially by responders