NEW Operator – Structures and Internal Tables

Can we declare NEW # as shown above for d_ref_struct? (NEW Operator – Structures and Internal Tables)
Since, d_ref_struct is anonymous data object, it would not recognize matnr and werks. The error is: No type can be derived from the context for the operator “NEW”.

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

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?
Since, d_ref_struct is mysterious information object, it wouldn’t perceive matnr and werks. The blunder is: No sort can be gotten from the setting for the administrator “NEW”.

The correct way is to characterize d_ref_struct as organized object as displayed underneath.

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?

X
WhatsApp WhatsApp us