Skip to content

Commit 2e94d89

Browse files
committed
Fix g_form workspace console helper for Utah/HR Agent
Issue #647
1 parent e33c87f commit 2e94d89

2 files changed

Lines changed: 64 additions & 18 deletions

File tree

  • Client-Side Components/Client Scripts/g_form console access in workspace

Client-Side Components/Client Scripts/g_form console access in workspace/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
When developing forms in ServiceNow it can be useful to try stuff out directly in the DevTools Console.
33
In UI16 this was pretty straightforward because g_form was available globally, Agent Workspace makes this a little bit more complicated.
44
So this script provides access to the g_form object of the currently active tab in a Workspace.
5+
It supports both older Agent Workspace DOM structures and Utah/configurable workspace structures.
56

67
Just copy the Script in the DevTools Console and run `var g_form = getGlideFormAW()`
7-
now you should be able to do stuff like `g_form.setValue("short_description", "Lorem ipsum")`
8+
If `g_form` is returned, you should be able to do stuff like `g_form.setValue("short_description", "Lorem ipsum")`.
9+
If `g_form` is `null`, open a record form tab first and run the function again.
Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,67 @@
11
function getGlideFormAW() {
2-
document.getElementsByTagName("sn-workspace-content")[0].shadowRoot.querySelectorAll("now-record-form-connected")[0]
2+
function collectDeepElements(selector) {
3+
var results = [];
4+
var queue = [document];
5+
var visited = [];
36

4-
var firstContentChild = document.getElementsByTagName("sn-workspace-content")[0].shadowRoot
5-
.querySelectorAll(".chrome-tab-panel.is-active")[0].firstChild;
7+
while (queue.length > 0) {
8+
var node = queue.shift();
9+
if (!node || visited.indexOf(node) >= 0) {
10+
continue;
11+
}
12+
visited.push(node);
613

7-
var snWorkspaceFormEl;
8-
if (firstContentChild.tagName == "NOW-RECORD-FORM-CONNECTED") {
9-
snWorkspaceFormEl = firstContentChild.shadowRoot.querySelectorAll(".sn-workspace-form")[0];
10-
} else {
11-
snWorkspaceFormEl = firstContentChild.shadowRoot.querySelectorAll("now-record-form-connected")[0]
12-
.shadowRoot.querySelectorAll(".sn-workspace-form")[0];
14+
if (node.querySelectorAll) {
15+
var matches = node.querySelectorAll(selector);
16+
for (var i = 0; i < matches.length; i++) {
17+
results.push(matches[i]);
18+
}
19+
}
20+
21+
var descendants = [];
22+
if (node.querySelectorAll) {
23+
descendants = node.querySelectorAll("*");
24+
} else if (node.children) {
25+
descendants = node.children;
26+
}
27+
for (var j = 0; j < descendants.length; j++) {
28+
if (descendants[j] && descendants[j].shadowRoot) {
29+
queue.push(descendants[j].shadowRoot);
30+
}
31+
}
32+
}
33+
34+
return results;
35+
}
36+
37+
function fromSnFormDataConnected() {
38+
var connected = collectDeepElements("sn-form-data-connected");
39+
for (var i = 0; i < connected.length; i++) {
40+
var gForm = connected[i] && connected[i].nowRecordFormBlob && connected[i].nowRecordFormBlob.gForm;
41+
if (gForm) {
42+
return gForm;
43+
}
44+
}
45+
return null;
1346
}
14-
if (!snWorkspaceFormEl) throw "Couldn't find sn-workspace-form";
1547

16-
var reactInternalInstanceKey = Object.keys(snWorkspaceFormEl).find(function (objKey) {
17-
if (objKey.indexOf("__reactInternalInstance$") >= 0) {
18-
return true;
48+
function fromSnWorkspaceForm() {
49+
var workspaceForms = collectDeepElements(".sn-workspace-form");
50+
for (var i = 0; i < workspaceForms.length; i++) {
51+
var snWorkspaceFormEl = workspaceForms[i];
52+
var reactInternalInstanceKey = Object.keys(snWorkspaceFormEl).find(function (objKey) {
53+
return objKey.indexOf("__reactInternalInstance$") >= 0;
54+
});
55+
var reactNode = reactInternalInstanceKey && snWorkspaceFormEl[reactInternalInstanceKey];
56+
if (reactNode && reactNode.return && reactNode.return.stateNode
57+
&& reactNode.return.stateNode.props
58+
&& reactNode.return.stateNode.props.glideEnvironment
59+
&& reactNode.return.stateNode.props.glideEnvironment._gForm) {
60+
return reactNode.return.stateNode.props.glideEnvironment._gForm;
61+
}
1962
}
20-
return false;
21-
});
22-
return snWorkspaceFormEl[reactInternalInstanceKey].return.stateNode.props.glideEnvironment._gForm;
23-
}
63+
return null;
64+
}
65+
66+
return fromSnFormDataConnected() || fromSnWorkspaceForm();
67+
}

0 commit comments

Comments
 (0)