The Zurich release has arrived! Interested in new features and functionalities? Click here for more

"top.window.location" not working in onSubmit catalog client script?

patricklatella
Mega Sage

Hi all,

I've got a catalog item with an onSubmit catalog client script that builds a URL, and then when the user submits the form they need to be redirected to that URL.  Did something with "top.window.location" break in New York?  The script is working about half the time, sometimes it correctly directs the user to the URL in the script, and sometimes it ignores that script.  Anyone else seeing this behavior or see an issue with my script?  Thanks!

Here's the script:

function onSubmit() {

var req = g_form.getValue('enter_your_vsa_request_number_to_get_started');//this is a reference field to the [sc_request] table
var URL = "itc?id=sc_request&table=sc_request&sys_id="+req;
alert('You will be directed to the Selected Vendor Assessment Request. Please click OK.');
top.window.onbeforeunload = null;//this removes the popup that asks if you want to leave
top.window.location = URL;
}

1 ACCEPTED SOLUTION

Michael Jones -
Giga Sage

You could try a good, old-fashioned setTimeOut. Move the last part of your code into a separate function and have it call-back after a second or so. 

function onSubmit() {

var req = g_form.getValue('enter_your_vsa_request_number_to_get_started');//this is a reference field to the [sc_request] table
var URL = "itc?id=sc_request&table=sc_request&sys_id="+req;
alert('You will be directed to the Selected Vendor Assessment Request. Please click OK.');

top.window.onbeforeunload = null;//this removes the popup that asks if you want to leave
setTimeout(sendOff(URL), 1000);
}

function sendOff(URL) {
top.window.location = URL;
}

That seems to be working every time for me...

If this was helpful or correct, please be kind and remember to click appropriately!

Michael Jones - Proud member of the CloudPires team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

View solution in original post

15 REPLIES 15

Michael Jones -
Giga Sage

Is this in the UI view or the Service Portal? You definitely won't be able to use this method in the Service Portal.

If this is in the UI then check to see if the Isolate Script flag is set to true for that particular script on that item (when set to true, you are unable to access DOM elements even in the UI). Can't explain why it would work sometimes and not others, unless you mean with a different item...

If this was helpful or correct, please be kind and remember to click appropriately! Michael Jones - Proud member of the CloudPires team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

Hi Michael, thanks for the feedback...this is definitely in Service Portal...do you know another redirect script I can use?

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Feels like correct what you are describing.
Sometimes it would work (so the redirect actually does work), sometimes not. Reason: onSubmit. The record is submitting. So depending on the performance, which kicks in first? The submission or the onSubmit client script.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hi Mark,

thanks for responding.

I would think the system would always run the onSubmit script before processing...isn't that how it works?  Do you know an alternative way to achieve what I'm shooting for?