Treat a lookup as a result that can fail

Looking up an ID is not the same as guaranteeing that the row exists. A dispatch can be missing, deleted, or outside the current work set. READ TABLE makes the lookup explicit and sy-subrc tells you whether a row was found.

Check before using the target

READ TABLE dispatches
  WITH TABLE KEY dispatch_id = 'D-1102'
  INTO DATA(found).

IF sy-subrc = 0.
  DATA(result) = found-status.
ENDIF.

The target is meaningful only after a successful read. Keep the sy-subrc check adjacent to the statement so later changes do not accidentally use a failed lookup’s initial target.

Use the table’s declared key when it matches the lookup. A free-key search is possible, but the cost and behavior depend on the table category and key. For a lookup that needs a distinct fallback, consider a table expression with OPTIONAL or DEFAULT instead.

The sample is a method-body fragment; READ TABLE does not access a database. ABAP Cloud and classic systems share these core concepts, though language-version and release availability should be checked for advanced additions.

Sources

Developer practice · ABAP

Practice: check whether a dispatch was found

Read dispatch D-1102 from a uniquely keyed table and set result to the status only when the read succeeds.

Choose your ABAP environment

This choice changes the output pattern checked here: WRITE belongs in a classic executable report, while out->write( ) uses the supplied output reference in an ABAP Cloud class-run method. It does not switch the language version of the other code; check release and API availability in your target system.

Complete the task and include the output statement shown for your selected environment. Variable names can differ if you use them consistently. This browser checker checks structure only; it does not compile or run ABAP.

0 required structures detected · 0 checks

Debug this code

Assume dispatches is a keyed internal table. A lookup can fail; repair this snippet so found-status is read only after a successful read.

Edit the code, then check for the expected repair. This is a structure check, not an ABAP compiler.