How to implement a Lock object in ABAP?

Its about a week passed since super typhoon Yolanda hit the country. As seen from the news around the world, the City of Tacloban and nearby towns were badly damaged due to this natural disaster. The Philippine government with the aid of the international community join forces together as fast as possible to rescue the victims, to give reliefs to the survivors and most importantly to lift their spirits and hopes.

Filipinos are known of our resiliency. No matter how many trials come along the way, we stand firm that we will overcome with God's help. We are fortunate that we were spared with that super typhoon. If we were in that place, can we survive the wrath of mother nature? If we survived then, what is our life right now? So we must be thankful we didn't experience the same as our fellow Filipinos are experiencing today. I appeal to the whole world please join me in donating anything you can. Please contact your national Red Cross to get more details. I am now extending my full gratitude to everyone.

So today, I will discuss with you about Lock Objects and how to create and implement it into your ABAP programs. 


What is a Lock Object Concept?
Lock objects is implemented in SAP to avoid the inconsistency during update into the database. It is needed to maintain data integrity. This simply means that no user can edit the same record at the same time. For example, user A is editing Delivery No. 80001055. When user B access the same Delivery No in VL02N, the system will says, "it is being locked by user A". 

What are the Types of Lock Objects?
SAP provides three(3) types of Lock objects:

a. Read Lock - protects read access to an object. The read lock allows other transactions read access but not write access to the locked area of the table

b. Write Lock - protects write access to an object. The write lock allows other transactions neither read nor write access to  the locked area of the table.

c. Enhanced write lock - works like a write lock except that the enhanced write lock also protects from further accesses from the same transaction.

How to create a Lock Object?
1. Go to tcode SE11. Then select the option Lock Object. Remember always that the start of the name of the lock object is "E"
and followed by ‘Z’ or ‘Y’.  For instance, "EZLIKP". Then click the Create button.

2. In this screen below, select the Tables tab. Give a meaningful short description of your lock object. Then on the primary table, enter the name of the table which will be locked. In my sample, I used table LIKP. Please note that it can be a standard table or a customized table. Then on the Lock Mode selection, select from the types of lock as discussed above. I choose "Write Lock" so that no one will be able to edit a particular Delivery No simultaneously. Save and activate.

As can be seen from the Lock Parameter tab, the primary keys (MANDT & VBELN) of the LIKP table are the one listed as the lock parameters.

So in this two steps, you were able to create a lock object. Remember always, the name of the lock object must begin with "E".

How to implement a Lock Object in ABAP?
 Once a lock object is created, two function modules were automatically created by the system. These are:

a. ENQUEUE_[Lockobject name] - To lock the object. ex: ENQUEUE_EZLIKP.
b. DEQUEUE_[Lockobject name] - To unlock the object. ex: DEQUEUE_EZLIKP.

Consider this case. I created a dialog program which change the picking date of a Delivery No. So in my first screen, I asked the user to input the Delivery No. just like this one below.

Once the user click the execute button, you will need to lock now that delivery no. so that no one would be able to edit it while you are doing the updates. Regardless you update it or not, you need to lock the object to maintain data consistency. Since I implemented this program using a dialog screen, I will call the FM ENQUEUE_EZLIKP into the PAI. But if you are using a SELECTION-SCREEN, call this FM at the event, START-OF-SELECTION. Pass the delivery no. as the exporting parameter of the Function Module.  Take this sample code:

      mode_likp      = 'E'
      mandt          = sy-mandt
      vbeln          = p_vbeln "Delivery No
      foreign_lock   = 1
      system_failure = 2
      OTHERS         = 3.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE c_mode_s NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 DISPLAY LIKE c_mode_e.
    "if locking is successful, it will call screen 100 
    CALL SCREEN 100.

So if your ABAP program passes into this code above, it will create a lock entry in SM12 as seen below. Anything listed here must be unlock first before anyone can use it again. The report below shows the lock mode and the lock argument of the lock object.

So if someone of your company try to access the same object which is currently locked, it will prompt to his screen this message below indicating it is being processed by a specific user . Hence, data integrity prevails.

As I said above, lock objects remains in the lock entry list until you logoff your SAP system or call  the FM DEQUEUE. As a best practice, take the second method always, call the FM DEQUEUE in order to unlock the object and other users may view and update it too. To call the FM, consider this snippet:

      mode_likp = 'E'
      mandt     = sy-mandt
      vbeln     = p_vbeln.

1. Lock Object starts with "E" and followed by "Z" or "Y";
2. There are 3 types of lock: Read, Write and Enhanced Write;
3. There are 2 FM automatically created and used in implementing locks: ENQUEUE_<name_of_lock_object> and DEQUEUE_<name_of_lock_object>.
4. Always call the FM DEQUEUE once done with the processing on your ABAP code;

Thank you once again to everyone for reading this simple blog. Please join me in my page at Facebook, ABAPTHINKERS to get more updates time to time.

No comments:

Post a Comment