How do I create a redirect rule that works after submitting a record and bring me to a specific view

ChuanYanF
Tera Guru

Dear experts,

 

I am facing a script problem where I tried to create a client script on submit for a redirect rule to a specific view which is opened by dynamic (me) but it keeps on failing. Can anyone here check my script and if there is anything I can change or another approach to work with? 

function onSubmit() {
var userID = g_user.userID; // Get current user ID
var redirectURL = "/sn_risk_advanced_event_list.do?sysparm_query=opened_by=" + encodeURIComponent(userID);

var sysID = g_form.getUniqueValue(); // Get record sys_id

console.log("Redirect URL: " + redirectURL); // Debugging

// Only set redirection if it's a new record
if (!sysID || sysID === "-1") {
sessionStorage.setItem("redirect_after_submission", redirectURL);
}

return true; // Allow normal form submission
}

// Redirect after submission **in a new tab** to avoid ServiceNow interference
window.onload = function () {
var redirectURL = sessionStorage.getItem("redirect_after_submission");

if (redirectURL) {
sessionStorage.removeItem("redirect_after_submission"); // Clear stored URL

console.log("Redirecting to: " + redirectURL); // Debugging

// Open the correct page in a new tab
setTimeout(function () {
window.open(redirectURL, "_self"); // Use "_self" to replace the current page
}, 500);
}
};

1 REPLY 1

KeerthiP
Mega Guru

The condition if (!sysID || sysID === "-1") might not be accurate for detecting new records. You can try using g_form.isNewRecord() instead and

Instead of window.open(redirectURL, "_self"), you can use window.location.href = redirectURL;