ERPClimb logoERPClimb

Code examples · Modern ABAP · Intermediate

Lock before you change: ENQUEUE and DEQUEUE a sales order and see the lock in SM12

The problem

A custom program changes a sales order.

How we solve it

  1. 1LockENQUEUE_EVVBAK
  2. 2Workchange safely
  3. 3ReleaseDEQUEUE_EVVBAK

The result

The program locks the order, shows the lock, releases it, and says who holds the lock when it cannot get it.

SAP objects in play

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

1. The requirement

A custom program changes a sales order. If two users or two jobs change the same order at the same time, one change silently overwrites the other. The program must lock the order first, and it must tell the user clearly who holds the lock when it cannot get it.

2. How we design it

SAP locks business objects with lock objects, defined in the ABAP Dictionary. For each lock object the system generates two function modules: ENQUEUE_<name> sets the lock and DEQUEUE_<name> releases it. The standard lock object EVVBAK locks a sales document by its number. The lock is not a database lock: it is an entry in the lock table of the system, visible in transaction SM12, and it only works because every program that changes the order asks for the same lock. We ask for the lock without waiting, read the exceptions, work, and release the lock in all cases.

Why we decided it this way

  1. 1Do not wait for the lock_WAIT = abap_false returns immediately with a clear exception. A dialog user gets an answer at once instead of a frozen screen.
  2. 2Exception FOREIGN_LOCK tells who holds itAfter this exception SY-MSGV1 contains the user name of the lock owner, so the message can say who to call.
  3. 3Always release, whatever happensA lock that is never released blocks everybody else until the session ends. The release sits after the TRY block, which handles errors of the work, so it always runs.
  4. 4Lock first, read afterwardsReading the order before the lock means the content may have changed when you write it back.

Before you start

  • A sales order number that exists in your system, and (to test the refusal) a second session in which the same order is open for change in VA02.
  • ADT and a package such as $TMP.

3. The code, step by step

Step 1 of 3: Set the lock

Get the lock, or learn who holds it.

ABAP · Class · ZCL_ORDER_LOCK (main method, part 1)
" Class frame and the order number
CLASS zcl_order_lock DEFINITION PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.
ENDCLASS.

CLASS zcl_order_lock IMPLEMENTATION.
  METHOD if_oo_adt_classrun~main.
    DATA(order_no) = CONV vbeln_va( '0000012345' ).
    " CALL FUNCTION 'ENQUEUE_EVVBAK'
    CALL FUNCTION 'ENQUEUE_EVVBAK'
      EXPORTING
        vbeln          = order_no
        _scope         = '2'
        _wait          = abap_false
      EXCEPTIONS
        foreign_lock   = 1
        system_failure = 2
        OTHERS         = 3.
    CASE sy-subrc.
      WHEN 0.
        out->write( |Lock set on order { order_no ALPHA = OUT }| ).
      WHEN 1.
        out->write( |Order { order_no ALPHA = OUT } is locked by { sy-msgv1 }| ).
        RETURN.
      WHEN OTHERS.
        out->write( 'The lock service is not available, try again later' ).
        RETURN.
    ENDCASE.
Part by part (plain text)
  1. Class frame and the order number. A runnable class and the order to lock. Replace the number with an order of your system.
  2. CALL FUNCTION 'ENQUEUE_EVVBAK'. VBELN is the key of the lock: the sales document. _WAIT = abap_false means: do not wait if the lock is taken. _SCOPE = '2' is the default: the lock belongs to the program, and if a COMMIT WORK with update modules happens, it passes to the update task and is released when the update ends. Our program has no update, so DEQUEUE releases it. FOREIGN_LOCK is raised when another user holds the lock, SYSTEM_FAILURE when the lock service itself fails. In both cases SY-SUBRC is not 0 and we stop.

Open in the portal

The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.

Transaction codes

4. The final outcome

The program locks the order, shows the lock, releases it, and says who holds the lock when it cannot get it.

What you should see

Lock set on order 12345 (The lock is visible in SM12 while the program waits) Lock released When another user holds the lock: Order 12345 is locked by JSMITH

Try it and check the result

  • Run the class with F9 and, within the 30 seconds, open transaction SM12 in SAP GUI, enter table VBAK (or your user name) and execute: the lock entry for your order is listed.
  • Open the same order for change in VA02 in a second session and run the class again: it prints that the order is locked by that user and does not wait.
  • After the run ends, refresh SM12: the entry is gone. A lock that stays after the program ended is a bug in the release logic.

Common mistakes

  • The lock only protects against programs that also ask for it. A program that changes VBAK directly without ENQUEUE is not stopped.
  • Do not lock for longer than needed. Long locks across user input (a dialog screen) block colleagues and are the usual cause of 'locked by' complaints.
  • After a program ends with a short dump the system releases the locks of that session. Do not rely on this: release them yourself.
  • Never delete foreign locks in SM12 to 'fix' a problem without asking the user who holds it; the other change may be in progress.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-25. Release: SAP S/4HANA or ECC system (for example an S/4HANA trial appliance from SAP Cloud Appliance Library). Uses SAP business tables, which are not released for ABAP Cloud.

Original ERPClimb teaching code with illustrative names. Not taken from a customer system. Syntax can differ by release; check the references below and activate in a sandbox first. ERPClimb is independent of SAP SE.