From 2e94d89b1bc7af5742fae2b3fd0f59220017c38d Mon Sep 17 00:00:00 2001 From: ishitadatta Date: Sat, 7 Mar 2026 17:37:05 -0500 Subject: [PATCH] Fix g_form workspace console helper for Utah/HR Agent Issue #647 --- .../README.md | 4 +- .../script.js | 78 +++++++++++++++---- 2 files changed, 64 insertions(+), 18 deletions(-) diff --git a/Client-Side Components/Client Scripts/g_form console access in workspace/README.md b/Client-Side Components/Client Scripts/g_form console access in workspace/README.md index 16992cdda3..d6393e951d 100644 --- a/Client-Side Components/Client Scripts/g_form console access in workspace/README.md +++ b/Client-Side Components/Client Scripts/g_form console access in workspace/README.md @@ -2,6 +2,8 @@ When developing forms in ServiceNow it can be useful to try stuff out directly in the DevTools Console. In UI16 this was pretty straightforward because g_form was available globally, Agent Workspace makes this a little bit more complicated. So this script provides access to the g_form object of the currently active tab in a Workspace. +It supports both older Agent Workspace DOM structures and Utah/configurable workspace structures. Just copy the Script in the DevTools Console and run `var g_form = getGlideFormAW()` -now you should be able to do stuff like `g_form.setValue("short_description", "Lorem ipsum")` \ No newline at end of file +If `g_form` is returned, you should be able to do stuff like `g_form.setValue("short_description", "Lorem ipsum")`. +If `g_form` is `null`, open a record form tab first and run the function again. diff --git a/Client-Side Components/Client Scripts/g_form console access in workspace/script.js b/Client-Side Components/Client Scripts/g_form console access in workspace/script.js index b80069ea6f..41257bc37a 100644 --- a/Client-Side Components/Client Scripts/g_form console access in workspace/script.js +++ b/Client-Side Components/Client Scripts/g_form console access in workspace/script.js @@ -1,23 +1,67 @@ function getGlideFormAW() { - document.getElementsByTagName("sn-workspace-content")[0].shadowRoot.querySelectorAll("now-record-form-connected")[0] + function collectDeepElements(selector) { + var results = []; + var queue = [document]; + var visited = []; - var firstContentChild = document.getElementsByTagName("sn-workspace-content")[0].shadowRoot - .querySelectorAll(".chrome-tab-panel.is-active")[0].firstChild; + while (queue.length > 0) { + var node = queue.shift(); + if (!node || visited.indexOf(node) >= 0) { + continue; + } + visited.push(node); - var snWorkspaceFormEl; - if (firstContentChild.tagName == "NOW-RECORD-FORM-CONNECTED") { - snWorkspaceFormEl = firstContentChild.shadowRoot.querySelectorAll(".sn-workspace-form")[0]; - } else { - snWorkspaceFormEl = firstContentChild.shadowRoot.querySelectorAll("now-record-form-connected")[0] - .shadowRoot.querySelectorAll(".sn-workspace-form")[0]; + if (node.querySelectorAll) { + var matches = node.querySelectorAll(selector); + for (var i = 0; i < matches.length; i++) { + results.push(matches[i]); + } + } + + var descendants = []; + if (node.querySelectorAll) { + descendants = node.querySelectorAll("*"); + } else if (node.children) { + descendants = node.children; + } + for (var j = 0; j < descendants.length; j++) { + if (descendants[j] && descendants[j].shadowRoot) { + queue.push(descendants[j].shadowRoot); + } + } + } + + return results; + } + + function fromSnFormDataConnected() { + var connected = collectDeepElements("sn-form-data-connected"); + for (var i = 0; i < connected.length; i++) { + var gForm = connected[i] && connected[i].nowRecordFormBlob && connected[i].nowRecordFormBlob.gForm; + if (gForm) { + return gForm; + } + } + return null; } - if (!snWorkspaceFormEl) throw "Couldn't find sn-workspace-form"; - var reactInternalInstanceKey = Object.keys(snWorkspaceFormEl).find(function (objKey) { - if (objKey.indexOf("__reactInternalInstance$") >= 0) { - return true; + function fromSnWorkspaceForm() { + var workspaceForms = collectDeepElements(".sn-workspace-form"); + for (var i = 0; i < workspaceForms.length; i++) { + var snWorkspaceFormEl = workspaceForms[i]; + var reactInternalInstanceKey = Object.keys(snWorkspaceFormEl).find(function (objKey) { + return objKey.indexOf("__reactInternalInstance$") >= 0; + }); + var reactNode = reactInternalInstanceKey && snWorkspaceFormEl[reactInternalInstanceKey]; + if (reactNode && reactNode.return && reactNode.return.stateNode + && reactNode.return.stateNode.props + && reactNode.return.stateNode.props.glideEnvironment + && reactNode.return.stateNode.props.glideEnvironment._gForm) { + return reactNode.return.stateNode.props.glideEnvironment._gForm; + } } - return false; - }); - return snWorkspaceFormEl[reactInternalInstanceKey].return.stateNode.props.glideEnvironment._gForm; -} \ No newline at end of file + return null; + } + + return fromSnFormDataConnected() || fromSnWorkspaceForm(); +}