Redirect user to external website on new tabe using onChange script but not working

trade_wala1
Tera Contributor

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.

19 REPLIES 19

Ankur Bawiskar
Tera Patron
Tera Patron

@trade_wala1 

you want to open in new tab that link or simply show it to users?

we can't add link using showFieldMsg

why not show them the link in info message

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.addInfoMessage(message);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@trade_wala1 

if you wish to open that link in new tab and also show the message then do this

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
        // Show message
        g_form.addInfoMessage(message);

        // Redirect
        top.window.open("https://www.microsoft.com/", "_blank");
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,

I have tried my above script and updated to yours but it doesn't do anything. When i go to test catalog item and select no from drop down, there's no redirection or any error message on browser. 

@trade_wala1 

please share your latest script

I am able to get the info message

 

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader