Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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")`
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.
Original file line number Diff line number Diff line change
@@ -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;
}
return null;
}

return fromSnFormDataConnected() || fromSnWorkspaceForm();
}
Loading