When we need to maintain values for a custom table in a table maintenance generator, certain situations may arise where we need to automatically populate another field in a table depending on the value of one field. Or we may have some custom requirement like validating a particular entry before it is saved. There can be various other requirements which we may face when we are maintaining data a custom table.
For example say we have a custom table ZTEST_MAT.
After the TMG is successfully created, open it again in display mode and get the source code name for the TMG. It will be a module pool program.
Put your custom module in the PAI Module. Here MODULE MODIFY_RECORD is the module in which we have put our code.
In this custom module we can write our code to, say, automatically populate the material description when the material number is entered.
In the sample code, you would notice we have the internal table ‘TOTAL’ which contains all the table records and also in ‘EXTRACT’, we have the selected records which are being displayed currently. The field <VIM_XTOTAL_KEY> contains the key.
For changing the current record, we need to modify both the corresponding EXTRACT and TOTAL table values.
The <ACTION> field indicates the current user action for that field – INSERT, UPDATE, DELETE.
<em>*& Code for automatic population of values ***
module MODIFY_RECORD input.
DATA: l_index TYPE sytabix, " Index to note the lines found
l_tabix TYPE sytabix. " Index of total table
DATA: l_matnr TYPE matnr.
FIELD-SYMBOLS: <record> TYPE ANY. " Work area for table
* Modify changed by and changed on fields for any updates
LOOP AT total.
l_tabix = sy-tabix.
* Assign the table header line to the work area
ASSIGN total TO <record>.
IF sy-subrc = 0. " If assigning is successful.
* Check if data was changed and get the corresponding line index
IF <action> = 'U' " Updated entry
OR <action> = 'N'. " New entry
READ TABLE extract WITH KEY <vim_xtotal_key>. " Key field
IF sy-subrc = 0.
* Clears the variables after updation.
l_index = sy-tabix.
SELECT SINGLE maktx
WHERE matnr = <record>+3(18).
* Modify total table
MODIFY total FROM <record> INDEX l_tabix.
CHECK l_index > 0.
extract = <record>.
* Modify extract table
MODIFY extract INDEX l_index.
endmodule. " MODIFY_RECORD INPUT</em>
We have also made the material text field display only. Now in SM30, when we try to maintain new material number. in the table, the corresponding text gets pulled in automatically. Add new entries to the table.
If you want to get more practical issues and resolutions straight to your inbox, please SUBSCRIBE. We respect your privacy and take protecting it seriously.
Thank you very much for your time!!
If you liked it, please share it.