通过用户出口为VA02特定单据类型添加必填字段咨询
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 transactionSE91if you don’t already have one. Add message number001with the text above. - Using an
E-type message ensures the document isn’t saved until the user fills in the mandatory field.
Step 3: Make the Field Visibly Mandatory on the Screen (Optional but Recommended)
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
CMODand create a new project (or use an existing one). - Add the enhancement
V45A0002(which contains bothUSEREXIT_SAVE_DOCUMENT_PREPAREandUSEREXIT_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

