- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2022 05:17 PM - edited 10-30-2022 05:19 PM
I am working on the script below to get the asset parameter from the URL of catalog item.
However, the onload script is not working or getting null. Can someone please provide some guidance as to how make it work. I am getting browser error and not sure how to fix it. Your expert advice will be appreciated. Thanks
The onload script is:
function onLoad() {
//Use the 'getParameterValue' function below to get the parameter values from the URL
var asset = getParameterValue('sysparm_asset');
if (asset) {
g_form.setValue('asset', asset);
}
}
function getParameterValue(name) {
var url = document.URL.parseQuery();
if (url[name]) {
return decodeURI(url[name]);
} else {
return;
}
}
The browser error is:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2022 08:57 PM - edited 10-30-2022 08:57 PM
Use the following updated code for getParameterValue() function
function getParameterValue(name) {
var url = top.location.href;
var value = new URLSearchParams(url).get(name);
if (value) {
return value;
}
if (!value) {
var gUrl = new GlideURL();
gUrl.setFromCurrent();
value = gUrl.getParam("sysparm_id");
return value;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2022 08:57 PM - edited 10-30-2022 08:57 PM
Use the following updated code for getParameterValue() function
function getParameterValue(name) {
var url = top.location.href;
var value = new URLSearchParams(url).get(name);
if (value) {
return value;
}
if (!value) {
var gUrl = new GlideURL();
gUrl.setFromCurrent();
value = gUrl.getParam("sysparm_id");
return value;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2022 06:59 PM - edited 10-31-2022 07:01 PM
Sai your guidance helped me fix the issue. My only outstanding issue is that I am not able to populate the remaining catalog item. The script is below in case that you have any additional advise to help me fix the remaining issue.
function onLoad() {
//Use the 'getParameterValue' function below to get the parameter values from the URL
alert('parameter onload= '+asset);
var asset = getParameterValue('sysparm_asset');
if (asset) {
g_form.setValue('asset', asset);
var ga = new GlideAjax('getStandardFields'); //this is the script include
ga.addParam("sysparm_name", "getFields"); //this is the function within the script include
ga.addParam("sysparm_asset", asset);
ga.getXML(getResponse);
g_form.clearValue('telecommuter');
}
function getResponse(response) {
var values = response.responseXML.documentElement.getAttribute('answer').toString().split('|');
g_form.setValue('asset_use', values[0]);
g_form.setValue('state', values[1]);
g_form.setValue('managed_by', values[2]);
g_form.setValue('location',values[3]);
g_form.setValue('building', values[4]);
g_form.setValue('floor', values[5]);
g_form.setValue('column', values[6]);
g_form.setValue('room_cube',values[7]);
}
}
function getParameterValue(name) {
var url = top.location.href;
var value = new URLSearchParams(url).get(name);
if (value) {
return value;
}
if (!value) {
var gUrl = new GlideURL();
gUrl.setFromCurrent();
value = gUrl.getParam("sysparm_id");
return value;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2022 08:53 PM
@Luis Roman1 Can you post the Script include code here?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2022 11:07 PM - edited 10-30-2022 11:14 PM
Hi @Luis Roman ,
The reason why your script is not working is, you are using document which is not defined yet at the time of your onload client script execution. Instead of document object, you can use the top object here with same process and can achieve your requirement as shown below:
function onLoad() {
//Use the 'getParameterValue' function below to get the parameter values from the URL
var asset = getParameterValue('sysparm_asset');
if (asset) {
g_form.setValue('asset', asset);
}
}
function getParameterValue(name) {
var url = top.location.href.parseQuery(); //Use top instead of document
if (url[name]) {
return decodeURI(url[name]);
} else {
return '' ;
}
}
I Hope this helps.
Please mark this helpful if this helps and Accept the solution if this solves your issue.
Regards,
Kamlesh