How to capture page URL using a UI Action/ declarative action?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2025 06:43 PM
I am on the record page within the workspace and have added a button. When the button is clicked, I want to capture the current page URL.
Specifically, I need to extract the sysID
parameter from the URL:
URL Format:<Instance Id>/now/mfg/isa-equipment-model/params/sys-id/<sysID>/selected-tab-index/4/sub/record/<table-name>/<record-sysId>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2025 07:30 PM
Try using the below function-
function getSysId() {
var sysId = NOW.url.getParsedQuery().sys_id;
if (sysId) {
console.log("sysID:", sysId);
alert("sysID: " + sysId);
return sysId;
} else {
console.error("sysID not found");
alert("sysID not found");
return null;
}
}
How This Works
- NOW.url.getParsedQuery() extracts URL parameters in ServiceNow Workspace.
- It directly retrieves the sys_id without needing to manually parse the URL.
- Much cleaner and OOB-supported compared to using window.location.href.
How to Use This in Your Button
1. If you are adding a UI Action button in a ServiceNow Workspace, set the onClick function to:
getSysId();
2. If you are using a Client Script, you can trigger this function when needed.
let me know if this helps
✔️ If this solves your issue, please mark it as Correct.
✔️ If you found it helpful, please mark it as Helpful.
—
Shubham Jain