0% found this document useful (0 votes)
126 views

What Is The Use of Aliases?

This document discusses how to create and use persistent objects in ABAP. It describes creating a database table to store persistent object references, defining a persistent class with mapping to the database table, and writing code to create, retrieve, and delete persistent object instances stored in the database using their business key attributes as an identity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views

What Is The Use of Aliases?

This document discusses how to create and use persistent objects in ABAP. It describes creating a database table to store persistent object references, defining a persistent class with mapping to the database table, and writing code to create, retrieve, and delete persistent object instances stored in the database using their business key attributes as an identity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

What is the use of aliases?

ALIASES:

This is the aliases name. it is only for interfaces.

Go to se24.

Then go to aliases tab.

Then provide another name for the interface method.

Then provide public.

Save it, check it, activate it.

Then go to SE38.

Change the method name also.

*&---------------------------------------------------------------------*
*& Report ZCL_INTERFACE *
*& *
*&---------------------------------------------------------------------*
REPORT ZCL_INTERFACE .
*provide mara table
DATA: MARA TYPE MARA.
*provide data objects
DATA: OBJ TYPE REF TO ZCL_INTERFACE,
IT_MARA TYPE Z_MARA,
WA_MARA TYPE MARA.
*provide selection screen
SELECT-OPTIONS: S_MATNR FOR MARA-MATNR.
*provide object
START-OF-SELECTION.
CREATE OBJECT OBJ.
*call the method.
* CALL METHOD OBJ->ZIF_INTERFACE~SELECT_METHOD
CALL METHOD OBJ->SEL
EXPORTING
P_MATNR_LOW = S_MATNR-LOW
P_MATNR_HIGH = S_MATNR-HIGH
IMPORTING
IT_MARA = IT_MARA
WA_MARA = WA_MARA.
*display the data
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR,
WA_MARA-ERSDA,
WA_MARA-ERNAM,
WA_MARA-MATKL,
WA_MARA-MEINS.
ENDLOOP.

The output would be as shown below:.


Creating a global class from a local class
In this tutorial, we would look into the procedure of creating a global class using a local
class defined in a program.

Consider the following Z program, which contains a local class:

REPORT zclass_test.
*---------------------------------------------------------*
* CLASS zcl_test DEFINITION
*---------------------------------------------------------*
*
*---------------------------------------------------------*
CLASS zcl_test DEFINITION.
PUBLIC SECTION.
METHODS: display.
ENDCLASS. "zcl_test DEFINITION
*--------------------------------------------------------*
* CLASS zcl_test IMPLEMENTATION
*--------------------------------------------------------*
*
*--------------------------------------------------------*
CLASS zcl_test IMPLEMENTATION.
METHOD display.
WRITE: 'SAPTechnical.com'.
ENDMETHOD. "display
ENDCLASS. "zcl_test IMPLEMENTATION

Now let us create a global class SE24 using the above local class:

Go to transaction SE24.
Following pop-up appears:

Enter your Z program in which the local class is defined and press ENTER.
The class name defined in our program is ZCL_TEST and the proposed global class name is
CL_ZCL_TEST. Now you can rename the global class name as per your requirement.

If the local class is defined inside an include, we need to check the checkbox “Explode
INCLUDEs‟.

Now click on import. Following message would appear:

Now check the global class ZCL_TEST.


Create Transaction for local class method
In this demo I am going to show how to create transaction on a local class method.

Step1: First create a local class in a report from transaction SE38.


REPORT z_demo_oop_jg .
*---------------------------------------------------------------------*
* CLASS create_report DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS create_report DEFINITION.
PUBLIC SECTION.
METHODS: main.
PRIVATE SECTION.
DATA: i_data TYPE STANDARD TABLE OF sbook INITIAL SIZE 0.
METHODS: fetch_data,
display_data.
ENDCLASS. "create_report DEFINITION
*---------------------------------------------------------------------*
* CLASS create_report IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS create_report IMPLEMENTATION.
METHOD fetch_data.
* Select 100 records from SBOOK table
SELECT * FROM sbook
INTO TABLE i_data
UP TO 100 ROWS.
ENDMETHOD. "fetch_data
METHOD display_data.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_structure_name = 'SBOOK'
TABLES
t_outtab = i_data
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDMETHOD. "display_data
METHOD main.
fetch_data( ).
display_data( ).
ENDMETHOD. "main
ENDCLASS. "create_report IMPLEMENTATION
Step2. Now from transaction SE93 create a transaction for the method MAIN as shown in
the screen shots given below:

Give a transaction name and press create button.

In the next screen give a description and choose the proper radio button

In the next screen provide report name (where the local class is defined), local class name
and method name.
Now save the
transaction and execute it.
In this case it will display the report.

This technique can be used to call a method (local class) from another program using
statement: call transaction.

EX: call transaction 'Z_OOP'.

Note: In the same way you can create a transaction on method of a global class.
Persistent Objects: A Quick Reference

Objective
To store references to the persistent object persistently in the database.

Step: 1 -> Create a database table

This table should contain 2 fields of type OS_GUID in addition to the GUID object attribute.
The first field is used to store the instance GUID while the other is used to store the class
GUID.

Step: 2 ->Create a Persistent Class

In the next screen select the class type as Persistent Class and then hit Save Button.
Step: 3 -> Persistent Mapping or Mapping

Goto->Persistence Representation

Give the table name. For e.g. ZSTUDENT03 and hit the enter button
Table fields appear in the lower half of the tool screen. Double Click the table field and press
the button. Add the remaining fields.

While adding the field INST_GUID choose the assignment type as Object reference and for
the attribute type specify the class name for e.g. ZCL_PERSIST_03
To assign a class indicator, select the corresponding table field of type OS_GUID by double-
clicking. Enter the name of the reference attribute for the attribute name.

Screen looks like below. Press Save.

Activate the Class. Press the Yes Button to activate the class actor as well.

Write a Program to create the persistent object.


Source Code excerpt:

DATA: AGENT TYPE REF TO ZCA_PERSIST_03,


STUDENT TYPE REF TO ZCL_PERSIST_03,
REF1 TYPE REF TO OBJECT.
DATA: SNO LIKE ZSTUDENT04-SNO VALUE '1000',
SNAME LIKE ZSTUDENT04-SNAME VALUE 'HAKIM',
MARK1 LIKE ZSTUDENT04-MARK1 VALUE '100',
MARK2 LIKE ZSTUDENT04-MARK2 VALUE '100'.
AGENT = ZCA_PERSIST_03=>AGENT.
TRY.
CALL METHOD AGENT->CREATE_PERSISTENT
EXPORTING
* I_INST_GUID =
I_MARK1 = MARK1
I_MARK2 = MARK2
I_SNAME = SNAME
I_SNO = SNO
* RECEIVING
* RESULT =
.
COMMIT WORK.
CATCH CX_OS_OBJECT_EXISTING .
ENDTRY.

Go to SE16 and check the entries.

Store the Persistent Object Reference in the database.

Source Code excerpt.

TRY.
CALL METHOD AGENT->IF_OS_CA_PERSISTENCY~GET_PERSISTENT_BY_OID
EXPORTING
I_OID = '30EA9E25999F0843BE6F7B86063F2916'
RECEIVING
RESULT = REF1
.
CATCH CX_OS_OBJECT_NOT_FOUND .
CATCH CX_OS_CLASS_NOT_FOUND .
ENDTRY.
STUDENT ?= REF1.
STUDENT->SET_INST_GUID( STUDENT ).
COMMIT WORK.

Go to SE16 and check the entries.


Persistent Objects: Using Business Key Identity
Objective

To Store the attributes of the Objects persistently in the database.

Step: 1 ->Create a Persistent Class

Go to Class Builder (TCode SE24)

Give persistent class name for e.g. ZCL_PERSIST_01 and hit the create button

In the next screen select the class type as Persistent Class and then hit Save Button.
Step: 2 -> Persistent Mapping or Mapping

Utilities->Persistence Representation

Give the table name. For e.g. ZSTUDENT01 and hit the enter button

Table fields appear below the mapping screen.


Double Click the table field and then press the upward arrow button

Add the remaining fields as well. Screen looks like this now.

Activate the Class. Press the Yes Button to activate the class actor as well.
Step: 3 -> Write a Program to create / fetch / delete the Persistent Object

Our Program Selection-Screen looks like below

Here I am creating a new student. Specify the value and hit the execute button.

Output:
Go to SE16 and check the entries

Source Code
*&---------------------------------------------------------------------*
*& Report Z_GET_PERSISTENT
*& Published @ SAPTechnical.com
*&---------------------------------------------------------------------*
*&Author : Abdul Hakim
*&Development Language: ABAP
*&System Release: SAP Netweaver 2004
*&Title: Persistent Object using Business Key Object Identity!!
*&---------------------------------------------------------------------*
REPORT Z_GET_PERSISTENT.
selection-screen begin of block blk1 with frame title tit1.
parameters: sno like zstudent01-sno obligatory,
sname like zstudent01-sname obligatory,
mark1 like zstudent01-mark1 obligatory,
mark2 like zstudent01-mark2 obligatory.
selection-screen end of block blk1.
selection-screen begin of block blk2 with frame title tit2.
parameters: r1 type c radiobutton group rad1,
r2 type c radiobutton group rad1,
r3 type c radiobutton group rad1.
selection-screen end of block blk2.
*---------------------------------------------------------------------*
* CLASS lcl_class1 DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
class lcl_class1 definition.
public section.
data: agent type ref to zca_persist_01,
students type ref to zcl_persist_01.
data result1 type ref to zcl_persist_01.
methods: fetch_persistent importing im_sno like sno
im_sname like sname,
create_persistent importing im_sno like sno
im_sname like sname
im_mark1 like mark1
im_mark2 like mark2,
delete_persistent importing im_sno like sno
im_sname like sname,
output.
private section.
data: sno type zstudent01-sno,
sname type zstudent01-sname,
mark1 type zstudent01-mark1,
mark2 type zstudent01-mark2.
endclass. "lcl_class1 DEFINITION
*---------------------------------------------------------------------*
* CLASS lcl_class1 IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
class lcl_class1 implementation.
method fetch_persistent.
agent = zca_persist_01=>agent.
try.
agent->get_persistent( exporting i_sno = im_sno
i_sname = im_sname
receiving result = students ).
.
sname = students->get_sname( ).
sno = students->get_sno( ).
mark1 = students->get_mark1( ).
mark2 = students->get_mark2( ).
if r1 eq 'X'.
output( ).
endif.
CATCH CX_OS_OBJECT_NOT_FOUND .
MESSAGE 'Object doesn''t exists' TYPE 'I' DISPLAY LIKE 'E'.
endtry.
endmethod. "fetch_persistent
method output.
write:/ sno,
sname,
mark1,
mark2.
endmethod. "output
method create_persistent.
fetch_persistent( exporting im_sname = im_sname
im_sno = im_sno ).
try.
agent->create_persistent( exporting i_mark1 = im_mark1
i_mark2 = im_mark2
i_sname = im_sname
i_sno = im_sno
receiving result = students ).
commit work.
write 'Object Created'.
CATCH CX_OS_OBJECT_EXISTING .
MESSAGE 'Object already exists' TYPE 'I' DISPLAY LIKE 'E'.
endtry.
endmethod. "create_persistent
method delete_persistent.
fetch_persistent( exporting im_sname = im_sname
im_sno = im_sno ).
try.
agent->delete_persistent( exporting i_sname = im_sname
i_sno = im_sno ).
commit work.
write 'Object Deleted'.
CATCH CX_OS_OBJECT_NOT_EXISTING .
MESSAGE 'Object doesn''t exists' TYPE 'I' DISPLAY LIKE 'E'.
endtry.
endmethod. "delete_persistent
endclass. "lcl_class1 IMPLEMENTATION
data ref_class1 type ref to lcl_class1.
*---------------------------------------------------------------------*
* Load-of-Program
*---------------------------------------------------------------------*
load-of-program.
tit1 = text-001.
tit2 = text-001.
*---------------------------------------------------------------------*
* Start-of-Selection
*---------------------------------------------------------------------*
start-of-selection.
create object ref_class1.
if r1 eq 'X'.
ref_class1->fetch_persistent( exporting im_sno = sno
im_sname = sname ).
elseif r2 eq 'X'.
ref_class1->create_persistent( exporting im_sno = sno
im_sname = sname
im_mark1 = mark1
im_mark2 = mark2 ).
else.
ref_class1->delete_persistent( exporting im_sno = sno
im_sname = sname ).
endif.
Persistent Objects: Using GUID Object Identity
Objective
To Store the attributes of the Objects persistently in the database.

Persistent Object’s Identity

Every Persistent Object has a unique identity with which it can be accessed. There are 2
types of Object identity

1. Business Key
2. GUID( Global Unique Identifier )

For Persistent Objects using Business key Identity please check my previous article,
“Persistent Objects: Using Business Key identity”

This article will focus only on Persistent Object using GUID.

Step: 1 -> Create a database table

This table should contain a key field of type OS_GUID.

Step: 2 ->Create a Persistent Class

Go to Class Builder (tcode SE24)

Give persistent class name for eg ZCL_PERSIST_02 and hit the create button
In the next screen select the class type as Persistent Class and then hit Save Button.

Step: 3 -> Persistent Mapping or Mapping

Goto->Persistence Representation
Give the table name. For eg ZSTUDENT02 and hit the enter button

Table fields appear in the lower half of the tool screen.


Double Click the table field

Press the upward arrow button


Add the remaining fields as well. Screen looks like this now. Press Save Button

Activate the Class. Press the Yes Button to activate the class actor as well.
Unlike Business Key, GUID is not an attribute of the Persistent Class.

Step: 4 -> Write a Program to create / fetch / delete the Persistent Object
Our Program Selection-Screen looks like below
Here I am creating a new student. Specify the value and hit the execute button.
Output:

Go to SE16 and check the entries

Source Code
*&-------------------------------------------------------------------*
*& Report Z_PERSISTENT_GUID
*&
*&-------------------------------------------------------------------*
*&Author : Abdul Hakim
*&Development Language: ABAP
*&System Release: SAP Netweaver 2004
*&Title: Persistent Object using GUID Object Identity!!
*&-------------------------------------------------------------------*
REPORT Z_PERSISTENT_GUID.
selection-screen begin of block b1 with frame title tit1.
parameters: sno like zstudent02-sno,
sname like zstudent02-sname,
mark1 like zstudent02-mark1,
mark2 like zstudent02-mark2,
guid like zstudent02-guid.
selection-screen end of block b1.
selection-screen begin of block b2 with frame title tit2.
parameters: r1 radiobutton group rad1,
r2 radiobutton group rad1,
r3 radiobutton group rad1.
selection-screen end of block b2.
data: agent type ref to zca_persist_02,
students type ref to zcl_persist_02.
data: result1 type ref to object,
result2 type ref to zcl_persist_02.
*-------------------------------------------------------------------*
* Load-of-Program
*-------------------------------------------------------------------*
load-of-program.
tit1 = text-001.
tit2 = tit1.
*-------------------------------------------------------------------*
* At Selection Screen
*-------------------------------------------------------------------*
at selection-screen.
if ( r2 eq 'X' ).
if sno is initial or sname is initial.
MESSAGE 'Enter the values in Sno/Sname fields'
TYPE 'E' DISPLAY LIKE 'E'.
endif.
endif.
*-------------------------------------------------------------------*
* Start-of-Selection
*-------------------------------------------------------------------*
start-of-selection.
agent = zca_persist_02=>agent.
if r1 eq 'X'.
TRY.
CALL METHOD AGENT->IF_OS_CA_PERSISTENCY~GET_PERSISTENT_BY_OID
EXPORTING
I_OID = guid
RECEIVING
RESULT = result1.
result2 ?= result1.
sno = result2->get_sno( ).
sname = result2->get_sname( ).
mark1 = result2->get_mark1( ).
mark2 = result2->get_mark2( ).

write:/ sno,

You might also like