Get URL value in client script

Rj27
Mega Guru

Hi,

I am trying to load portal page from a ui page. This page is a record producer.
I am able to to that (loading portal page).
Suppose my url that coming on portal is :https://<INSTANCE>.service-now.com/portal?id=sp_home&sysparm_product_sysid=<product sys id>

I want to give a check if sysparm_product_sysid has some value then the a field on item should be auto checked as true on load.

For this i have added a onload client script in my record producer. 

My ui type is set to Mobile/Service portal on client script .

and I have enabled isolate script to true.


Tried both ways but none of them is working. When the portal is loaded, its giving error..

1. 

   var prdID = getParmVal('sysparm_product_sysid');
   if(prdID){
      alert('prd');
	   //g_form.addInfoMessage('prd'); 
   }
}

function getParmVal(name){
    var url = document.URL.parseQuery();
    if(url[name]){
		alert('inside function');
        return decodeURI(url[name]);
    }
    else{
        return;
    }
	

 

Error:
find_real_file.png

 

2.

var gUrl = new GlideURL();
gUrl.setFromCurrent();
var prdID= gUrl.getParam("sysparm_product_sysid");
alert('prdID  '+prdID);

Error message:
find_real_file.png


Reference pages:
https://servicenowguru.com/scripting/client-scripts-scripting/parse-url-parameters-client-script/

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

7 REPLIES 7

Arpitha92
Tera Contributor

Worked like a charm! Thank you ðŸ˜Š

It worked, I appreciate it

Paul Kunze
Tera Guru

Hi, you can use the property top.location.href in combination with the JavaScript object URLSearchParams. This makes it very flexible and easy to use.

Here is an example script:

        var url = top.location.href;
        var params = new URLSearchParams(url);
        var parentSysId = params.get('sysparm_collectionID');
        if (parentSysId) {
            g_form.setValue('task', parentSysId);
        }