Redirect user to external website on new tabe using onChange script but not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-09-2025 10:14 PM
I am currently building a Catalog item and I want user to be redirected to an external website (e.g. Microsoft.com) if they select no in the group_hr variable question which is select box type. The goal is to not let them submit the form if they are selecting no in that variable. Below is my script but it is not doing anything when i tested. Please assist:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Check if the new value is 'no'
if (newValue == 'no') {
// Message with a clickable link
var message = 'Please click <a href="https://www.microsoft.com" target="_blank">here</a> to get more information about this access.';
// Show message
g_form.showFieldMsg('group_hr', message, 'info');
// Redirect
top.window.open = "https://www.microsoft.com/";
}
}
I have tried window.location.href for redirect but no luck. It's also not displaying any messages that's in the code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-09-2025 11:10 PM
Hi Ankur,
Please see the below snip.
Do i need to change anything in the fields above the script? I have confirmed that the variable name is also correct and it's value in the select box but no luc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2025 01:50 AM
you are running this script in Desktop i.e. in Service catalog view
there top.window.open won't work
You can use g_navigation for Desktop and top.window.open in portal.
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
‎01-12-2025 04:49 PM
HI Ankur,
Yes I have tried both you suggested but still no luck. The error message comes up but there's no redirection.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2025 07:21 PM
in which view it's not working? native or portal?
I already informed you should use g_navigation in native
Please share your scripts here along with client script config screenshot
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
‎01-12-2025 08:07 PM
Hi Ankur,
Yes I have already tried g_navigation but it gave me this error: Uncaught ReferenceError: g_navigation is not defined
at eval (eval at y (sp_min.jsx?v=12-02-2024_1408:2587:3277), <anonymous>:12:1)
Here's the script I tried:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Check if the user selected 'No'
if (newValue === 'no') {
// Show an error message
g_form.showFieldMsg('group_hr', 'Access denied. Redirecting to the homepage...', 'error');
// Use g_navigation for proper redirection in Service Portal
setTimeout(function() {
g_navigation.open('/sp'); //redirect user to service portal homepage
}, 1000);
// Prevent form submission
g_form.setDisabled('submit', true);
}
}
I am on Xanadu version