This website is not affiliated with, sponsored by, or approved by SAP AG.

Message text

Development (ABAP Development WorkBench, ABAP/4 programming)

Moderators: Snowy, thx4allthefish, Gothmog, YuriT

Message text

Postby panda2006 on Wed Feb 21, 2007 6:35 am

Hi all,

How to write message text from a message class to internal table?
eq: I have message class : XX with message number 001, message text 'Error in conversion'.

I want to write this message text to internal table field ITAB-MESSAGE_TEXT.

It is similar like in the function of displaying job log in SM37.

Please advise.

thanks a whole lot
Karina

panda2006
 
Posts: 88
Joined: Tue Apr 18, 2006 2:27 am

Postby panda2006 on Wed Feb 21, 2007 6:39 am

I need this as in the loop , i will do checking for certain criteria. Instead of displaying it on user's screen using 'MESSAGE ID ...', I want to store it in internal table for output display.

Again, thanks
Karina
panda2006
 
Posts: 88
Joined: Tue Apr 18, 2006 2:27 am

Postby sapmanie on Wed Feb 21, 2007 7:01 am

Hi,
This can be done simply by putting the message into a string and concatenating the data to an internal table. There are a few function modules which will create the string for you, based on the message id etc. inputs. e.g. RPY_MESSAGE_COMPOSE, MESSAGE_TEXT_BUILD, MESSAGE_PREPARE... (see also 'common sap r/3 function modules' book) .

In your case, something like the following should help in what you are looking to do:

DATA msg(250) TYPE c.
DATA: BEGIN OF imsgs OCCURS 0,
msgs(250) TYPE c,
END OF imsgs.

CALL FUNCTION 'MESSAGE_TEXT_BUILD'
EXPORTING
msgid = sy-msgid <- message id
msgnr = sy-msgno <- message number
msgv1 = sy-msgv1 <- message text 1
msgv2 = sy-msgv2
msgv3 = sy-msgv3
msgv4 = sy-msgv4
IMPORTING
message_text_output = msg.

imsgs-msgs = msg.
APPEND imsgs.
sapmanie
 
Posts: 463
Joined: Fri Jan 23, 2004 10:39 am

Postby maen on Wed Feb 21, 2007 7:06 am

Why not use:

MESSAGE I001(XX) INTO ITAB-MESSAGE_TEXT.
maen
 
Posts: 141
Joined: Wed Feb 21, 2007 7:01 am

Postby sapmanie on Wed Feb 21, 2007 7:28 am

Didn't know about that variant maen! Very handy! Thank-you!
sapmanie
 
Posts: 463
Joined: Fri Jan 23, 2004 10:39 am

Postby m@t on Wed Feb 21, 2007 7:47 am

If you're collecting error messages, use the application log: function group SBAL. Fully documented.

Why re-invent the wheel?

m@t
TULY Image The quality of answers is roughly proportional to the quality of the question.

The downside of being better than everyone else is that people tend to assume you're pretentious.
m@t
 
Posts: 4118
Joined: Thu Sep 04, 2003 7:37 am
Location: Switzerland

Postby Rich on Wed Feb 21, 2007 8:54 am

m@t wrote:If you're collecting error messages, use the application log: function group SBAL. Fully documented.

Why re-invent the wheel?

m@t


Yup
Regards

Rich

Image
Abap KC:http://www.richard-harper.me.uk/Kb
SFMDR:http://www.se37.com
Rich
 
Posts: 7092
Joined: Thu Oct 31, 2002 4:47 pm
Location: Geneva

Postby sapmanie on Wed Feb 21, 2007 10:17 am

Found a how-to of the application logging functionality for you:

Author: Ashim Chowdhury

This article explains how to create your own application logging program in ABAP.
SAP provides standard functionality to write into as well as view the application log. This article will explain in detail how to create your own logging function module for application logging using standard SAP log facility. Two transaction code related to application logging are
? SLG0 -> Used to maintain the log object
? SLG1 -> Used to view the log

Configuration step:
In this step create a log object.
1. Open transaction SLG0. An information message about cross-client table will come.
2. Click the button "New Entries". Enter the name and description of your log object (say ZTESTLOG as object name and "Test Log" as object description) and save.

Development Step:
The following standard function modules provided by SAP has been used to create the Z function module for logging.
? BAL_GLB_MSG_DEFAULTS_SET
? BAL_DB_SAVE
? BAL_LOG_MSG_ADD

Here are the steps:

1. Go to transaction SE11. Create a z-structure Z_LOG_MESSAGE having the following fields

Component Component Type
MSGTY SYMSGTY
MSG_TEXT_1 SYMSGV
MSG_TEXT_2 SYMSGV
MSG_TEXT_3 SYMSGV
MSG_TEXT_4 SYMSGV

2. Crate a message class say ZMESSAGE in transaction SE91 and a message 999 with four placeholder ( & ) as the text.
3. Go to transaction SE37. Create a function group say ZLOG

4. After the function group is created, create a function module in that group. Let is name it ZIU_MESSAGE_LOGGING.

Import parameters:
a. I_LOG_OBJECT type BALOBJ_D -> Application log: Object name (Application code)
b. I_EXTNUMBER type String -> Application Log: External ID

Export parameters: None
Changing parameters: None
Tables parameters:
T_LOG_MESSAGE type Z_LOG_MESSAGE
Exceptions:
LOG_HEADER_INCONSISTENT
LOGGING ERROR

Paste the code below as the source code

FUNCTION ZIU_MESSAGE_LOGGING.
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" REFERENCE(I_LOG_OBJECT) TYPE BALOBJ_D
*" REFERENCE(I_EXTNUMBER) TYPE STRING
*" TABLES
*" T_LOG_MESSAGE STRUCTURE Z_LOG_MESSAGE
*" EXCEPTIONS
*" LOG_HEADER_INCONSISTENT
*" LOGGING_ERROR
*"----------------------------------------------------------------------

* Author :Ashim Chowdhury

* DESCRIPTION: This function module is used insert messages in the
* application log

CONSTANTS: c_message TYPE syst-msgid VALUE 'ZMESSAGE',
c_999 TYPE syst-msgno VALUE '999'.


DATA:
l_log_handle TYPE balloghndl,
l_s_log TYPE bal_s_log,
l_dummy type string,
l_ext_no type bal_s_log-extnumber,
l_s_mdef TYPE bal_s_mdef.


if T_LOG_MESSAGE[] is not initial.

l_s_log-object = I_LOG_OBJECT.
l_ext_no = I_EXTNUMBER.
l_s_log-extnumber = l_ext_no.

* Create the log with header data
CALL FUNCTION 'BAL_LOG_CREATE'
EXPORTING
i_s_log = l_s_log
IMPORTING
E_LOG_HANDLE = l_log_handle
EXCEPTIONS
LOG_HEADER_INCONSISTENT = 1
OTHERS = 2.

IF sy-subrc <> 0.
case sy-subrc.
when 1.
raise LOG_HEADER_INCONSISTENT.
when others.
raise LOGGING_ERROR.
endcase.
ENDIF.

l_s_mdef-log_handle = l_log_handle.

* Set the default value
CALL FUNCTION 'BAL_GLB_MSG_DEFAULTS_SET'
EXPORTING
i_s_msg_defaults = l_s_mdef
EXCEPTIONS
OTHERS = 0.

* Loop the message table and write the messages into the log
loop at T_LOG_MESSAGE.

* Use the message type ZMESSAGE and msg no 999
* Issue the message in a dummy variable
message ID C_message type t_log_message-MSGTY number C_999
with t_log_message-MSG_TEXT_1 t_log_message-MSG_TEXT_2
t_log_message-MSG_TEXT_3 t_log_message-MSG_TEXT_4
into l_dummy.

* The parameters set by message statement will be used
* Add the message in the log
PERFORM msg_add.
endloop.


* save logs in the database
CALL FUNCTION 'BAL_DB_SAVE'
EXPORTING
I_SAVE_ALL = 'X'
EXCEPTIONS
LOG_NOT_FOUND = 1
SAVE_NOT_ALLOWED = 2
NUMBERING_ERROR = 3
OTHERS = 4
.
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

endif.


ENDFUNCTION.


*--------------------------------------------------------------------
* FORM MSG_ADD
*--------------------------------------------------------------------
* Add the message to the log
*-------------------------------------------------------------------*
FORM msg_add.
DATA:
l_s_msg TYPE bal_s_msg.

* define data of message for Application Log
l_s_msg-msgty = sy-msgty.
l_s_msg-msgid = sy-msgid.
l_s_msg-msgno = sy-msgno.
l_s_msg-msgv1 = sy-msgv1.
l_s_msg-msgv2 = sy-msgv2.
l_s_msg-msgv3 = sy-msgv3.
l_s_msg-msgv4 = sy-msgv4.

* add this message to log file
* (I_LOG_HANDLE is not specified, we want to add to the default log.
* If it does not exist we do not care =>EXCEPTIONS log_not_found = 0)
CALL FUNCTION 'BAL_LOG_MSG_ADD'
EXPORTING
* I_LOG_HANDLE =
i_s_msg = l_s_msg
EXCEPTIONS
log_not_found = 0
OTHERS = 1.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

ENDFORM.

Using the function module:
In your ABAP program write the following code and use this function module ZIU_MESSAGE_LOGGING for logging.


Data declaration->
* Internal table for message logging
DATA: it_log_message TYPE STANDARD TABLE OF z_log_message,
wa_log_message TYPE z_log_message,
l_ext_number TYPE string.

Constants:
c_obj_zxiproxy TYPE balobj_d VALUE 'ZTESTLOG'.

* Now populate the internal table with the log messages as shown below. wa_log_message-
* msgty is the type of the message.
* E -> Error, W -> Warning, S -> Success

* Logging code for insert message into log
CLEAR wa_log_message.
wa_log_message-msgty = 'E'. " Can use W or S
wa_log_message-msg_text_1 = < Message text 1>.
wa_log_message-msg_text_2 = < Message text 2>
wa_log_message-msg_text_3 = < Message text 3>
wa_log_message-msg_text_4 = < Message text 4>

* Append the message into the internal table
APPEND wa_log_message TO it_log_message.

At the end transfer the log message to the system log by calling function module ZIU_MESSAGE_LOGGING. L_EXT_NUMBER will bt any string of your choice.

* Function module ZIU_MESSAGE_LOGGING will do the logging
* i_log_object is the object type (to be configrd using txn SLG0

CALL FUNCTION 'ZIU_MESSAGE_LOGGING'
EXPORTING
i_log_object = c_obj_zxiproxy
i_extnumber = l_ext_number
TABLES
t_log_message = it_log_message
EXCEPTIONS
log_header_inconsistent = 1
logging_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDIF.

The logged messages can be viewed in transaction SLG1. For filtering the messages use the Object type.
sapmanie
 
Posts: 463
Joined: Fri Jan 23, 2004 10:39 am

Postby mr on Wed Feb 21, 2007 12:21 pm

sapmanie wrote:Found a how-to of the application logging functionality for you:

Author: Ashim Chowdhury

This article explains how to create your own application logging program in ABAP.
SAP provides standard functionality to write into as well as view the application log. This article will explain in detail how to create your own logging function module for application logging using standard SAP log facility. Two transaction code related to application logging are
? SLG0 -> Used to maintain the log object
? SLG1 -> Used to view the log

Configuration step:
In this step create a log object.
1. Open transaction SLG0. An information message about cross-client table will come.
2. Click the button "New Entries". Enter the name and description of your log object (say ZTESTLOG as object name and "Test Log" as object description) and save.

Development Step:
The following standard function modules provided by SAP has been used to create the Z function module for logging.
? BAL_GLB_MSG_DEFAULTS_SET
? BAL_DB_SAVE
? BAL_LOG_MSG_ADD

Here are the steps:

1. Go to transaction SE11. Create a z-structure Z_LOG_MESSAGE having the following fields

Component Component Type
MSGTY SYMSGTY
MSG_TEXT_1 SYMSGV
MSG_TEXT_2 SYMSGV
MSG_TEXT_3 SYMSGV
MSG_TEXT_4 SYMSGV

2. Crate a message class say ZMESSAGE in transaction SE91 and a message 999 with four placeholder ( & ) as the text.
3. Go to transaction SE37. Create a function group say ZLOG

4. After the function group is created, create a function module in that group. Let is name it ZIU_MESSAGE_LOGGING.

Import parameters:
a. I_LOG_OBJECT type BALOBJ_D -> Application log: Object name (Application code)
b. I_EXTNUMBER type String -> Application Log: External ID

Export parameters: None
Changing parameters: None
Tables parameters:
T_LOG_MESSAGE type Z_LOG_MESSAGE
Exceptions:
LOG_HEADER_INCONSISTENT
LOGGING ERROR

Paste the code below as the source code

FUNCTION ZIU_MESSAGE_LOGGING.
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" REFERENCE(I_LOG_OBJECT) TYPE BALOBJ_D
*" REFERENCE(I_EXTNUMBER) TYPE STRING
*" TABLES
*" T_LOG_MESSAGE STRUCTURE Z_LOG_MESSAGE
*" EXCEPTIONS
*" LOG_HEADER_INCONSISTENT
*" LOGGING_ERROR
*"----------------------------------------------------------------------

* Author :Ashim Chowdhury

* DESCRIPTION: This function module is used insert messages in the
* application log

CONSTANTS: c_message TYPE syst-msgid VALUE 'ZMESSAGE',
c_999 TYPE syst-msgno VALUE '999'.


DATA:
l_log_handle TYPE balloghndl,
l_s_log TYPE bal_s_log,
l_dummy type string,
l_ext_no type bal_s_log-extnumber,
l_s_mdef TYPE bal_s_mdef.


if T_LOG_MESSAGE[] is not initial.

l_s_log-object = I_LOG_OBJECT.
l_ext_no = I_EXTNUMBER.
l_s_log-extnumber = l_ext_no.

* Create the log with header data
CALL FUNCTION 'BAL_LOG_CREATE'
EXPORTING
i_s_log = l_s_log
IMPORTING
E_LOG_HANDLE = l_log_handle
EXCEPTIONS
LOG_HEADER_INCONSISTENT = 1
OTHERS = 2.

IF sy-subrc <> 0.
case sy-subrc.
when 1.
raise LOG_HEADER_INCONSISTENT.
when others.
raise LOGGING_ERROR.
endcase.
ENDIF.

l_s_mdef-log_handle = l_log_handle.

* Set the default value
CALL FUNCTION 'BAL_GLB_MSG_DEFAULTS_SET'
EXPORTING
i_s_msg_defaults = l_s_mdef
EXCEPTIONS
OTHERS = 0.

* Loop the message table and write the messages into the log
loop at T_LOG_MESSAGE.

* Use the message type ZMESSAGE and msg no 999
* Issue the message in a dummy variable
message ID C_message type t_log_message-MSGTY number C_999
with t_log_message-MSG_TEXT_1 t_log_message-MSG_TEXT_2
t_log_message-MSG_TEXT_3 t_log_message-MSG_TEXT_4
into l_dummy.

* The parameters set by message statement will be used
* Add the message in the log
PERFORM msg_add.
endloop.


* save logs in the database
CALL FUNCTION 'BAL_DB_SAVE'
EXPORTING
I_SAVE_ALL = 'X'
EXCEPTIONS
LOG_NOT_FOUND = 1
SAVE_NOT_ALLOWED = 2
NUMBERING_ERROR = 3
OTHERS = 4
.
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

endif.


ENDFUNCTION.


*--------------------------------------------------------------------
* FORM MSG_ADD
*--------------------------------------------------------------------
* Add the message to the log
*-------------------------------------------------------------------*
FORM msg_add.
DATA:
l_s_msg TYPE bal_s_msg.

* define data of message for Application Log
l_s_msg-msgty = sy-msgty.
l_s_msg-msgid = sy-msgid.
l_s_msg-msgno = sy-msgno.
l_s_msg-msgv1 = sy-msgv1.
l_s_msg-msgv2 = sy-msgv2.
l_s_msg-msgv3 = sy-msgv3.
l_s_msg-msgv4 = sy-msgv4.

* add this message to log file
* (I_LOG_HANDLE is not specified, we want to add to the default log.
* If it does not exist we do not care =>EXCEPTIONS log_not_found = 0)
CALL FUNCTION 'BAL_LOG_MSG_ADD'
EXPORTING
* I_LOG_HANDLE =
i_s_msg = l_s_msg
EXCEPTIONS
log_not_found = 0
OTHERS = 1.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

ENDFORM.

Using the function module:
In your ABAP program write the following code and use this function module ZIU_MESSAGE_LOGGING for logging.


Data declaration->
* Internal table for message logging
DATA: it_log_message TYPE STANDARD TABLE OF z_log_message,
wa_log_message TYPE z_log_message,
l_ext_number TYPE string.

Constants:
c_obj_zxiproxy TYPE balobj_d VALUE 'ZTESTLOG'.

* Now populate the internal table with the log messages as shown below. wa_log_message-
* msgty is the type of the message.
* E -> Error, W -> Warning, S -> Success

* Logging code for insert message into log
CLEAR wa_log_message.
wa_log_message-msgty = 'E'. " Can use W or S
wa_log_message-msg_text_1 = < Message text 1>.
wa_log_message-msg_text_2 = < Message text 2>
wa_log_message-msg_text_3 = < Message text 3>
wa_log_message-msg_text_4 = < Message text 4>

* Append the message into the internal table
APPEND wa_log_message TO it_log_message.

At the end transfer the log message to the system log by calling function module ZIU_MESSAGE_LOGGING. L_EXT_NUMBER will bt any string of your choice.

* Function module ZIU_MESSAGE_LOGGING will do the logging
* i_log_object is the object type (to be configrd using txn SLG0

CALL FUNCTION 'ZIU_MESSAGE_LOGGING'
EXPORTING
i_log_object = c_obj_zxiproxy
i_extnumber = l_ext_number
TABLES
t_log_message = it_log_message
EXCEPTIONS
log_header_inconsistent = 1
logging_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDIF.

The logged messages can be viewed in transaction SLG1. For filtering the messages use the Object type.


I'd use a macro instead of a FM, using ABAP memory to pass the error data...

MESSAGE xnnn(mid) WITH var INTO v_dummy.
BALMSG_ADD.

Then in macro BALMSG_ADD definition, use sy-msgty, sy-msgid, etc. to fill FM BAL_LOG_MSG_ADD input parameter...

...the same dog with another cord

mr.
mr
 
Posts: 285
Joined: Wed Jun 25, 2003 11:52 am
Location: Uruguay

Postby panda2006 on Thu Feb 22, 2007 12:41 am

Thanks all.. the one is from maen is really good.. very smart.

karina
panda2006
 
Posts: 88
Joined: Tue Apr 18, 2006 2:27 am


Return to ABAP

Who is online

Users browsing this forum: Google Adsense [Bot] and 9 guests



This website is not affiliated with, sponsored by, or approved by SAP AG.