A common ABAP puzzle: you call CL_GUI_FRONTEND_SERVICES=>GET_DESKTOP_DIRECTORY, pass a CHANGING string, and in the debugger lv_str is still blank. Then you step over cl_gui_cfw=>flush and the path suddenly appears. It is not a debugger glitch — it is how the GUI automation queue works.
Why the CHANGING Parameter Is Empty Until FLUSH
Methods of CL_GUI_FRONTEND_SERVICES run on the presentation server, not inside the AS ABAP work process. ABAP collects these calls in the automation queue and sends them to the client in a bundle at the next synchronization point. The CHANGING parameter is written back only when the queued call actually executes on the client. Until then, lv_str simply holds the initial value you passed in.
The official GET_DESKTOP_DIRECTORY documentation is explicit: “Call CL_GUI_CFW=>FLUSH in the ABAP program after calling this method to actually execute the method on the client.” The same page notes that the parameter should have been defined as EXPORTING — whatever you put into it is ignored.
A Clean ABAP Snippet
DATA: lv_desktop TYPE string,
lv_subrc TYPE sy-subrc.
CALL METHOD cl_gui_frontend_services=>get_desktop_directory
CHANGING
desktop_directory = lv_desktop
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.
lv_subrc = sy-subrc. "save the result of the getter BEFORE the flush
CALL METHOD cl_gui_cfw=>flush
EXCEPTIONS
cntl_system_error = 1
cntl_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE 'Flush failed' TYPE 'E'.
ELSEIF lv_subrc <> 0.
MESSAGE 'Cannot determine desktop directory' TYPE 'E'.
ENDIF.
WRITE lv_desktop.
Two details in the original code can be simplified: the reference variable lr_d is never instantiated and is not needed at all, because GET_DESKTOP_DIRECTORY is a static method; and BREAK-POINT belongs in debugging only, never in transportable code.
The Hidden Trap: sy-subrc After FLUSH
The original snippet checks IF sy-subrc <> 0 twice, with the second check placed after the flush. That second check no longer sees the result of GET_DESKTOP_DIRECTORY: sy-subrc has been overwritten by the flush call. If the directory lookup failed but the flush itself succeeded, the program silently continues with an empty path.
Save the first sy-subrc into a local variable immediately after the getter call (lv_subrc in the snippet above) and test that variable when you handle the frontend-service error. Keep the later sy-subrc check strictly for the flush.
Why It Works in Some Programs but Not Others
The automation queue is also flushed implicitly at synchronization points — typically when a screen is sent to the GUI (PBO/PAI) or when a control method requires a synchronous answer. In a classic dialog program the value may already be filled when you read it later during PAI, so the bug stays hidden. In a report, an initialization routine, or a helper method called before any round trip, nothing triggers the implicit flush before you read the variable.
Rule of thumb: if you need the returned value on the very next line, flush on that line. Do not depend on a later screen change to do it for you.
Limits and Better Targets
GET_DESKTOP_DIRECTORY returns the folder containing desktop objects under SAP GUI for Windows; the Web GUI returns the current directory. The documentation also warns against using this function to choose a file destination: files should not be written to the client desktop, and the returned path is not guaranteed to be a desktop directory. Prefer GET_TEMP_DIRECTORY or GET_SAPGUI_WORKDIR for client-side files.
When the directory cannot be determined, CNTL_ERROR is raised; NOT_SUPPORTED_BY_GUI occurs for unsupported GUI types; ERROR_NO_GUI is currently unused. In background jobs there is no presentation server at all, so frontend service methods cannot work — use an application-server directory or a customizing setting instead.
Quick Checklist
- Call
cl_gui_cfw=>flushafter frontend-service getter methods that do not flush internally. - Store
sy-subrcimmediately after the getter; a later call overwrites it. - Do not use the desktop path as a file target; use the temp directory or SAP GUI work directory.
- Never rely on frontend methods in batch/background processing.
- Remove
BREAK-POINTand unused variable declarations before release.
