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.
Figure 5
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.
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.
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.
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.
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( ).
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.