This Blog Talks about handling custom errors when creating/changing SAP Purchase orders both at header level and item level. It also tries to explain error handling using the standard Macros mmpur_context and mmcnt_context_badi.
Header Level Error Handling using Method PROCESS_HEADER of the BADI
Make sure you add the include which holds Macors for message handling ( as shown below)
One example we can look at is, raise error message when a service PO Validity date entered by user should no be less that current date.
INCLUDE mm_messages_mac. "useful macros for message handling
*Get Header details
DATA: lwa_hdr TYPE mepoheader.
* Get Document Type
lwa_hdr = im_header->get_data( ).
( Below is example where we want raise error when validity period is less than current date)
IF lwa_hdr-kdatb < sy-datlo.
*STEP 1 --> Invalidate Header object
* invalidate the object
CALL METHOD im_header->invalidate( ).
*Add a custom Error Context ( the number is important to remove messages when required)
mmpur_context 910.
mmpur_message_forced 'E' 'ZXYZ' '007' lv_msgv1 '' '' ''.
ELSE.
*In Case of No error, call the macro to remove message
mmpur_remove_msg_by_context lwa_hdr-id 910.
ENDIF.
Item Level Error Handling using Method PROCESS_ITEM of the BADI
Example –> Raise Error Message at item level to make sure that only item category K or 2 ( Consignment) is allowed if the material entered has the field SOBSL (Special Procurement Type) is set to 10 ( Consignment) .
DATA: lwa_item TYPE mepoitem
* im_item of PROCESS_ITEM can be used as a referance to get item data
lwa_item = im_item->get_data( ).
IF lwa_item-matnr IS NOT INITIAL.
lv_msgv1 = lwa_item-ebelp.
mmpur_context 921. " New Context 921 to be added
mmpur_business_obj_id lwa_item-id. " To ensure right item gets flagged for error
mmpur_message_forced 'E' 'ZXYZ' '005' lv_msgv1 '' '' ''.
* invalidate the object
CALL METHOD lr_item->invalidate( ). " Invlidate Ite,
ELSE.
mmpur_remove_msg_by_context lwa_item-id 921 . " Needed if Message is to be removed "upon user correction
ENDIF
Importance of METHOD CHECK in BADI
In Some instances we have seen that the Methods PROCESS_HEADER & PROCESS_ITEM are not called , for example when creating Purchase Order with reference to Contract or when creating Purchase Order by copying existing PO. ( This is the behavior that we noticed, so any suggestions or comments are welcome) . In such instances we need to add validation in Method CHECK, as this method gets called during CHECK & SAVE Events of PO.
The Key thing in CHECK PO is to make sure we set the flag CH_FAILED to ‘X’ when custom validation fails
IF " Custom Validation Failed
lv_msgv1 = lwa_item-ebelp.
mmpur_context 921.
mmpur_business_obj_id lwa_item-id.
mmpur_message_forced 'E' 'ZXYZ' '005' lv_msgv1 '' '' ''.
* invalidate the object
CALL METHOD lr_item->invalidate( ).
ch_failed = abap_true.
ELSE.
mmpur_remove_msg_by_context lwa_item-id 921 .
ENDIF.
Importance of BADI ME_HOLD_PO
Even after adding custom error messages as mentioned earlier, SAP allows the PO to be saved in status HOLD. If there is a requirement to make sure that a PO does not get saved until all the errors are corrected, we can use BADI “ME_HOLD_PO”. All you need to do is create implementation and set the flag CH_ALLOWED = SPACE.
Hi
Nice blog for guys who never worked on MM,I think i used this message macro’s 3 years back.
Regards,
Trevor
Thanks Trevor.