Hello Dear Learners, If you want to Learn SAP ABAP OOPs Notes for Beginners | SAP ABAP OOPs Notes for Beginners | SAP OOPS ABAP Notes for classes for Beginners | Classes and OOPS notes in SAP ABAP | OOPS ABAP Notes | SAP ABAP with OOPS | OOPS ABAP Classes then you will get all the details here.
Persistence Class in SAP ABAP
Persistence Service: it is the layer in between of ABAP program and data base table which enables
to store the attributes of the object with unique identity and when ever requires it retrieves from
the data base in the same state as it was at the time of saving.
Persistence Class: – A class which enables the Persistence Service is known as Persistence Class.
If we create something class and object, Object information will clear once we close the program.
If we want save the last update information (Object), then we have to go for Persistence Class. To
save the last update Object information, we need to create one table. Here, I’ve created a table.

Create the Persistence class.
Execute SE24. Provide the class name (ZCL_SO_INFO).

Once we select the radio button – Persistent class, automatically the Inst. Generation will change
from ‘PUBLIC’ to ‘Protected’.
System automatically add one interface with its methods.

If we open the methods tab,

Now we need to attach the table information to the class.
Click on Go to (menu button) → Persistent Representant → Provide the table name –
ZSALES_ORD_INFO, click on enter button.

Double click on the field VBELN (last window), the field will come to bit top beside to the up arrow
mark. Click on that up arrow mark. The field will add in main area. Do the same thing for all fields.

Click on save button.
System will add a few attributes and methods.

In table how many fields are there, that many of attributes it will add here.
If we go for Method tab,

System is adding a few methods like GET_* and SET_. In table, for all fields system will create GET_ method and for non key fields also system add
methods like SET_*.
System will create one Friend class for this Persistent class.

We will call this as Base Agent Class.
If we open this class, this is Abstract method. So we can’t create object for it.

Base agent class will add a few methods like this.
If we activate the Persistent Class then system will add a few methods in Base Agent class.

CREATE_PERSISTENT, DELETE_PERSISTENT, GET_PERSISTENT…
System will create one more class at the time of activating the Persistent class. We will call it as
Actor class.

This is sub class for Base class – ZCB_SO_INFO and it is Private class, so we can’t access the
properties of this class directly.
The naming convention for the classes
ZCL → Persistence Class
ZCB → Base Agent Class
ZCA → Actor Class

We can see one static attribute in Actor class. The association is like
AGENT TYPE REF TO ZCA_SO_INFO.
Means, it is referring one object for the same class.
If we access this attribute by using class name, if any constructor is there, it will execute
automatically. Open the methods tab and check for constructor.

One Static Constructor is there. Open the method implementation.

This constructor will execute by default, and it will provide one object for this class.
Create one local program.
PARAMETERS p_vbeln TYPE vbeln_va.
PARAMETERS p_kunnr TYPE kunnr.
PARAMETERS p_vbtyp TYPE vbtyp.
PARAMETERS: p_cr RADIOBUTTON GROUP s USER-COMMAND uc DEFAULT 'X',
p_dl RADIOBUTTON GROUP s,
p_gt RADIOBUTTON GROUP s,
p_ch RADIOBUTTON GROUP s.
DATA obj_actor TYPE REF TO zca_so_info.
DATA obj_per TYPE REF TO zcl_so_info.
START-OF-SELECTION.
obj_actor = zca_so_info=>agent.
"Object will come here
IF p_cr = 'X'.
TRY.
CALL METHOD obj_actor->create_persistent
EXPORTING
i_vbeln = p_vbeln
RECEIVING
result = obj_per.
" Newly Generated Persistent
Object
TRY.
CALL METHOD obj_per->set_kunnr
EXPORTING
i_kunnr = p_kunnr.
" Attribute Value
TRY.
CALL METHOD obj_per->set_vbtyp
EXPORTING
i_vbtyp = p_vbtyp.
" Attribute Value
CATCH cx_os_object_not_found. " Object Services Exception
ENDTRY.
COMMIT WORK.
CATCH cx_os_object_not_found. " Object Services Exception
ENDTRY.
CATCH cx_os_object_existing. " Object Services Exception
ENDTRY.
ELSEIF p_dl = 'X'.
p_vbeln = |{ p_vbeln ALPHA = IN }|.
TRY.
CALL METHOD obj_actor->delete_persistent
EXPORTING
i_vbeln = p_vbeln.
" Business Key
CATCH cx_os_object_not_existing. " Object Services Exception
ENDTRY.
COMMIT WORK.
ELSEIF p_GT = 'X'.
CLEAR: obj_per.
TRY.
CALL METHOD obj_actor->get_persistent
EXPORTING
i_vbeln = p_vbeln
" Business Key
RECEIVING
result = obj_per.
" Persistent Object
IF obj_per IS BOUND.
CLEAR: p_kunnr, p_vbtyp.
TRY.
CALL METHOD obj_per->get_kunnr
RECEIVING
result = p_kunnr.
" Attribute Value
CATCH cx_os_object_not_found. " Object Services Exception
ENDTRY.
ENDIF.
CATCH cx_os_object_not_found. " Object Services Exception
ENDTRY.
ELSEIF p_ch = 'X'.
CALL METHOD obj_actor->get_persistent
EXPORTING
i_vbeln = p_vbeln
" Business Key
RECEIVING
result = obj_per.
" Persistent Object
IF obj_per IS BOUND.
CALL METHOD obj_per->set_kunnr
EXPORTING
i_kunnr = p_kunnr.
" Attribute Value
COMMIT WORK.
CALL METHOD obj_per->set_vbtyp
EXPORTING
i_vbtyp = p_vbtyp.
" Attribute Value
COMMIT WORK.
ENDIF.
ENDIF
Events in SAP ABAP
Event is an action to do one particular activity. We can declare events in the class. But Event
handle method should in the same class or any other class.
Create a class with adding one event.


Implement the method.

If data is not fetching, then event is raising here. Now need to take one method which will handle
this event.

EHM_VBAK is the method which will handle the event. But we need to tell the system that,
EHM_VBAK method will handle the event. Select the EHM_VBAK method, click on Go to
Parameters.

Select the checkbox of ‘Event Handler for’. Provide the class name and event name. click on
‘Change’ button.

Now we can see the symbol of Event Handler Method as shown in the above image.
Implement the Event Handler Method.

Activate the class. Call the method GET_VBAK in one program.
REPORT ztest_2931.
DATA obj_t1 TYPE REF TO zcl_t1.
PARAMETERS p_kunnr TYPE kunnr.
START-OF-SELECTION.
CREATE OBJECT obj_t1.
SET HANDLER obj_t1->ehm_vbak for obj_t1.
CALL METHOD obj_t1->get_vbak
EXPORTING
i_kunnr = p_kunnr
" Customer Number
IMPORTING
es_vbak = DATA(ls_vbak).
" Sales Document: Header Data
write:/ ls_vbak-vbeln, ls_vbak-kunnr.
Before displaying the output, need to register the event by using below syntax.
SET HANDLER obj_t1->ehm_vbak for obj_t1.
Here, event handler method and normal method both are in the same class. We can maintain the
event handler method in another class also.
Remove the event handler method in the present class and create one new class, maintain the
event handler method in the new class.

Removed the event handler method from the present class.
Create one more new class and maintain the event handler method here.

Make the method as event handler method.

Click on change button.

Now go to the program and register the event.
REPORT ztest_2931.
DATA obj_t1 TYPE REF TO zcl_t1.
DATA obj_t2 TYPE REF TO zcl_t2.
PARAMETERS p_kunnr TYPE kunnr.
START-OF-SELECTION.
CREATE OBJECT obj_t1.
CREATE OBJECT obj_t2.
set HANDLER obj_t2->ehm_vbak for obj_t1.
CALL METHOD obj_t1->get_vbak
EXPORTING
i_kunnr = p_kunnr
IMPORTING
es_vbak = DATA(ls_vbak).
write:/ ls_vbak-vbeln, ls_vbak-kunnr.
We can give importing parameters for events.

The main method logic should be like this.
METHOD get_vbak.
SELECT SINGLE * FROM vbak INTO es_vbak WHERE kunnr = i_kunnr.
IF sy-subrc <> 0.
RAISE EVENT no_data_found EXPORTING im_kunnr = i_kunnr.
ENDIF.
ENDMETHOD.
For event handler method, we can take the inputs like this.

We can’t give more parameters here. What ever the parameters we have given in event, those
parameters should be mention here. If we pass different parameters, system will through errors.

(here I’m maintaining normal method and event handler method in the same class).
REPORT ztest_2931.
DATA obj_t1 TYPE REF TO zcl_t1.
PARAMETERS p_kunnr TYPE kunnr.
START-OF-SELECTION.
CREATE OBJECT obj_t1.
set HANDLER obj_t1->ehm_vbak for obj_t1.
CALL METHOD obj_t1->get_vbak
EXPORTING
i_kunnr = p_kunnr
IMPORTING
es_vbak = DATA(ls_vbak).
write:/ ls_vbak-vbeln, ls_vbak-kunnr.
It will display the message with the customer number.