Pass URL parameters - service portal

arobertson
Tera Guru

Hi,

I've been using the following script (below) for years to pass URL parameters and just found out that it does not work in Service Portal. I've seen a few possible workarounds but none of them worked.

Anyone have a good solution to getting this to work in Service Portal? i would normally use the below in an onLoad script.

Closest thing i found to a workable fix is: https://jace.pro/post/2018-07-15-sp-set-variables-via-url/ but not 100% clear how to implement it.

var appointment = getParameterValue('sysparm_appointment');

function getParameterValue(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location);
if (results == null) {
return "";
} else {
return unescape(results[1]);
}
}

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

that's because window object is not recommended by ServiceNow; instead use this and check

var url = top.location.href;
var appointment = new URLSearchParams(url).get("sysparm_appointment");

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

that's because window object is not recommended by ServiceNow; instead use this and check

var url = top.location.href;
var appointment = new URLSearchParams(url).get("sysparm_appointment");

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Many thanks 🙂

I was able to get away with replacing window.location with top.location.href

Much appreciated.

Excellent solution, thanks for sharing.

 

Kind Regards

 

Ashley