How to create a redirect rule that is only for form submit and update and not the other UI button?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2025 11:15 PM
Dear experts,
I would like to set a redirect rule for my users when they submit or update the form when creating risk event form based on their role, and I realize that the global submit and update button will also affect the analyze, and request for approval button and so on the primary buttons. Currently this is my script, but I think it is not so efficient as I list down all the primary actions one by one, any suggestions or recommendations can be change to my script.
function onSubmit() {
var userID = g_user.userID;
var redirectURL = "/sn_risk_advanced_event_list.do?sysparm_fixed_query=opened_by=" + userID;
// Get sys_id and action name
var sysID = g_form.getUniqueValue();
var actionName = g_form.getActionName(); // Get the clicked button
console.log("User ID:", userID);
console.log("Sys ID:", sysID);
console.log("Action Name:", actionName);
console.log("Redirect URL:", redirectURL);
// List of actions that should NOT trigger a redirect
var excludedActions = [
"analyze",
"request_approval",
"closed",
"reanalyze",
"approve",
"reopen_risk_event",
"reject_risk_event",
"event_request_more_info",
"sysverb_update" // Prevents redirection when "Update" is clicked
];
// **Check if file is being attached**
if (window.location.href.includes("sys_attachment.do")) {
console.log("File attachment detected. No redirection.");
return true; // Stop redirection
}
// Prevent redirection if the action is in the excluded list
if (excludedActions.includes(actionName)) {
console.log("Action '" + actionName + "' clicked. No redirection.");
return true; // Allow normal behavior
}
if (sysID === "-1") {
sessionStorage.setItem("redirect_after_creation", redirectURL);
} else {
setTimeout(function() {
g_navigation.open(redirectURL); // Redirect only for new submissions
}, 500);
}
}
// Handle redirection after form submission for new records
(function() {
var redirectURL = sessionStorage.getItem("redirect_after_creation");
if (redirectURL) {
sessionStorage.removeItem("redirect_after_creation"); // Clear stored redirect
g_navigation.open(redirectURL);
}
})();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2025 11:26 PM
try this with some minor changes
function onSubmit() {
var userID = g_user.userID;
var redirectURL = "/sn_risk_advanced_event_list.do?sysparm_fixed_query=opened_by=" + userID;
// Get sys_id and action name
var sysID = g_form.getUniqueValue();
var actionName = g_form.getActionName(); // Get the clicked button
console.log("User ID:", userID);
console.log("Sys ID:", sysID);
console.log("Action Name:", actionName);
console.log("Redirect URL:", redirectURL);
// Use a dynamic check for excluded actions
var excludedPatterns = /^(analyze|request_approval|closed|reanalyze|approve|reopen_risk_event|reject_risk_event|event_request_more_info|sysverb_update)$/;
// Check if file is being attached
if (window.location.href.includes("sys_attachment.do")) {
console.log("File attachment detected. No redirection.");
return true; // Stop redirection
}
// Prevent redirection if the action matches excluded patterns
if (excludedPatterns.test(actionName)) {
console.log("Action '" + actionName + "' clicked. No redirection.");
return true; // Allow normal behavior
}
if (sysID === "-1") {
sessionStorage.setItem("redirect_after_creation", redirectURL);
} else {
setTimeout(function() {
g_navigation.open(redirectURL); // Redirect only for new submissions
}, 500);
}
}
// Handle redirection after form submission for new records
(function() {
var redirectURL = sessionStorage.getItem("redirect_after_creation");
if (redirectURL) {
sessionStorage.removeItem("redirect_after_creation"); // Clear stored redirect
g_navigation.open(redirectURL);
}
})();
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2025 11:43 PM
So the only way to approach this is to exclude all the actions rather than target the specific action that we want right?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2025 12:46 AM
that's correct.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2025 01:35 AM
Hello @ChuanYanF
You can just go to specific "UI Action" and in the server part of the script
var redirectURL = sessionStorage.getItem("redirect_after_creation");
if (redirectURL) {
sessionStorage.removeItem("redirect_after_creation"); // Clear stored redirect
g_navigation.open(redirectURL);
}
Just add this.
Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket.
Regards,
Shivalika
My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194
My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY