top.window.location not working in service portal

Naresh291
Tera Contributor

Hi ,

I have a requirement to redirect the users on the same tab if a particular ci is selected in a form . I am using top.window.location but its quite finicky .its not working and throwing me an error of "browser console error " upon inspecting it says top.window.location is an invalid function.

find_real_file.png

Any alternatives or suggestions to this.

My On change client script is as below:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ci = g_form.getValue('cmdb_ci');
if (ci== 'd4e7b6766fd8460026a6bce0be3hd657') {
var url = "/sp?id=sc_cat_item&sys_id=ae5e9fcd1bf764d0607beb11604bcba1";
top.window.location(url);
}
}

Any leads on this is highly appreciated!

3 REPLIES 3

Pranav Bhagat
Kilo Sage

I would recommend you to use this below code in widget controller

 

function ($window) {
    var c = this;
    c.quantity = 1;


    var isViewedOnDesigner = $window.location.href.toString().indexOf('$spd.do') > -1;
    if(isViewedOnDesigner){
        c.timeout = $timeout(function(){
             //you can put some modal / pop up here
             console.log('Session time out');
                    }).then(function() {
                            $window.location.reload(true);
                    }, function() {
                            $window.location.reload(true);
                    })
        }, c.data.timeout * 60000, false);
        

 

Reference Link 

 

https://community.servicenow.com/community?id=community_question&sys_id=10791a56db75930058dcf4621f961951

Alok Das
Tera Guru

Hi Naresh,

I believe you are using this in a catalog client script, change the isolate script to false and if it still doesn't work could you please give a try using "window.location.href(url)" and changing the isolate script to false.

https://community.servicenow.com/community?id=community_question&sys_id=b153909ddb7f23c0e0e80b55ca96...

Kindly mark my answer as Correct and Helpful based on the Impact.

Regards,

Alok

Ankur Bawiskar
Tera Patron
Tera Patron

@Naresh29 

can you update as this

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ci = g_form.getValue('cmdb_ci');
if (ci== 'd4e7b6766fd8460026a6bce0be3hd657') {
var url = "/sp?id=sc_cat_item&sys_id=ae5e9fcd1bf764d0607beb11604bcba1";
top.location.href = url;
}
}

Regards
Ankur

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