Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

1/8/23, 6:21 PM Using Persistent Class for CRUD(Create, Read, Update and Delete) Operations | SAP Blogs

Community

 SAP Community - Groups Read-Only Mode 


SAP Community Groups will be in read-only mode from 1.30AM EST/ 7.30AM CET on
Saturday January 7 until 7PM EST January 7/ 1AM CET Sunday January 8. During this
time no engagement activities will be available.

Ask a Question Write a Blog Post Login

Former Member
November 6, 2012 | 6 minute read

Using Persistent Class for


CRUD(Create, Read, Update and
Delete) Operations
Follow
 2  7  17,722

 Like

Using Persistent Class for CRUD(Create, Read, Update and


 RSS Feed

Delete) Operations

Introduction
In contrast to the traditional SELECT and UPDATE query, SAP also provides the
facility to use PERSISTENT Classes in order to perform CRUD(Create, Read,
Update and Delete) operations.
 
The following blog will take you through the steps on how to perform CRUD
operations on a TABLE using persistent objects.

Step by Step

https://blogs.sap.com/2012/11/06/using-persistent-class-for-crudcreate-read-update-and-delete-operations/ 1/10
1/8/23, 6:21 PM Using Persistent Class for CRUD(Create, Read, Update and Delete) Operations | SAP Blogs

Table which will be manipulated using the Persistent Class

I have selected an already created table in my system, you can create your own
and use and existing one.

Figure 1

Creating the Persistent Class

Persistent classes are like normal classes and are created using the class builder
(se24). The only difference while creating a persistent class is the tick mark
Persistent Class, which identifies the class as a persistent class.

Goto SE24 write a name and then press the create button, select the Class type
as Persistent Class and press SAVE as shown in Figure 2.

On Save system will generate two more classes along with the Persistent Class
Agent class ZCA_ASSET_VERF_DOC_HEADER and the
Base class ZCB_ASSET_VERF_DOC_HEADER

https://blogs.sap.com/2012/11/06/using-persistent-class-for-crudcreate-read-update-and-delete-operations/ 2/10
1/8/23, 6:21 PM Using Persistent Class for CRUD(Create, Read, Update and Delete) Operations | SAP Blogs

Figure 2

Since the Persistent class ZCL_ASSET_VERF_DOC_HEADER is a Protected class,


system creates ZCA_(Agent class), which is used to get the
instance of the persistent class and ZCB_(Base class), which is an abstract class
from which the agent class is derived.

This is done since the persistent class is kept by the system as a managed object
and its class cannot be instantiated directly using the CREATE OBJECT
command. So when ever CREAT_PERSISTENT, GET_PERSISTENT etc methods of
the agent class are called, a new instance of the persistent class
will be created and returned.

Linking the table with the persistent class


While editing ZCL_ASSET_VERF_DOC_HEADER press the persistence button in
the toolbar and in the popup screen enter the table to which you want
link the class to as shown in Figure 3.

https://blogs.sap.com/2012/11/06/using-persistent-class-for-crudcreate-read-update-and-delete-operations/ 3/10
1/8/23, 6:21 PM Using Persistent Class for CRUD(Create, Read, Update and Delete) Operations | SAP Blogs

Figure 3

Add the fields you want to make persistent by double clicking the fields in the
bottom section and then pressing the arrow button as shown in Figure 4.

Figure 4

Tips: Click the Generator Settings button in the toolbar and select the
Generate Methods for Query checkbox as shown in Figure 5, this will generate
the GET_PERSISTENT_BY_QUERY method as well.

https://blogs.sap.com/2012/11/06/using-persistent-class-for-crudcreate-read-update-and-delete-operations/ 4/10
1/8/23, 6:21 PM Using Persistent Class for CRUD(Create, Read, Update and Delete) Operations | SAP Blogs

Figure 5

On saving you will see that the system will generate Setter and Getter methods
for all selected fields as shown in Figure 6.

Figure 6

Activate the class and you can now use it to Create, Read, Update and Delete data
from ZASTVERFH table.

Test program to Create, Read, Update and Delete from table using
Persistent Class

Create:

DATA lo_astverf_obj TYPE REF TO zcl_asset_verf_doc_header.


DATA lv_physinv TYPE zastverfh–physinv VALUE ‘1000000000’.
DATA lv_gjahr TYPE zastverfh–gjahr VALUE ‘2010’.

TRY .

lo_astverf_obj = zca_asset_verf_doc_header=>agent->create_persistent(
                            i_physinv =
lv_physinv                                                                                                                               
                            i_gjahr = lv_gjahr ).

CALL METHOD lo_astverf_obj->set_crtdate( sy–datum ).


CALL METHOD lo_astverf_obj->set_crttime( sy–uzeit ).

https://blogs.sap.com/2012/11/06/using-persistent-class-for-crudcreate-read-update-and-delete-operations/ 5/10
1/8/23, 6:21 PM Using Persistent Class for CRUD(Create, Read, Update and Delete) Operations | SAP Blogs

CALL METHOD lo_astverf_obj->set_crtuname( sy–uname ).


CALL METHOD lo_astverf_obj->set_hstatus( ‘I’ ).

COMMIT WORK.
CATCH cx_os_object_existing.

ENDTRY.

Update:

For updation everything remains the same only you need to replace the
CREATE_PERSISTENT method with the GET_PERSISTENT  method and the
exception will change from cx_os_object_existing to cx_os_object_not_found.
TRY.

lo_astverf_obj = zca_asset_verf_doc_header=>agent->get_persistent(

                           i_physinv = lv_physinv                                                   

                          i_gjahr = lv_gjahr ).

CALL METHOD lo_astverf_obj->set_crtdate( sy–datum ).


CALL METHOD lo_astverf_obj->set_crttime( sy–uzeit ).
CALL METHOD lo_astverf_obj->set_crtuname( sy–uname ).
CALL METHOD lo_astverf_obj->set_hstatus( ‘I’ ).

COMMIT WORK.

CATCH cx_os_object_not_found.

ENDTRY.

Read:

Code for Read is same as update, the only change is we will use GETTER methods
instead of SETTER methods.
TRY .
lo_astverf_obj = zca_asset_verf_doc_header=>agent->get_persistent(

                           i_physinv = lv_physinv


                          i_gjahr = lv_gjahr ).

zastverfh–crtdate = lo_astverf_obj->get_crtdate( ).
zastverfh–crttime = lo_astverf_obj->get_crttime( ).
zastverfh–crtuname = lo_astverf_obj->get_crtuname( ).
zastverfh–hstatus = lo_astverf_obj->get_hstatus( ).

CATCH cx_os_object_not_found.

ENDTRY.

https://blogs.sap.com/2012/11/06/using-persistent-class-for-crudcreate-read-update-and-delete-operations/ 6/10
1/8/23, 6:21 PM Using Persistent Class for CRUD(Create, Read, Update and Delete) Operations | SAP Blogs

Delete:

For deletion use method DELETE_PERSISTENT and exception


CX_OS_OBJECT_NOT_EXISTING.
 

TRY .
zca_asset_verf_doc_header=>agent->delete_persistent(

     i_physinv = lv_physinv


    i_gjahr = lv_gjahr ).

COMMIT WORK.

CATCH cx_os_object_not_existing.

ENDTRY.

Read Multiple Records:

Here instead of using the GET_PERSISTENT method we use the


GET_PERSISTENT_BY_QUERY method, which will return us the list of objects.
Each
record in the database will correspond to an object in the internal table.

DATA lt_obj TYPE osreftab.


DATA ls_obj TYPE osref.

 
TRY .
    lt_obj = zca_asset_verf_doc_header=>agent-
>if_os_ca_persistency~get_persistent_by_query(
                  i_par1 = ‘5%’
                  i_query = cl_os_system=>get_query_manager( )->create_query(
                                    i_filter = ‘PHYSINV LIKE PAR1’ ) ).

    IF lines( lt_obj ) <> 0.

      LOOP AT lt_obj INTO ls_obj.

        lo_astverf_obj ?= ls_obj.

        zastverfh–physinv = lo_astverf_obj->get_physinv( ).


        zastverfh–gjahr = lo_astverf_obj->get_gjahr( ).
        zastverfh–crtdate = lo_astverf_obj->get_crtdate( ).
        zastverfh–crttime = lo_astverf_obj->get_crttime( ).

https://blogs.sap.com/2012/11/06/using-persistent-class-for-crudcreate-read-update-and-delete-operations/ 7/10
1/8/23, 6:21 PM Using Persistent Class for CRUD(Create, Read, Update and Delete) Operations | SAP Blogs

        zastverfh–crtuname = lo_astverf_obj->get_crtuname( ).


        zastverfh–hstatus = lo_astverf_obj->get_hstatus( ).

        WRITE: / zastverfh–physinv,


        zastverfh–gjahr,
        zastverfh–crtdate,
        zastverfh–crttime,
        zastverfh–crtuname,
        zastverfh–hstatus.

      ENDLOOP.

    ENDIF.

  CATCH cx_os_object_not_found.

ENDTRY.

Alert Moderator

Assigned Tags

Retagging required

abap persistence

create update delete read using persistence

crud operations

crud using persistence

khusro habib

persistence

View more... 

Similar Blog Posts 


My journey of building FIORI apps from BOPF to RAP – Part 2 (Authorization on standard CUD operations)
By Prabhjot Bhatia Jul 10, 2021

Reading and Creating Long Texts using RAP APIs


By Vijay Chintarlapalli Nov 20, 2021

Determinations in ABAP RESTful Programming Model

https://blogs.sap.com/2012/11/06/using-persistent-class-for-crudcreate-read-update-and-delete-operations/ 8/10
1/8/23, 6:21 PM Using Persistent Class for CRUD(Create, Read, Update and Delete) Operations | SAP Blogs

By Rohan Bhateja Jan 05, 2021

Related Questions 
OO Design question's
By Former Member Jun 23, 2009

ABAP OO Interface for DB Access


By Former Member Feb 21, 2018

[PERSISTENCE CLASS] CRUD for several records


By Former Member Feb 27, 2014

2 Comments

You must be Logged on to comment or reply to a post.

Joseph BERTHE
November 24, 2013 at 2:29 pm

Hello,

Thanks for your blog, it is very helpfull.

However, in the Read Multiple Records section, I tried to use the parameter PAR1 but it seams it doesn't
work, in DEBUG, the query is something like this :

>> PHYSINV LIKE PAR1

PAR1 is not replace by i_par1.

Regards,

Like 0 | Share

https://blogs.sap.com/2012/11/06/using-persistent-class-for-crudcreate-read-update-and-delete-operations/ 9/10
1/8/23, 6:21 PM Using Persistent Class for CRUD(Create, Read, Update and Delete) Operations | SAP Blogs

Former Member
October 24, 2017 at 2:35 am

In 'Linking the table with the persistent class' step to get to the screen in the Figure 3,  Menu: Goto -->
Persistence Representant.

Like 0 | Share

Find us on

Privacy Terms of Use

Legal Disclosure Copyright

Trademark Cookie Preferences

Newsletter Support

https://blogs.sap.com/2012/11/06/using-persistent-class-for-crudcreate-read-update-and-delete-operations/ 10/10

You might also like