ERPClimb logoERPClimb

Code examples · Modern ABAP · Intermediate

User exit in sales orders: stop the save when an item quantity is too high (MV45AFZZ)

The problem

The business wants a sales order with an item quantity above 1000 to be stopped at save time, with a message that names the item.

How we solve it

  1. 1Find the exitMV45AFZZ
  2. 2Loop and testXVBAP
  3. 3Stop the saveerror message

The result

Saving an order with a quantity above 1000 shows an error and the order is not saved.

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

1. The requirement

The business wants a sales order with an item quantity above 1000 to be stopped at save time, with a message that names the item. All other orders must save normally.

2. How we design it

SAP's sales document program (SAPMV45A) calls empty FORM routines that are meant for customer code. They are called user exits and they sit in the includes MV45AFZZ, MV45AFZA and MV45AFZB. USEREXIT_SAVE_DOCUMENT_PREPARE is called immediately before the save and is meant for changes and checks. The item table XVBAP holds the items of the current document, so we loop over it and test the quantity.

Why we decided it this way

  1. 1Use the PREPARE exit, not SAVE_DOCUMENTSAVE_DOCUMENT is meant for writing your own tables. Checks and changes of the document belong to PREPARE, which runs before the document is finalised.
  2. 2Do not test sy-tcode or sy-ucommThe same exit runs when orders are created by BAPI or batch input. A check on screen codes would silently skip those.
  3. 3Skip deleted itemsA deleted item is still in XVBAP with update flag D. It must not block the save.

Before you start

  • A development system and a transport request. You change a customer include of an SAP program.
  • Display of include MV45AFZZ in SE38 or SE80. Look at the comments of USEREXIT_SAVE_DOCUMENT_PREPARE first.

3. The code, step by step

Step 1 of 3: Open the exit and limit it to sales orders

Code that only runs for sales orders.

ABAP · Include · MV45AFZZ (FORM userexit_save_document_prepare, part 1)
" The empty FORM routine is already there
FORM userexit_save_document_prepare.
  CHECK vbak-vbtyp = 'C'.
Part by part (plain text)
  1. The empty FORM routine is already there. In SE38 open MV45AFZZ and find FORM userexit_save_document_prepare. It exists with comments and no code. VBAK-VBTYP = 'C' is the document category sales order; the CHECK leaves the routine for quotations, contracts and other categories.

Open in the portal

The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.

SAP tables

Transaction codes

4. The final outcome

Saving an order with a quantity above 1000 shows an error and the order is not saved.

What you should see

Item 000010: quantity 1200 is above the limit of 1000 (order stays in change mode, nothing is saved)

Try it and check the result

  • Create a sales order in VA01, enter a quantity of 1200 in one item and save: the error appears and the order is not created.
  • Reduce the quantity to 900 and save: the order is created.
  • Set a breakpoint in the FORM and create an order by BAPI_SALESORDER_CREATEFROMDAT2 with test run: SAP notes that the save user exits are not called in simulation, so the check does not run.

Common mistakes

  • After the error message the screen may call the exit again on the next Enter. Some projects set the function code to 'ENT1' before the message to avoid a loop; test this on your release.
  • User exits run for every channel: dialog, BAPI, batch input and IDoc. Keep the code free of screen assumptions.
  • Changing an SAP include is a customer modification. Keep the code small: call your own class from the exit and put the logic there, so it can be unit-tested.
  • XVBAP holds the new state and YVBAP the old state. Check changes by comparing both when you only want to react to changed fields.

Keep learning