You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过用户出口为VA02特定单据类型添加必填字段咨询

How to Implement Mandatory Field Validation in SD User Exit SAPMV45A

Got it, let's walk through this step by step since you're new to user exit customizations in SAP SD. You’ve already picked the right exit—USEREXIT_SAVE_DOCUMENT_PREPARE in program SAPMV45A is perfect for this because it executes right before the sales document is saved, so we can catch missing fields before any data is written to the database.

Step 1: Access Screen Fields in the User Exit

First off, you don’t need any special calls to access VBAK fields here. Since SAPMV45A is the main program for sales order processing, the global structure VBAK is directly available in this user exit. You can reference fields like VBAK-AUART, VBAK-AUGRU, and VBAK-SUMBI directly in your code.

Step 2: Add Validation Logic to Block Save if Field is Missing

We’ll add an IF condition to check your criteria, then throw an error message if VBAK-SUMBI is empty when the conditions are met. Here’s the code snippet to insert into the user exit:

" Check if document type and reason code match the required criteria
IF ( VBAK-AUART = 'CR' OR VBAK-AUART = 'CRQ' OR VBAK-AUART = 'RE' )
    AND ( VBAK-AUGRU = '145' OR VBAK-AUGRU = '160' ).

  " Verify if SUMBI is empty
  IF VBAK-SUMBI IS INITIAL.
    " Replace ZSD with your custom message class, and 001 with your message number
    MESSAGE e001(zsd) WITH 'Reason for Rejection (SUMBI) is mandatory for this document type and reason code.'
      DISPLAY LIKE 'E'.
    " The E-type message will immediately stop the save process and alert the user
  ENDIF.

ENDIF.

Notes on the Message:

  • You’ll need to create a custom message class (e.g., ZSD) using transaction SE91 if you don’t already have one. Add message number 001 with the text above.
  • Using an E-type message ensures the document isn’t saved until the user fills in the mandatory field.

If you want the SUMBI field to show a mandatory asterisk (*) on the sales order screen when your criteria are met, you’ll need to use another user exit: USEREXIT_SCREEN_MODIFY (also in SAPMV45A). This exit runs during the screen’s Process Before Output (PBO) phase, allowing you to dynamically adjust field properties.

Here’s the code for this exit:

" Check if we're targeting the SUMBI field
IF SCREEN-NAME = 'VBAK-SUMBI'.

  " Apply mandatory status only if our criteria are met
  IF ( VBAK-AUART = 'CR' OR VBAK-AUART = 'CRQ' OR VBAK-AUART = 'RE' )
      AND ( VBAK-AUGRU = '145' OR VBAK-AUGRU = '160' ).
    SCREEN-REQUIRED = '1'. " Set field to mandatory
  ELSE.
    SCREEN-REQUIRED = '0'. " Reset to optional if criteria aren't met
  ENDIF.

  MODIFY SCREEN. " Save the screen field changes

ENDIF.

Step 4: Activate the Enhancement

To make your changes take effect:

  • Go to transaction CMOD and create a new project (or use an existing one).
  • Add the enhancement V45A0002 (which contains both USEREXIT_SAVE_DOCUMENT_PREPARE and USEREXIT_SCREEN_MODIFY).
  • Activate the project.

That’s it! Now when users create or modify sales orders matching your criteria, they’ll be forced to fill in SUMBI before saving, and the field will show as mandatory on the screen.

内容的提问来源于stack exchange,提问作者ehyatt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:12:12