Tuesday, October 14, 2014

Automatic Population of Values during Table Maintenance

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 some custom requirement like validating a particular entry before it is saved. There can be various other specific requirements which we may face when we are maintaining data a custom table

Now for example say we have a custom table ZTEST_MAT.

Now we can all create the table maintenance generator for this table using menu Path Utilities --> table maintenance generator.

After the TMG is successfully created, then open it again in display mode and get the source code name for the TMG. It will be a module pool program.



Now open the source code SAPLZTEST_MAT. And open the screen flow logic.

Put your custom module in the PAI Module. Here MODULE MODIFY_RECORD is the module in which we have put our code.

Now 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 – new insertion or updation or deletion.

*& 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
FROM makt
INTO <record>+21(40)
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.
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.

endmodule. " MODIFY_RECORD INPUT

We have also made the material text field display only. Now in SM30, when we try to maintain new material nos. in the table, the corresponding text gets pulled in automatically. Add new entries to the table.



Now when we press Enter.



The material text is populated automatically. This is a simple example of how we can add useful features to the standard table maintenance.
Post a Comment