Release system locks on materials using ABAP

Jimbo's picture

http://s.hswstatic.com/gif/combination-lock-ch.jpg|http://diarmfs.com/wp-content/uploads/2014/06/information-system-security-officer.jpg|http://box.brameco.com/images/products/large/brm5602109lg.jpg|http://cdn.shopify.com/s/files/1/0686/5373/products/combo-lock.jpg?v=1423153562|https://n2.sdlcdn.com/imgs/a/3/l/Link-Lock-Iron-Lock-SDL627097610-1-a4bbc.jpg|http://img5a.flixcart.com/image/lock/z/z/r/s-57-ss-link-s-57-200x200-imae2egdgr4erhsj.jpeg|https://lh5.googleusercontent.com/-Nyx00iFvM60/UMIkyPvuhkI/AAAAAAAAAFo/pweAMy-cXkc/s490/s2-1.gif|http://pimg.tradeindia.com/00431313/b/2/Premium-Series-Pad-Lock.jpg|http://imgsplanet.com/pics/main/31/319299-lock.jpg|http://ecx.images-amazon.com/images/I/71MCB44mrRL._SY355_.jpg|http://images1.hellotrade.com/data2/MU/FK/IMFCP-2827045/ikon-and-nelson-round-shutter-lock-801030-250x250.jpg|https://n2.sdlcdn.com/imgs/a/4/k/Harrison-Silver-Steel-Lock-SDL656683109-1-34b8b.jpg|https://www.housegoblin.co.uk/images/detailed/21/SPL120-x400.jpg|https://images-na.ssl-images-amazon.com/images/I/41pGhvY3y-L._AC_UL320_SR316,320_.jpg|http://keyosk.co.uk/images-products/pr-moon-discus-padlock.jpg|https://a2ua.com/lock/lock-006.jpgSAP forbids changes to any object when another user has it open in a change transaction. The optimal solution in this case is to contact the user who has it open and kindly ask that the transaction be closed. If the user is in a meeting or has already left for the day then one option is to use the SM12 transaction to manually unlock the object.

When running a batch in background mode, it is easy enough to simply run the batch again to pick up the records that fell out due to system locks. When making changes using a BAPI to update materials it is easier to release the system lock on the material using the ENQUE_DELETE function right before the BAPI is run, but it is only recommended for a developer to delete his own locks.

This simple form does all the work necessary to ensure a material is unlocked before a BAPI is run. It checks to see if the material is locked--potentially from the last BAPI--and unlocks the material after waiting one second if it is locked by the user who is running the program.

 FORM release_system_locks USING pv_matnr      TYPE    matnr.
   DATA: lv_gname    TYPE    eqegraname,
         lv_guname   TYPE    eqeuname,
         lv_gclient  TYPE    eqeclient,
         lv_garg     TYPE    eqegraarg,
         lv_subrc    TYPE    sysubrc.

   DATA: lt_enq     TYPE STANDARD TABLE OF  seqg3.

   lv_gname   = ' '.
   lv_guname  = sy-uname.
   lv_gclient = sy-mandt.
   CONCATENATE sy-mandt pv_matnr '*' INTO lv_garg.

   REFRESH lt_enq.
   CALL FUNCTION 'ENQUEUE_READ'
     EXPORTING
       gclient = lv_gclient
       guname  = lv_guname
       gname   = lv_gname
       garg    = lv_garg
     IMPORTING
       subrc   = lv_subrc
     TABLES
       enq     = lt_enq
     EXCEPTIONS
       OTHERS  = 1.

  DELETE lt_enq WHERE gtcode NE sy-tcode.
  WAIT UP TO 1 SECONDS.

* If the material is still locked, delete the locks
  CHECK NOT lt_enq[] IS INITIAL.
  CALL FUNCTION 'ENQUE_DELETE'
    EXPORTING
      check_upd_requests = 1
    IMPORTING
      subrc              = lv_subrc
    TABLES
      enq                = lt_enq.

ENDFORM.                    " RELEASE_SYSTEM_LOCKS