Script not working: document.URL.parseQuery(); [Scripting and Coding]

Luis Roman
Kilo Guru

I am working on the script below to get the asset parameter from the URL of catalog item.

https://dev58970.service-now.com/sp?id=sc_cat_item&sys_id=4ef5c3a62fba511076795fd92799b6f7&sysparm_a...

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: 

LuisRoman_0-1667175145452.png

 

 

1 ACCEPTED SOLUTION

Sai Kumar B
Mega Sage
Mega Sage

@Luis Roman 

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;
        }
    }

 

 

View solution in original post

4 REPLIES 4

Sai Kumar B
Mega Sage
Mega Sage

@Luis Roman 

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;
        }
    }

 

 

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;
}
}

@Luis Roman1 Can you post the Script include code here?

kamlesh kjmar
Mega Sage
Mega Sage

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