Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How do you read URL from Catalog Client Script

marclindsay
Tera Guru

I have created a UI Action that runs server side and redirects a user to a Record Producer. The UI Action adds data to the URL (UI Action Redirect : com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=21378b88db9e2300c218d740cf961961&sysparm_comments=testing passing information). Within the Catalog Client Script I am trying to read the URL to get the parameter needed for defaulting some of the fields on the Record Producer screen.

Everything I have read looks like I should be able to do the following to get my data out of the URL. document.URL.parseQuery();  This command is not working. I keep getting the error 'Cannot read property 'URL' of null'.

Stuck as to how I get my information out of the URL

1 ACCEPTED SOLUTION

Ok, Can you try this once.

var gUrl = new GlideURL();
gUrl.setFromCurrent();
var comments = gUrl.getParam("sysparm_comments");
g_form.setValue('description', comments);

 

View solution in original post

16 REPLIES 16

Anurag Tripathi
Mega Patron
Mega Patron

Hi M,

 

So lets say you want to read u_xyz parameter from url, lere is the sample code

var xyz= getParmVal('u_xyz');   //called from client script

 

 

//Function in the same script

 

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

 

 

-Anurag

that is what I am doing but it I am getting an error. I am not doing this from a Service Portal. Just using the native screens (ServiceNow Internal / Fullifer view).

Below is the code in my Client Script, and I keep getting an error with the document.URL.parseQuery(); statement.  Error "Cannot read property 'URL' of null"

function onLoad() {
    
   
    //Type appropriate comment here, and begin script below
    var comm = getParmVal('sysparm_comments');
    g_form.addInfoMessage("after Parm comment : " + comm);
    
    g_form.setValue("description", comm);
       
}

function getParmVal(name) {

    var url;
   
    try {
        url = document.URL.parseQuery();
        g_form.addInfoMessage("Parm 2b : " + url);
    }
    catch (err) {
        g_form.addInfoMessage(err.message);
    }
    g_form.addInfoMessage("Parms 3 : " + url);

     if(url[name]){
         return decodeURI(url[name]);
     }
     else{
         return;
     }

}

Alikutty A
Tera Sage

Hello,

Please try this out

function onLoad() {
var desc = getParameterValue('sysparm_comments');
if (desc) {
g_form.setValue('description', desc);
}

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

 

Did this. I am still getting an error with the document.URL.parseQuery() statement. 

I am doing this in a personal Instance would that be impacting this command?