{"id":7941,"date":"2025-10-30T23:42:48","date_gmt":"2025-10-30T18:12:48","guid":{"rendered":"https:\/\/www.sapewmhelp.com\/?question=adding-validations-for-fields-in-a-module-pool-dynpro-in-sap"},"modified":"2025-10-30T23:42:48","modified_gmt":"2025-10-30T18:12:48","slug":"adding-validations-for-fields-in-a-module-pool-dynpro-in-sap","status":"publish","type":"question","link":"https:\/\/www.sapewmhelp.com\/?question=adding-validations-for-fields-in-a-module-pool-dynpro-in-sap","title":{"rendered":"Adding validations for fields in a Module Pool (Dynpro) in SAP"},"content":{"rendered":"<p><strong>Step 1: Identify Where to Add Validation<\/strong><\/p>\n<p>In <strong>Module Pool (ABAP Dynpro) programming<\/strong>, validations are usually added in:<\/p>\n<ol>\n<li>\n<p><strong>PBO (Process Before Output)<\/strong> \u2013 usually for initial field checks or setting default values.<\/p>\n<\/li>\n<li>\n<p><strong>PAI (Process After Input)<\/strong> \u2013 <strong>most common place for validations<\/strong> after the user inputs data and presses a button.<\/p>\n<\/li>\n<\/ol>\n<p>The <strong>PAI module<\/strong> is triggered when a user interacts with a screen element (like pressing Enter, or a function key).<\/p>\n<\/p>\n<p><strong>Step 2: Define Field in Screen<\/strong><\/p>\n<p>Suppose you have a screen (<code>0100<\/code>) with a field <code>S_MATNR<\/code> (Material Number).<\/p>\n<ol>\n<li>\n<p>Go to <strong>Screen Painter (SE51)<\/strong>.<\/p>\n<\/li>\n<li>\n<p>Add your input field (<code>S_MATNR<\/code>) and assign a <strong>name<\/strong>.<\/p>\n<\/li>\n<li>\n<p>Assign a <strong>Module<\/strong> in the PAI Flow Logic (we\u2019ll validate in this module):<\/p>\n<\/li>\n<\/ol>\n<div class=\"contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary\">\n<div class=\"sticky top-9\">\n<div class=\"absolute end-0 bottom-0 flex h-9 items-center pe-2\">\n<div class=\"bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs\"><\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\"><code><span>PROCESS AFTER INPUT.<\/span><\/code><\/div>\n<div class=\"overflow-y-auto p-4\"><code><span>  MODULE validate_material INPUT.<\/span><\/code><\/div>\n<\/div>\n<div>\n<p><strong>Step 3: Write Validation Logic in ABAP Module<\/strong><\/p>\n<p>Go to your <strong>program<\/strong> and write the module:<\/p>\n<p>MODULE validate_material INPUT.<br \/>\n&#8221; Check if field is empty<br \/>\nIF s_matnr IS INITIAL.<br \/>\nMESSAGE &#8216;Material number cannot be empty&#8217; TYPE &#8216;E&#8217;.<br \/>\nENDIF.<\/p>\n<p>&#8221; Check if material exists in MARA table<br \/>\nDATA: lv_count TYPE i.<br \/>\nSELECT COUNT(*) INTO lv_count<br \/>\nFROM mara<br \/>\nWHERE matnr = s_matnr.<\/p>\n<p>IF lv_count = 0.<br \/>\nMESSAGE &#8216;Material does not exist&#8217; TYPE &#8216;E&#8217;.<br \/>\nENDIF.<br \/>\nENDMODULE.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong>Step 4: Optional \u2013 Validate on Field Exit<\/strong><\/p>\n<p>If you want <strong>validation when leaving a field<\/strong>, you can use <strong>Field Exit<\/strong> or <strong>MODULE \u2026 AT EXIT-COMMAND<\/strong>:<\/p>\n<p>MODULE validate_matnr_onexit INPUT.<br \/>\nIF s_matnr IS INITIAL.<br \/>\nMESSAGE &#8216;Material cannot be empty&#8217; TYPE &#8216;E&#8217;.<br \/>\nENDIF.<br \/>\nENDMODULE.<\/p>\n<p><strong>Step 5: Common Validation Techniques<\/strong><\/p>\n<ol>\n<li>\n<p><strong>Check for mandatory fields<\/strong> \u2192 <code>IS INITIAL<\/code>.<\/p>\n<\/li>\n<li>\n<p><strong>Check for valid values in DB<\/strong> \u2192 <code>SELECT<\/code> from tables (MARA, KNA1, etc.).<\/p>\n<\/li>\n<li>\n<p><strong>Cross-field validation<\/strong> \u2192 compare values of multiple fields.<\/p>\n<\/li>\n<li>\n<p><strong>Custom messages<\/strong> \u2192 Use <code>MESSAGE<\/code> statement with message class.<\/p>\n<\/li>\n<\/ol>\n<p>MODULE validate_data INPUT.<br \/>\nIF s_qty &lt;= 0.<br \/>\nMESSAGE &#8216;Quantity must be greater than zero&#8217; TYPE &#8216;E&#8217;.<br \/>\nENDIF.<\/p>\n<p>IF s_matnr IS INITIAL AND s_kunnr IS INITIAL.<br \/>\nMESSAGE &#8216;Material or Customer must be entered&#8217; TYPE &#8216;E&#8217;.<br \/>\nENDIF.<br \/>\nENDMODULE.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","question-category":[158],"question_tags":[178,450,177],"class_list":["post-7941","question","type-question","status-publish","hentry","question-category-abap","question_tags-abap","question_tags-mpp","question_tags-sap"],"_links":{"self":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/question\/7941","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/question"}],"about":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/types\/question"}],"author":[{"embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7941"}],"wp:attachment":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7941"}],"wp:term":[{"taxonomy":"question-category","embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fquestion-category&post=7941"},{"taxonomy":"question_tags","embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fquestion_tags&post=7941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}