Definition: Message classes are created within an ABAP project at the ABAP package level. They are used to group messages within a development object. So, you can combine related messages and reuse them in other programs. After creating a message class, you add the individual messages.
Message: Messages notify a user about unexpected behaviour, an error, a status, or a result of an action. An application will display the message at runtime. A message is specified by a unique 3-digit message number, a single-digit language key, text information, and a message class.
Some of you might think that , hey we have SAP UI5 on cloud for front end , so why do we need a message from backend to show messages ?
Front end is not aware of every action or event happening at backend so we need meaningful response
The fact is, UI could bind dynamic responses , so that whatever happens at backend could be shown via messages .
Step 1 : Right click on Package -> New -> Other ABAP Repository Object.
Message: Messages notify a user about unexpected behaviour, an error, a status, or a result of an action. An application will display the message at runtime. A message is specified by a unique 3-digit message number, a single-digit language key, text information, and a message class.
Why do we need message class in cloud ?
Some of you might think that , hey we have SAP UI5 on cloud for front end , so why do we need a message from backend to show messages ?
Front end is not aware of every action or event happening at backend so we need meaningful response
The fact is, UI could bind dynamic responses , so that whatever happens at backend could be shown via messages .
Creating Message Class :
Step 1 : Right click on Package -> New -> Other ABAP Repository Object.
Step 2 : Choose Message Classes -> Next
Update : Earlier message classes were found under Message Class section after the latest update its under Text.
Step 3 : Give a name and Description -> Next
Step 4 : Choose Transport Request-> Next
Step 5 : now our message class should look like:
Step 6 : Insert some Messages and their Numbers, By clicking the respective cells . Keep in mind that the message number is only three digits long. After entering the specific number of messages save the class (Activation is not required or not permitted for message classes , saving is enough.). Until you save the message there will be a lock symbol in the lock column , lock symbol indicates that the changes in the respective row is not saved .
◉ Before Saving:
◉ After Saving:
‘&’ – Is the Parameter to be passed to the message , which will replace the symbol with the value of the passed parameter at run time , currently message classes in sap cloud platform support up to 4 parameters , first symbol will be replaced with first parameter , second symbol with second parameter and so on .
Using Message Classes: Custom Messages using the message class could be used thrown from a behaviour implementation class or a function module / class-method called/ initiated from a behaviour implementation. Sadly Custom Entities does not support these messages to be thrown. These messages could be thrown from create/update/delete methods or from save method according to the need .
Since we have three messages configured for Create, Update and Delete we will respective messages from each method.
Basic Syntax for creation and returning messages:
DATA(item_msg) = new_message( id = 'Z_MESSAGE_CLASS' " id = Name Of message class
number = '001' "number of message defined in the message class
severity = cl_abap_behv=>ms-success "type of message
v1 = ls_create-id "First Parameter
v2 = '' "Second Parameter
v3 = '' "Third Parameter
v4 = '' "Fourth Parameter
).
************************** Apeending the Message Response ************************
APPEND VALUE #( %cid = ls_create-%cid " %cid = "Content ID" in ABAP Behavior
id = ls_create-id "key
%msg = item_msg "%msg = type ref to if_abap_behv_message / Message to be passed
) TO reported-zdatadefinition. "type response for reported early zdatadefinition
Now let’s use the message in a behaviour Implementation :
This is a basic behaviour Implementation for a CDS.
CLASS lcl_buffer DEFINITION.
* 1) define the data buffer
PUBLIC SECTION.
TYPES: BEGIN OF ty_buffer.
INCLUDE TYPE ztable AS lv_table.
TYPES: flag TYPE c LENGTH 1,
END OF ty_buffer.
CLASS-DATA mt_buffer TYPE TABLE OF ty_buffer.
ENDCLASS.
CLASS lhc_zdatadefinition DEFINITION INHERITING FROM cl_abap_behavior_handler.
PRIVATE SECTION.
METHODS create FOR MODIFY
IMPORTING entities FOR CREATE zdatadefinition.
METHODS delete FOR MODIFY
IMPORTING keys FOR DELETE zdatadefinition.
METHODS update FOR MODIFY
IMPORTING entities FOR UPDATE zdatadefinition.
METHODS read FOR READ
IMPORTING keys FOR READ zdatadefinition RESULT result.
ENDCLASS.
CLASS lhc_zdatadefinition IMPLEMENTATION.
METHOD create.
LOOP AT entities INTO DATA(ls_create).
INSERT VALUE #( flag = 'C' lv_table = CORRESPONDING #( ls_create-%data ) ) INTO TABLE lcl_buffer=>mt_buffer.
*********************Message Handling Create****************
************C reating Message using Message Class *************
DATA(item_msg) = new_message( id = 'Z_MESSAGE_CLASS' " id = Name Of message class
number = '001' "number of message defined in the message class
severity = cl_abap_behv=>ms-success "type of message
v1 = ls_create-id "First Parameter
v2 = '' "Second Parameter
v3 = '' "Third Parameter
v4 = '' "Fourth Parameter
).
************ Apeending the Message Response *************
APPEND VALUE #( %cid = ls_create-%cid " %cid = "Content ID" in ABAP Behavior
id = ls_create-id "key
%msg = item_msg "%msg = type ref to if_abap_behv_message / Message to be passed
) TO reported-zdatadefinition. "type response for reported early zdatadefinition
********************Message End****************
ENDLOOP.
ENDMETHOD.
METHOD delete.
LOOP AT keys INTO DATA(ls_delete).
READ TABLE lcl_buffer=>mt_buffer
ASSIGNING FIELD-SYMBOL(<ls_buffer>) WITH KEY id = ls_delete-id.
" not yet in buffer.
INSERT VALUE #( flag = 'D' id = ls_delete-id ) INTO TABLE lcl_buffer=>mt_buffer.
********************Message Handling Delete******************
***********C reating Message using Message Class ****************
DATA(item_msg) = new_message( id = 'Z_MESSAGE_CLASS' " id = Name Of message class
number = '003' "number of message defined in the message class
severity = cl_abap_behv=>ms-success "type of message
v1 = ls_delete-id "First Parameter
v2 = '' "Second Parameter
v3 = '' "Third Parameter
v4 = '' "Fourth Parameter
).
********************* Apeending the Message Response ***************
APPEND VALUE #( %cid = ls_delete-%cid_ref " %cid = "Content ID" in ABAP Behavior
id = ls_delete-id "key
%msg = item_msg "%msg = type ref to if_abap_behv_message / Message to be passed
) TO reported-zdatadefinition. "type response for reported early zdatadefinition
*********************Message End***************
ENDLOOP.
ENDMETHOD.
METHOD update.
LOOP AT entities INTO DATA(ls_update).
READ TABLE lcl_buffer=>mt_buffer
ASSIGNING FIELD-SYMBOL(<ls_buffer>) WITH KEY id = ls_update-id.
" not yet in buffer, read from table
SELECT SINGLE * FROM ztable WHERE id = @ls_update-id INTO @DATA(ls_db).
INSERT VALUE #( flag = 'U' lv_table = ls_db ) INTO TABLE lcl_buffer=>mt_buffer ASSIGNING <ls_buffer>.
IF ls_update-%control-name IS NOT INITIAL..
<ls_buffer>-name = ls_update-name.
ENDIF.
*******************Message Handling Delete********************
************C reating Message using Message Class ************
DATA(item_msg) = new_message( id = 'Z_MESSAGE_CLASS' " id = Name Of message class
number = '002' "number of message defined in the message class
severity = cl_abap_behv=>ms-success "type of message
v1 = ls_update-id "First Parameter
v2 = '' "Second Parameter
v3 = '' "Third Parameter
v4 = '' "Fourth Parameter
).
************ Apeending the Message Response *********************
APPEND VALUE #( %cid = ls_update-%cid_ref " %cid = "Content ID" in ABAP Behavior
id = ls_update-id "key
%msg = item_msg "%msg = type ref to if_abap_behv_message / Message to be passed
) TO reported-zdatadefinition. "type response for reported early zdatadefinition
***********Message End*************
ENDLOOP.
ENDMETHOD.
METHOD read.
ENDMETHOD.
ENDCLASS.
CLASS lsc_zdatadefinition DEFINITION INHERITING FROM cl_abap_behavior_saver.
PROTECTED SECTION.
METHODS check_before_save REDEFINITION.
METHODS finalize REDEFINITION.
METHODS save REDEFINITION.
ENDCLASS.
CLASS lsc_zdatadefinition IMPLEMENTATION.
METHOD check_before_save.
ENDMETHOD.
METHOD finalize.
ENDMETHOD.
METHOD save.
DATA lt_table TYPE STANDARD TABLE OF ztable.
" find all rows in buffer with flag = created
lt_table = VALUE #( FOR row IN lcl_buffer=>mt_buffer WHERE ( flag = 'C' ) ( row-lv_table ) ).
IF lt_table IS NOT INITIAL.
INSERT ztable FROM TABLE @lt_table.
ENDIF.
" find all rows in buffer with flag = updated
lt_table = VALUE #( FOR row IN lcl_buffer=>mt_buffer WHERE ( flag = 'U' ) ( row-lv_table ) ).
IF lt_table IS NOT INITIAL.
UPDATE ztable FROM TABLE @lt_table.
ENDIF.
" find all rows in buffer with flag = deleted
lt_table = VALUE #( FOR row IN lcl_buffer=>mt_buffer WHERE ( flag = 'D' ) ( row-lv_table ) ).
IF lt_table IS NOT INITIAL.
DELETE ztable FROM TABLE @lt_table.
ENDIF.
CLEAR lcl_buffer=>mt_buffer.
ENDMETHOD.
ENDCLASS.
Before use of custom messages:
◉ Creating:
◉ Updating:
◉ Deleting:
After use of custom messages :
◉ Creating:
◉ Updating:
◉ Deleting:
Types of messages:
Message classes support different types of messages which will be available to front end via responses, Error messages will be in error response , Success messages will be retuned in Success .
1. Error Messages.
severity = cl_abap_behv=>ms-error
2. None Messages
severity = cl_abap_behv=>ms-none
3. Warning Messages
severity = cl_abap_behv=>ms-warning
4. Information Messages
severity = cl_abap_behv=>ms-warning
5. Success Messages
severity = cl_abap_behv=>ms-success
No comments:
Post a Comment