Archivo

Posts Tagged ‘call transaction’

Mostrar mensajes error BDC Batch Input -> messtab

Caso: buscamos mostrar los mensajes de error de un Batch Input (igual sirve para cualquiera de las tablas de mensajes que nos retornan algunas BAPIs) en un popup interactivo.

Solución: Hay un par de funciones interesantes para ello, que lo muestra como el estándar. Dejo la parte importante del código, no la del Batch Input, total lo importante es definir la tabla de mensajes en el CALL TRANSACTION:

* Generar el batch input
DATA: BEGIN OF bdc_tab OCCURS 0.
INCLUDE STRUCTURE bdcdata.
DATA: END OF bdc_tab.
DATA BEGIN OF messtab OCCURS 10.
INCLUDE STRUCTURE bdcmsgcoll.
DATA END OF messtab.

DATA: gs_opt TYPE ctu_params.

… líneas de armado del batch input en bdc_tab…

CALL FUNCTION ‘MESSAGES_INITIALIZE’
EXCEPTIONS
OTHERS = 1.

CALL TRANSACTION ‘IW32’ USING bdc_tab
OPTIONS FROM gs_opt
MESSAGES INTO messtab.

READ TABLE messtab TRANSPORTING NO FIELDS WITH KEY msgtyp = ‘E’.

IF sy-subrc NE 0.

CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’
EXPORTING
wait = ‘X’.

ELSE.
CALL FUNCTION ‘BAPI_TRANSACTION_ROLLBACK’.

LOOP AT messtab.

CALL FUNCTION ‘MESSAGE_STORE’
EXPORTING
arbgb = messtab-msgid
msgty = messtab-msgtyp
msgv1 = messtab-msgv1
msgv2 = messtab-msgv2
msgv3 = messtab-msgv3
msgv4 = messtab-msgv4
txtnr = messtab-msgnr
EXCEPTIONS
message_type_not_valid = 1
not_active = 2
OTHERS = 3.

ENDLOOP.

CALL FUNCTION ‘MESSAGES_SHOW’
EXPORTING
i_use_grid = ‘X’ " Comment for list display
batch_list_type = ‘L’
EXCEPTIONS
inconsistent_range = 1
no_messages = 2
OTHERS = 3.

ENDIF.

Anuncio publicitario

Crear LOG para Batch Input o Call Transaction

Transacciones asociadas: SLG0, SLG1

Vamos a crear un LOG para ver el resultado de la ejecución de, por ejemplo, un CALL TRANSACTION (puede usarse para cualquier cosa que use una tabla de mensajes)

Para ello necesitamos meter mucho código (recomiendo armarse un incluye) y crear un objeto de log.

Empecemos creando el objeto de log: tenemos que ir a la transacción SLG0, seleccionamos un objeto (ejemplo ZFI) y hacemos click en “Objetos inferiores”.

Allí creamos un log para nuestro programa (con “Entradas nuevas”) y grabamos

Ahora vayamos al código:

Las funciones que se usan son las funciones BAL_LOG* para la creación y manipulación del log y BAL_* para visualizar y grabarlo en SAP.

La secuencia sería la siguiente:

  1. Creo un objeto de log con la función BAL_LOG_CREATE
  2. Hago un refresh de la tabla interna que contiene los datos de log con la función BAL_LOG_REFRESH
  3. loopeo la tabla de mensajes para agregar todos los mensajes al log (supongamos que los mensajes son generados por la ejecución de un call transaction), con la función BAL_LOG_MSG_ADD
  4. Muestro el resultado del log en pantalla con la función BAL_DSP_LOG_DISPLAY
  5. Grabo en SAP el log, para luego poder acceder a verlo por la transacción SLG1, con la función BAL_DB_SAVE.

Hay varias funciones más relacionadas, les recomiendo mirar las que existen. En este link hay una explicación de casi todas creo:

http://abap4.tripod.com/Using_Application_Logging.html

Bien, les paso ahora el código y el include:

Ejemplo:

constants:
* Objeto y sub-objeto de log
co_object type balobj_d value ‘ZFI’,
co_subobject type balsubobj value ‘ZFI_AR0001’.

* Creo el LOG

perform log_create.

* Llamo al call transaction

call transaction ‘VK11’

using l_bdcdata_tab
mode co_mode
update co_updt

messages into p_messtab_tab. » Lo que nos importa son los mensajes

* Proceso la tabla de mensajes p_messtab_tab

perform procesar_mess_tab changing p_messtab_tab[].

* Muestra el LOG de ejecución
perform log_display.
perform log_save.

form procesar_mess_tab changing p_mess_tab type ty_bdcmsgcoll_tab.
data: l_mess_err type c.

perform log_process changing p_mess_tab
l_mess_err.

endform. » PROCESAR_MESS_TAB

form log_create.
data: l_log_wa type bal_s_log.

if g_ctrllog_wa-crtlog is initial.

g_ctrllog_wa-crtlog = abap_true.

perform log_refresh using g_log_handle
g_log_handle_tab.

l_log_wa-object = co_object.
l_log_wa-subobject = co_subobject.
l_log_wa-aldate = sy-datum.
l_log_wa-altime = sy-uzeit.
l_log_wa-aluser = sy-uname.
l_log_wa-altcode = sy-tcode.
l_log_wa-alprog = sy-repid.
l_log_wa-almode = ‘D’.
l_log_wa-alchdate = sy-datum.
l_log_wa-alchtime = sy-uzeit.
l_log_wa-alchuser = sy-uname.

call function ‘BAL_LOG_CREATE’
exporting
i_s_log = l_log_wa
importing
e_log_handle = g_log_handle
exceptions
log_header_inconsistent = 1
others = 2.

if not sy-subrc is initial.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

endif.

endform. » LOG_CREATE

form log_process
changing p_mess_tab type ty_bdcmsgcoll_tab
p_mess_err type c.

data: l_dummy type c, «#EC NEEDED
l_mess_wa type bdcmsgcoll.

loop at p_mess_tab into l_mess_wa.
message id l_mess_wa-msgid
type l_mess_wa-msgtyp
number l_mess_wa-msgnr
with l_mess_wa-msgv1
l_mess_wa-msgv2
l_mess_wa-msgv3
l_mess_wa-msgv4
into l_dummy.
«Añadimos los mensajes a nuestro log.
perform log_msg_add changing p_mess_err.
endloop.

clear: p_mess_tab.
refresh: p_mess_tab.
free: p_mess_tab.

endform. » LOG_PROCESS

form log_refresh
using p_log_handle type balloghndl
p_log_handle_tab type bal_t_logh.

call function ‘BAL_LOG_REFRESH’
exporting
i_log_handle = p_log_handle
exceptions
log_not_found = 1
others = 2.

if not sy-subrc is initial.
exit.
endif.

refresh p_log_handle_tab.
endform. » LOG_REFRESH

form log_msg_add
changing p_mess_err type c.

data: l_msg_wa type bal_s_msg.

l_msg_wa-msgty = sy-msgty.
l_msg_wa-msgid = sy-msgid.
l_msg_wa-msgno = sy-msgno.
l_msg_wa-msgv1 = sy-msgv1.
l_msg_wa-msgv2 = sy-msgv2.
l_msg_wa-msgv3 = sy-msgv3.
l_msg_wa-msgv4 = sy-msgv4.

case sy-msgty.
when co_msgty_x.
l_msg_wa-probclass = co_probclass_very_high.
p_mess_err = ‘X’.
when co_msgty_a.
l_msg_wa-probclass = co_probclass_very_high.
p_mess_err = ‘X’.
when co_msgty_e.
l_msg_wa-probclass = co_probclass_high.
p_mess_err = ‘X’.
when co_msgty_w.
l_msg_wa-probclass = co_probclass_medium.
when co_msgty_i.
l_msg_wa-probclass = co_probclass_low.
when co_msgty_s.
l_msg_wa-probclass = co_probclass_low.
when co_msgty_none.
l_msg_wa-probclass = co_probclass_none.
endcase.

call function ‘BAL_LOG_MSG_ADD’
exporting
i_log_handle = g_log_handle
i_s_msg = l_msg_wa
exceptions
log_not_found = 1
msg_inconsistent = 2
log_is_full = 3
others = 4.

if not sy-subrc is initial.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

append g_log_handle to g_log_handle_tab.

endform. » LOG_MSG_ADD

form log_display .

call function ‘BAL_DSP_LOG_DISPLAY’
* EXPORTING
* I_S_DISPLAY_PROFILE =
* I_T_LOG_HANDLE =
* I_T_MSG_HANDLE =
* I_S_LOG_FILTER =
* I_S_MSG_FILTER =
* I_T_LOG_CONTEXT_FILTER =
* I_T_MSG_CONTEXT_FILTER =
* I_AMODAL = ‘ ‘
* IMPORTING
* E_S_EXIT_COMMAND =
exceptions
profile_inconsistent = 1
internal_error = 2
no_data_available = 3
no_authority = 4
others = 5.

if not sy-subrc is initial.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

endform. » LOG_DISPLAY

form log_save.
data: l_bal_t_lgnm type bal_t_lgnm.

call function ‘BAL_DB_SAVE’ «Application Log: Database: Save logs
exporting
i_client = sy-mandt » sy-mandt Client in which the new log is to be saved
i_in_update_task = space » boolean Save in UPDATE TASK
i_save_all = space » boolean Save all logs in memory
i_t_log_handle = g_log_handle_tab » bal_t_logh Table of log handles
importing
e_new_lognumbers = l_bal_t_lgnm «#EC NEEDED » bal_t_lgnm Table of new log numbers
exceptions
log_not_found = 1 » Log not found
save_not_allowed = 2 » Cannot save
numbering_error = 3 » Number assignment error
.
if not sy-subrc is initial.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

endform. «log_save

Ejecutamos el programa y luego accedemos a la transacción SLG1 para ver los resultados de la ejecución del LOG.

Acá vemos los resultados que se generaron al usar el call transaction

Categorías: ABAP/4 Etiquetas: , , ,