Language:English

WinCC Unified scripting · Problem and fix

Screen.FindItem returns undefined? Fix the path

The script runs in

It wants to reach

Screen windows and objects

(root) · Screen_Main

Lamp_Header

Screen window_1 · Screen_Pump

Lamp_1

Screen window_2 · Screen_Valve

Valve_1

Detail · Screen_Detail

Lamp_Run

Screen.FindItem (relative)

Screen.FindItem('../Lamp_Header')

UI.FindItem (absolute)

UI.FindItem('//Lamp_Header')
  • Screen names never appear in the path: only screen windows and objects.
Choose where the script runs and the object it wants. The finder writes the relative and absolute paths, and shows what changes when a main screen window is defined.

The lamp should light up. Instead, the Trace Viewer says “Cannot set properties of undefined”. FindItem found nothing, and nine times out of ten the path is the reason. Pick where your script runs and what it wants to reach: the path writes itself.

  • The four prefixes (. .. ~ /) explained with one drawing
  • Six causes of “undefined”, from screen names to pop-ups that are not loaded yet
  • A FindItem that tells you which part of the path is wrong
Start reading

HMIx team6 min readUpdated

What you see

Nothing changes, and a TypeError points at the wrong line

FindItem does not fail when it finds nothing: it returns nothing. The error comes one line later, when you try to set a property on that nothing:

Avoid· Two classic mistakes in two lines

UI.SysFct.OpenScreenInPopup('Detail', 'Screen_PumpDetail', false, 'Pump', 100, 80, false);// 1) Too early: the pop-up's screen has not loaded yet.// 2) A screen name in the path: paths never name screens.Screen.FindItem('/Detail/Screen_PumpDetail/Lamp_Run').Visible = true;// TypeError: Cannot set properties of undefined (setting 'Visible')

Depending on the Runtime version, the message reads Cannot set properties of undefined (setting 'Visible') or Cannot set property 'Visible' of undefined. Either way it is the line after the real problem.

How object paths work

Screen windows and objects, joined by slashes. Never screens.

An object path is made of the names of screen windows and screen objects, joined with / in their nesting order. Siemens is explicit: “Screens (Screens) and their names are not used in the formulation.” Siemens Tips §5.4.1 The beginning of the path says where you start from:

Starts withMeansExample
(nothing)An object of the screen of the scriptLamp_1
./Down from the screen window of the script./Screen window_1/Lamp_1
../Up one screen window (can be repeated at the start)../../Lamp_2
~/From the top of the script's own window tree~/Screen window_1/Lamp_1
/A top-level window by its name/Main screen window/Lamp_1

Screen.FindItem() searches from the screen of the script and accepts relative paths. UI.FindItem() searches globally and needs an absolute one. And when the object is on the same screen as the script, skip paths altogether: Siemens recommends Screen.Items('Name'), which is also faster Siemens EG.

Example· The same idea, five ways

Screen.Items('Lamp_1');                        // same screen: fastest, no pathScreen.FindItem('./Screen window_1/Lamp_1');   // child screen windowScreen.FindItem('../Lamp_2');                  // from inside a screen window, one level upUI.FindItem('~/Screen window_1/Lamp_1');       // absolute, from the top of this window treeUI.FindItem('/Main screen window/Screen window_1/Lamp_1'); // with a main screen window

Six reasons FindItem finds nothing

Check them in this order

  1. 01A screen name in the path. ./Screen window_1/Screen_Pump/Lamp_1 will never work: remove Screen_Pump. The window shows a screen; the path names only the window.
  2. 02The wrong starting point. A relative path is relative to the screen window the script runs in. The same line works on a button in the base screen and fails on a button inside a screen window.
  3. 03// after a main screen window. When a main screen window is defined in the screen management, it becomes the named top level: “References with double slashes “//” no longer function and must be adapted.” Use /Main screen window/… or ~/… instead Siemens readme V18 Update 4.
  4. 04Inside a pop-up, the pop-up is the top. Siemens: within a pop-up window, the pop-up itself is the top-level screen window, addressed with // or its own name. ~/ from a pop-up does not reach the screen behind it.
  5. 05Too early. OpenScreenInPopup returns at once, before the pop-up's screen has loaded its objects. The window can be found right away; the objects inside it, often not yet.
  6. 06Inside a faceplate. From the screen, a faceplate is a container you talk to through its interface (.Properties). Its inner objects are reached from the faceplate's own scripts with Faceplate.Items().
Fig. 1

Why “too early” returns undefined

OpenScreenInPopup()pop-up window existsits screen loads…FindItem here → undefinedLoaded: objects existCode here → found
Opening a pop-up starts loading its screen; the script carries on. Simplified, illustrative timing.

The fix: find the object where it really exists

Run the code in the right screen, and make the path explain itself

For pop-ups, move the work into the pop-up's own screen: its Loaded event runs when its objects exist, and there the object is on the same screen, so Screen.Items() is enough.

Do this· In the pop-up's screen, event Loaded

// Screen "Screen_PumpDetail" > Events > Loaded.// This code runs INSIDE the pop-up, when its objects really exist.const running = Tags('Pump1_Running').Read();Screen.Items('Lamp_Run').Visible = Boolean(running);

For faceplates, set what the faceplate exposes from outside, and touch its inner objects only from inside:

Do this· Faceplate from outside and from inside

// From the screen: talk to the faceplate through its interfaceScreen.Items('Pump1_Faceplate').Properties.Indicator = 0xFF00C000;// Inside the faceplate type (its own scripts): its own objectsFaceplate.Items('Lamp_Run').Visible = true;

And for everything else, a small helper saves hours of trial and error: it behaves like FindItem, but when nothing is found it writes to the trace which part of the path does not exist. Two more tricks: a screen window's .Path property gives you its real path, and the Copy property name command in TIA avoids typos in the property itself.

Ready-to-use script

findOrExplain: FindItem that tells you where the path breaks

A global module with one function. Use it like FindItem; when nothing is found it walks the path step by step and writes to the trace the first name that does not exist, with the three rules that usually explain why.

Where it goes
Scripts > Global modules > Paths
Runs on
WinCC Unified V16 or later

Do this· hmix-find-or-explain.js

// Leading part of a path that is not a name: ../../  ./  ~/  //  /const PATH_START = /^((?:\.\.\/)+|\.\/|~\/|\/\/|\/)?/;​function tryFind(scope, path) {  try {    return scope.FindItem(path) || null;  } catch (error) {    return null;  }}​export function findOrExplain(scope, path) {  const item = tryFind(scope, path);  if (item) {    return item;  }

Three ways HMIx saves you the trial and error

  1. 01

    Describe what the script must do. HMIx asks for your tags, writes it, explains it part by part and simulates it.

  2. 02

    Paste a script you already have. HMIx explains it and flags, for free, the classic errors.

  3. 03

    Something fails? HMIx walks you through the checks, from the trace message to the fix.

Frequently asked questions

Why does Screen.FindItem return undefined in WinCC Unified?

Because the path does not lead to an object from where the script runs: a screen name in the path, a relative path from another screen window, // after a main screen window was defined, a pop-up treated as part of the main tree, or an object of a pop-up that has not loaded yet.

What is the difference between Screen.FindItem and UI.FindItem?

Screen.FindItem searches from the screen of the script and accepts relative paths (./, ../). UI.FindItem has a global search context and requires an absolute path (~/ or /). For an object on the same screen, Siemens recommends Screen.Items('Name').

Do I put the screen name in the FindItem path?

No. Paths are built from the names of screen windows and screen objects only; Siemens states that screens and their names are not used in object paths.

How do I change an object inside a pop-up from a script?

Do it from the Loaded event of the pop-up's own screen, where the object exists and Screen.Items('Name') reaches it. From outside, right after OpenScreenInPopup, the pop-up's objects are often not loaded yet.

My paths with // stopped working after an upgrade. Why?

When a main screen window is defined in the screen management (introduced with V18 Update 4), it becomes the named top-level window and references with // no longer work. Use the window's name, /Main screen window/…, or ~/…

Ready to use, from the HMIx catalogue

Free to download: this article’s script, shared from HMIx, and controls for the screen around it.

Browse the free catalogue →

Sources

Every Siemens statement in this article was read in the document linked here. Versions and behaviour can change with updates: check the manual of your own version.

  1. Siemens: SIMATIC WinCC Unified Tips and Tricks for Scripting (JavaScript), entry 109758536, V5.0 (09/2025)
  2. Siemens: Engineering guideline for WinCC Unified, entry 109827603, V4.0 (11/2025)
  3. Siemens: Readme WinCC Unified Runtime V18 Update 4 (German)
  4. Siemens: Screen.FindItem() (WinCC Unified V21.1 object model)
  5. Siemens: UI.FindItem() (WinCC Unified V21.1 object model)
  6. Siemens: Opening and closing a screen in a pop-up window (WinCC Unified V21.1)
  7. Siemens: SysFct.OpenScreenInPopup() (WinCC Unified V21.1 object model)
  • WinCC Unified FindItem
  • Screen.FindItem
  • UI.FindItem
  • screen window path
  • Cannot set properties of undefined
  • pop-up
  • faceplate
  • TIA Portal