When working with SAP ALV grids in ABAP, developers frequently encounter the need to track modifications made by end-users during data display. The cl_gui_alv_grid class provides a robust mechanism for handling these interactions, and understanding how to retrieve changed rows is crucial for implementing efficient data synchronization logic. This functionality becomes essential in scenarios involving mass updates, approval workflows, or any application where user edits must be selectively processed on the backend.

Understanding the Data Exchange Mechanism

The core of tracking modifications lies in the data exchange between the front-end ALV and the ABAP backend. The grid control maintains an internal table that reflects the current state displayed to the user, which may differ from the original data fetched initially. To synchronize these states, SAP employs a system of packing modified data into a specific structure during the execution of standard SAP events, most notably the on_modify event. This structure acts as a container, capturing not just the altered field values but also their precise location within the dataset, enabling precise change identification.
Implementing the Event Handler

To capture modifications, you must first establish a connection between the grid and your event handling logic. This involves setting the i_parent reference and registering the method that will process the on_modify event. The registration is typically performed directly after creating the ALV instance using the set_ready_for_input method with a function code that triggers the frontend service. Without this registration, the system will not generate the necessary data packet detailing the user's actions, rendering change tracking impossible.
The Structure of Modified Data

The event on_modify passes a table of type LVC_T_MODI to the implementing method. This table is the key to identifying changed rows, as each entry corresponds to a specific cell alteration. Each structure in this table contains critical fields: ROW_ID and FIELDNAME. The ROW_ID is particularly important as it represents the logical key of the modified line, usually correlating with the initial table index or a specific key field defined in the layout. By analyzing this table, you can determine exactly which rows require processing on the database level.
| Field Name | Data Type | Description |
|---|---|---|
| ROW_ID | CHAR 10 | Identifies the modified line in the internal table. |
| FIELDNAME | CHAR 30 | Holds the name of the field that was modified. |
| VALUE_NEW | CHAR 255 | Contains the new value entered by the user. |
Extracting the Changed Rows

While the LVC_T_MODI table provides granular cell-level data, the ultimate goal is often to isolate the complete rows that have been altered. To achieve this, you iterate through the modification table and collect the unique ROW_ID values into a separate index table or standard table of row identifiers. Since a single row may contain multiple changed fields, it is vital to use a check to ensure that each row is only added to the change list once. This collection phase transforms low-level cell modifications into actionable business object updates.
Synchronizing with the Database
Once the list of changed row identifiers is compiled, the next phase involves updating the persistent database tables. This step requires a loop through the main internal table containing the current display data, cross-referenced with your list of changed row IDs. For performance optimization, it is generally recommended to prepare an update table and execute a single `MODIFY` or `UPDATE` statement for the entire batch rather than issuing individual database commits for each row. This approach minimizes database overhead and ensures transactional integrity during the save process.

Handling Optimistic Locking
In multi-user environments, concurrent data access can lead to update conflicts that must be managed gracefully. When processing the changed rows identified by `ROW_ID`, the system should implement optimistic locking checks using the `SYSTEM-COMMAND` field or the `COL_POS` system field within the modification structure. If a conflict is detected—meaning the underlying database record has been altered by another user since the ALV was displayed—the application should notify the specific row to the user rather than performing a blind overwrite. This ensures data consistency and prevents accidental loss of recent modifications.



















