Release system locks on materials using ABAP
SAP 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