Skip to Content
Author's profile photo Suresh Karri


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 '' '' ''. 


*In Case of No error, call the macro to remove message
mmpur_remove_msg_by_context lwa_hdr-id 910.

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,

mmpur_remove_msg_by_context lwa_item-id 921 . " Needed if Message is to be removed                                          "upon user correction



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.

mmpur_remove_msg_by_context lwa_item-id 921 .



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.

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member



      Nice blog for guys who never worked on MM,I think i used this message macro's 3 years back.




      Author's profile photo Suresh Karri
      Suresh Karri
      Blog Post Author

      Thanks Trevor.

      Author's profile photo Bharath Padmanabhan
      Bharath Padmanabhan

      Fantastic blog. Exactly what I was looking for today for my requirement. Thank you so much for creating this blog.

      Author's profile photo Prakash Alavandi
      Prakash Alavandi

      Hi Suresh,

      Once we display any Custom Error message from the Check Method, after correcting the error we are still getting the old messages. Tried deleting the Old messages using Methods from class cl_message_handler_mm.

      How to delete custom error message but not delete SAP standard error message in PO badi IF_EX_ME_PROCESS_PO_CUST.

      Can you please help here.


      Prakash A