Persistent Objects: Using GUID Object Identity

...Previous

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,
                sname,
                mark1,
                mark2.
      CATCH CX_OS_OBJECT_NOT_FOUND .
*      CATCH CX_OS_CLASS_NOT_FOUND .
        MESSAGE 'Object doesn''t exists' TYPE 'I' DISPLAY LIKE 'E'.
    ENDTRY.
  elseif r2 eq 'X'.
    TRY.
        CALL METHOD AGENT->CREATE_PERSISTENT
          EXPORTING
            I_MARK1 = mark1
            I_MARK2 = mark2
            I_SNAME = sname
            I_SNO   = sno
          RECEIVING
            RESULT  = students.
        commit work.
        write 'Object Created'.
      CATCH CX_OS_OBJECT_EXISTING .
        MESSAGE 'Object already exists' TYPE 'I' DISPLAY LIKE 'E'.
    ENDTRY.
  else.
    TRY.
        CALL METHOD AGENT->IF_OS_CA_PERSISTENCY~GET_PERSISTENT_BY_OID
          EXPORTING
            I_OID  = guid
          RECEIVING
            RESULT = result1.
      CATCH CX_OS_OBJECT_NOT_FOUND .
*      CATCH CX_OS_CLASS_NOT_FOUND .
        MESSAGE 'Object doesn''t exists' TYPE 'I' DISPLAY LIKE 'E'.
    ENDTRY.
    result2 ?= result1.
    TRY.
        CALL METHOD AGENT->IF_OS_FACTORY~DELETE_PERSISTENT
          EXPORTING
            I_OBJECT = result2.
        commit work.
        write 'Object Deleted'.
      CATCH CX_OS_OBJECT_NOT_EXISTING .
        MESSAGE 'Object doesn''t exists' TYPE 'I' DISPLAY LIKE 'E'.
    ENDTRY.
  endif.

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image