SAP & Oracle partner and support companies

Loading

NEW Operator – Structures and Internal Tables

SAP

NEW Operator – Structures and Internal Tables

NEW operator for internal tables and ABAP structures Can we declare NEW # for d_ref_struct as it appears above? (NEW Operator: Internal Tables and Structures)
D_ref_struct would not recognize matnr and werks because it is an anonymous data object. The mistake is that for the operator “NEW,” no type can be inferred from the context.

The right way is to define d_ref_struct as structured object as shown below. NEW Operator – Structures and Internal Tables

You can also read for :- NEW Operator – Single Value for All Data Types

1. For Structures
i) Anonymous data object

* Example 1
TYPES: BEGIN OF ty_marc,
  matnr TYPE matnr,
  werks TYPE werks_d,
END OF ty_marc.
 
DATA: d_ref_struct TYPE REF TO data.      " Anonymous data object
 
d_ref_struct = new ty_marc( matnr = '165251' werks = '4030' ).
* Example 2
d_ref_struct = NEW #( matnr = '165251' werks = '4030' ).

Might we at any point proclaim NEW # as displayed above for d_ref_struct?
D_ref_struct would not perceive matnr and werks because it is a mysterious information item. The mistake is that the administrator’s “NEW” setting does not yield any results.

The proper approach to describe d_ref_struct is as an organized object that is shown below.

ii) Structured data object

* Example 2
TYPES: 
BEGIN OF ty_marc,
  matnr TYPE matnr,
  werks TYPE werks_d,
END OF ty_marc.

DATA: d_ref_struct TYPE REF TO ty_marc. " Structured data object

d_ref_struct = NEW #( matnr = '165251' werks = '4030' ).

When “#” succeeds NEW, it means the object ref in the Left Hand Side determines the type.

2. For Internal Tables
i) Anonymous data object

* Example 1
TYPES: 
BEGIN OF ty_marc,
  matnr TYPE matnr,
  werks TYPE werks_d,
END OF ty_marc.

DATA: d_ref_tab TYPE REF TO data. " Anonymous data object

d_ref_tab = NEW ty_marc( ( matnr = '165251' werks = '4030')
                         ( matnr = '165251' werks = '5172') ).

Going by the case of designs displayed above before inside table, this model 1 of inward table looks right. In any case, SAP could do without it..

What turned out badly?
NEW ty_marc( ) is pertinent for structure. In this way, can’t switch over completely to interior table.

Lets attempt once more by proclaiming a table kind.

* Example 1 -- first retry
TYPES: 
BEGIN OF ty_marc,
  matnr TYPE matnr,
  werks TYPE werks_d,
END OF ty_marc.

TYPES: tt_marc TYPE TABLE OF ty_marc.

DATA: d_ref_tab TYPE REF TO data. " Anonymous data object

d_ref_tab = NEW tt_marc( ( matnr = '165251' werks = '4030')
                         ( matnr = '165251' werks = '5172') ).

Here we rolled out two improvements.
I) Table Sort tt_marc is characterized.
ii) NEW tt_marc is utilized rather than NEW ty_marc

Yet, still the framework gives mistake “A worth of the nonexclusive kind “TT_MARC” can’t be built”.

How about we offer another chance.
Here we will add only 3 watchwords “WITH DEFAULT KEY” in Table Kind announcement.

* Example 1 -- second retry
TYPES: 
BEGIN OF ty_marc,
  matnr TYPE matnr,
  werks TYPE werks_d,
END OF ty_marc.

TYPES: tt_marc TYPE TABLE OF ty_marc WITH DEFAULT KEY.

DATA: d_ref_tab TYPE REF TO data. " Anonymous data object

d_ref_tab = NEW tt_marc( ( matnr = '165251' werks = '4030')
                         ( matnr = '165251' werks = '5172') ).

Bingooo!!!! WITH DEFAULT KEY got the job done. Unknown information object is currently an inner table.

The following is one more guide to characterize and populate inward table with mysterious item.

* Example 2
TYPES:
begin of ty_marc,
  matnr type matnr,
  werks type werks_d,
end of ty_marc.

DATA: d_ref_tab TYPE TABLE OF REF TO DATA, " Anonymous data object
      wa_marc   TYPE ty_marc.

 SELECT matnr werks UP TO 10 ROWS FROM marc INTO wa_marc.
  APPEND NEW ty_marc( wa_marc ) to d_ref_tab. " Object is created here

The substance of the line is alloted to the information object. The item is made at the assertion Add and the NEW information reference is affixed straightforwardly to an inward table with the suitable line type. The outcome is a table that references generally new unknown information objects of 10 lines.

ii) Structured data object

* Example 3
 TYPES:
 BEGIN OF ty_marc,
   matnr TYPE matnr,
   werks TYPE werks_d,
 END OF ty_marc.

 DATA: d_ref_tab TYPE STANDARD TABLE OF REF TO ty_marc, "Structured object
       wa_marc   TYPE ty_marc.

 SELECT matnr werks UP TO 10 ROWS FROM marc INTO wa_marc.
   APPEND NEW #( wa_marc ) TO d_ref_tab.
 ENDSELECT.

Trust with the above models and pieces, you would be in the situation to mess with NEW Administrator. In our next post we would investigate the Worth Administrator.

YOU MAY LIKE THIS

SAP MM Salary Guide: What You Need to Know

abap ale idoc rfc step by step

What is the demand for the SAP PP module?

× How can I help you?