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|https://upload.wikimedia.org/wikipedia/commons/thumb/4/44/Padlock.jpg/220px-Padlock.jpg|https://www.wpownersmanual.com/wp-content/uploads/2014/04/lock.jpg|https://cdn.americanfinancing.net/images/gallery/learning-center/tlc_featured/locks_2x.jpg?1530628380|https://www.uhaul.com/MovingSupplies/Image/GetMedia/?id=11638&media=8927|https://cdn3.volusion.com/fscwp.utjhn/v/vspfiles/photos/PL-127-CP-2.jpg?v-cache=1563260596|https://images-na.ssl-images-amazon.com/images/I/61%2BKGX3aJsL._AC_SL1152_.jpg|https://i0.wp.com/practicalselfreliance.com/wp-content/uploads/2019/03/Lock-Picking-Schlage-Lock.jpg?fit=600%2C400&ssl=1|https://theravingtrends.com/wp-content/uploads/2020/03/lock-picking-set-.jpg|https://images2.minutemediacdn.com/image/upload/c_crop,h_845,w_1500,x_0,y_134/f_auto,q_auto,w_1100/v1555173146/shape/mentalfloss/501600_istock-609693864.jpg|https://media.thetab.com/blogs.dir/155/files/2012/10/lock1.jpg|https://locksmith-dallas.us.com/wp-content/uploads/2015/04/Fotolia_33217486_XS.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

https://i.pinimg.com/originals/54/04/5e/54045e08de02ffb345cf9658b52037e0.jpg|https://img.memecdn.com/big-lock_o_1430297.jpg|https://i.chzbgr.com/full/7712631552/h0CAD7E7F/im-not-the-sharpest-knife-in-the-drawer|https://i.imgflip.com/296vaf.jpg|https://media.techeblog.com/images/bikelock.jpg|https://randomtidbitsofthought.files.wordpress.com/2010/09/img_6690.jpg?resize=490%2C367|https://assets.horseheadhuffer.com/hashed_silo_content/silo_content/6321/resized/1198422979032.jpg|https://lockandkey-sandiegolocksmith.com/wp-content/uploads/2014/12/door-chain.jpg