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.

Unable to decompose a URL to get the parameters

ArpitaVK
Tera Expert

Hello, I am trying to decompose a URL and get its parameters. I was able to get the URL using catalog client script. In that used URL(top.location).

Also, I was able to get the rest of the required parameters. But there is one parameter 'url' in that URL. I am getting null value for it even though it is present in the URL. How to get that parameter value? 

 

here is the sample URL: https://instance_name.service-now.com/sp?id=sc_cat_item&sys_id=sample_sys_id&sysparm_category=category_sys_id%3Furl%3Dhttp:%2F%2Fsample_url%2Fhttp-post%2F&referer=&reason=abc&reasoncode=xyz

1 ACCEPTED SOLUTION

Hi @ArpitaVK 

 I have tried the URL and code it's working for me.

 https://instance_name.service-now.com/sp?id=sc_cat_item&sys_id=bd0c5e6a97040210b958f84de053af8c&sysparm_category=e15706fc0a0a0aa7007fc21e1ab70c2f&url=http:%2F%2Fsample_url%2Fhttp-post%2F&referer=&reason=abc&reasoncode=xyz

 

function onLoad() {
   //Type appropriate comment here, and begin script below
   
   var url = getParameterValue('url');
   //alert(url);
    var blockReason = getParameterValue('reason');
    var reasonCode = getParameterValue('reasoncode');
    var action = getParameterValue('action');
    var urlCategory = getParameterValue('sysparm_category');   

    var desc = 'Block Reason: ' + blockReason + '\n Reason Code: ' + reasonCode + '\n Action: ' + action + '\n URL Category: ' + urlCategory;

    var shortDesc = 'url = ' + url;
	alert("desc:"+"\n"+desc  +"\n"+"shortDesc :"+shortDesc);

    //g_form.setValue('short_description', shortDesc);
   // g_form.setValue('description', desc);

}

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

 

dgarad_0-1716295704794.png

 

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

View solution in original post

9 REPLIES 9

function onLoad() {

    var url = getParameterValue('url');
    var blockReason = getParameterValue('reason');
    var reasonCode = getParameterValue('reasoncode');
    var action = getParameterValue('action');
    var urlCategory = getParameterValue('cat');   

    var desc = 'Block Reason: ' + blockReason + '\n Reason Code: ' + reasonCode + '\n Action: ' + action + '\n URL Category: ' + urlCategory;

    var shortDesc = 'url = ' + url;

    g_form.setValue('short_description', shortDesc);
    g_form.setValue('description', desc);

}

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

}

 try the above code.

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

Hi @dgarad 

this is not working for me.

 

can you share the URL  you try.

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

Hi @ArpitaVK 

 I have tried the URL and code it's working for me.

 https://instance_name.service-now.com/sp?id=sc_cat_item&sys_id=bd0c5e6a97040210b958f84de053af8c&sysparm_category=e15706fc0a0a0aa7007fc21e1ab70c2f&url=http:%2F%2Fsample_url%2Fhttp-post%2F&referer=&reason=abc&reasoncode=xyz

 

function onLoad() {
   //Type appropriate comment here, and begin script below
   
   var url = getParameterValue('url');
   //alert(url);
    var blockReason = getParameterValue('reason');
    var reasonCode = getParameterValue('reasoncode');
    var action = getParameterValue('action');
    var urlCategory = getParameterValue('sysparm_category');   

    var desc = 'Block Reason: ' + blockReason + '\n Reason Code: ' + reasonCode + '\n Action: ' + action + '\n URL Category: ' + urlCategory;

    var shortDesc = 'url = ' + url;
	alert("desc:"+"\n"+desc  +"\n"+"shortDesc :"+shortDesc);

    //g_form.setValue('short_description', shortDesc);
   // g_form.setValue('description', desc);

}

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

 

dgarad_0-1716295704794.png

 

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

@dgarad Thank you so much! This works for me. The URL I was using had '%3Furl%3D' instead of '&url='. That's why it was not getting decomposed.

Also, on a side note, 'unescape()' is deprecated. Instead, we can use 'decodeURIComponent()'.