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.

Decode URI is not returning correct value , not working as expected

deepum
Giga Guru

Hello All,

I am trying to decode a URL, but due to the special character "&", the url is not coming back as expected. 

url that has to comeback: com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=342dc43a60d929007c9ff3c5c6f68fcf&sysparm_filter=assignment_group=da73fd09db2963001fafd0c0ce961951^state=1^assigned_to=d8d103f4c8722500ceb1e6aedf04ae98^last_found<javascript:gs.dateGenerate('2022-08-04','start')&sysparm_vulid=&sysparm_filtertype=defined

In the above link, in the sys_param_filter because of the highlighted portion, the url is not taking the entire filter, the url is taking until last found and the other conditions in the filter is being ignore, which results in the issue of list of the records. How to deal with this issue. used below code. 

Please help me on this. Thanks

Below is my code. the filter value is not working as expected when ever we use greater or less than in the filter.

function onLoad() {
    //Populate the variables with the parameters passed in the URL
    //Use the 'getParmVal' function below to get the parameter values from the URL
    var filterV = getParmVal('sysparm_filter');
    var filterTypeV = getParmVal('sysparm_filtertype');
    var vulV = getParmVal('sysparm_vulid');
    
    if(filterV){
        g_form.setValue('u_vulnerability_filter',filterV);
    }
    if(vulV){
        g_form.setValue('u_vulnerable_ids',vulV);
    }
    if(filterTypeV){
        g_form.setValue('u_filter_type',filterTypeV);
    }
}

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

1 ACCEPTED SOLUTION

Hello Mahendra,

I got this issue resolved. The filter which I am using in this client script is being rendered from a UI Page.  I had to add a condition as below in that script

var queryF = request.getParameter("query_filter").toString();
if (queryF.includes("&lt") || (queryF.includes(">"))) {
    queryF = queryF.replace("&amp;lt;", "<");
    queryF = queryF.replace("&gt;", ">");
}

 

Thanks for your script. 

View solution in original post

14 REPLIES 14

Harshad Wagh
Tera Guru

Maybe try following

function onLoad() {
    //Populate the variables with the parameters passed in the URL
    //Use the 'getParmVal' function below to get the parameter values from the URL
    var filterV = getParmVal('sysparm_filter');
    var filterTypeV = getParmVal('sysparm_filtertype');
    var vulV = getParmVal('sysparm_vulid');
    
    if(filterV){
        g_form.setValue('u_vulnerability_filter',filterV);
    }
    if(vulV){
        g_form.setValue('u_vulnerable_ids',vulV);
    }
    if(filterTypeV){
        g_form.setValue('u_filter_type',filterTypeV);
    }
}

function getParmVal(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]);
	}
}

Hello Harshad,

The results return as null. I have alerted as below.

I dont see any url returning here. what I did is correct??

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

deepum
Giga Guru

 @Ankur Bawiskar ,

is this something you can help? Please

ar1
Kilo Sage

Hi @Ankur Bawiskar ,
Could you please help us here.

Advance thanks.