There may be situations when the SAPGUI system under test generates an intermittent or unexpected window which results in the script reporting errors.

There are two approaches that can be used.

If you know the specific point in the transaction where the window may be generated you can use SapGuiVerifyWindowAvailability to determine if the window exists. Ensure you set the nSeverity parameter to SEVERITY_INFORMATIONAL otherwise the SapGuiVerifyWindowAvailability function will throw an error if the intermittent window does not exist.
 
// Here is our first screen - we enter some value and hit enter
SapGuiSetActiveWindow("wnd[0]", "First screen");
SapGuiSetText("usr/txt1", "Some value");
SapGuiSendVKey(SAPGUI_VKEY_ENTER);
 
// Check for existence of the window that may be generated
if(SapGuiVerifyWindowAvailability("wnd[1]", "Some Popup Window", false, SEVERITY_INFORMATIONAL)) then
  //Handle the window if found 
  SapGuiSetActiveWindow("wnd[1]", "Some Popup Window");
  SappGuiPressButton("usr/btnPOP-OPTION1");
end;

// we go on with our next screen that we expect in both cases
SapGuiSetActiveWindow("wnd[0]", "Next screen");

If there is a situation where different dialogs can appear and you have to handle them individually – you can check for the new window by ID only by setting the sWindowTitle parameter to null:

// Check for existence of the window that may be generated:
if(SapGuiVerifyWindowAvailability("wnd[1]", null, false, SEVERITY_INFORMATIONAL)) then
  SapGuiSetActiveWindow("wnd[1]");

  // now - check what the window title is and depending on that do some action
  sWindowTitle := SapGuiGetActiveWindowTitle();

  if(sWindowTitle = "Some Alert") then
    SapGuiPressButton("usr/btnPOP-OPTION1");
  end;

  if(sWindowTitle = "Some other alert") then
    SapGuiPressButton("usr/btnPOP-OPTION2");
  end;
end;


An event handler approach can be used when you are not sure when the unexpected window will appear so you rely on an error being raised. The event handler catches the error raised and you have an oppertunity to check whether the unexpected window exists and deal with it.

dclevent
handler Handler1
begin
  if(SapGuiVerifyWindowAvailability("wnd[1]", "Some Popup Window", false, SEVERITY_INFORMATIONAL)) then
    SapGuiSetActiveWindow("wnd[1]", "Some Popup Window");
    SapGuiPressButton("usr/btnPOP-OPTION1");
  end;
  else
    throw;
  end;
end Handler1;