Tuesday, June 2, 2015

SAP ABAP: How to check if an object is locked

Usually during a program/fm development we need to use multiple function modules, bapi's or call transaction in order to complete a whole customer process, for instance customer creation + sales order creation, or unpacking a HU into an outbound delivery and unblocking it for further processing.

Between the function calls we should wait until the object is released by the previous logic unit. Over there we can find solutions like "wait" instruction and also the function module "ENQUEUE_REPORT" which let us know what is locked for the moment this FM is ran.

Following  a sample code to determine if a sales document is locked:

  lv_repeat = 'Y'.
  while lv_repeat = 'Y'.
    CALL FUNCTION 'ENQUEUE_REPORT'
      EXPORTING
        GCLIENT               = SY-MANDT
        GNAME                 = ' '
        GTARG                 = ' '
        GUNAME                = SY-UNAME
      IMPORTING
        NUMBER                = lv_number
        SUBRC                 = lv_subrc
      TABLES
        ENQ                   = lt_enq
      EXCEPTIONS
        COMMUNICATION_FAILURE = 1
        SYSTEM_FAILURE        = 2
        OTHERS                = 3.

    concatenate sy-mandt p_vbeln into lv_key.
    read table lt_enq with key garg = lv_key transporting no fields.
    if sy-subrc = 0. "if found the object is locked yet
      lv_repeat = 'Y'.
    else.
      lv_repeat = 'N'.
    endif.
  endwhile.

If you find this useful, please do not hesitate in like and share it!
-------------------------------------------------------------------------------------------------------
Por lo general, durante la construccion de un  programa o modulo de funciones tenemos que utilizar múltiples FM, BAPI's o call transactions con el fin de completar un proceso cliente. Por ejemplo la creación de un cliente + orden de venta, o desempaquetar un unidad de empaque en una entrega de salida y desbloqueo para su posterior procesamiento.

Entre las llamadas a funciones que deberíamos esperar hasta que el objeto es liberado por la unidad lógica anterior. Allí podemos encontrar soluciones como usar la instruccion "wait"  y también el módulo de función "ENQUEUE_REPORT", que nos dice lo que está bloqueado al momento de su ejecucion.

A continuacion código de ejemplo para determinar si un documento de ventas está bloqueado:

  lv_repeat = 'Y'.
  while lv_repeat = 'Y'.
    CALL FUNCTION 'ENQUEUE_REPORT'
      EXPORTING
        GCLIENT               = SY-MANDT
        GNAME                 = ' '
        GTARG                 = ' '
        GUNAME                = SY-UNAME
      IMPORTING
        NUMBER                = lv_number
        SUBRC                 = lv_subrc
      TABLES
        ENQ                   = lt_enq
      EXCEPTIONS
        COMMUNICATION_FAILURE = 1
        SYSTEM_FAILURE        = 2
        OTHERS                = 3.

    concatenate sy-mandt p_vbeln into lv_key.
    read table lt_enq with key garg = lv_key transporting no fields.
    if sy-subrc = 0. "if found the object is locked yet
      lv_repeat = 'Y'.
    else.
      lv_repeat = 'N'.
    endif.
  endwhile.

Si encuentras este post util, por favor no dudes en darle "like" o compartirlo!

By Jose Antonio Lagonell


No comments:

Post a Comment

Python - borrar lineas extra de archivos .txt en un directorio

 Aqui el codigo Python para borrar lineas extra en un archivo plano: import os path = "c:\\temp" path_new='c:\\temp\\CleanFile...