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.

URLSearchParams.delete() method in ServiceNow

Pompha
Mega Contributor

Hi,

I am trying to use URLSearchParams.delete() method and I am getting a compilation error. 

Error is 

"Could not save record because of a compile error: JavaScript parse error at line (25) column (22) problem = missing name after . operator (<refname>; line 25)"

Code is

var url = top.location.href;
var params = new URLSearchParams(url);
params.delete('foo');

Has anyone seen this issue or know how to fix this issue, please let me know.

Thanks

7 REPLIES 7

var url = new URL(top.location.href); // Parse the full URL
var queryParams = new URLSearchParams(url.search); // Extract only the query string
queryParams.delete('id');
queryParams.delete('table');

@Mohammed_Iqbal  
Still getting the same error - "Could not save record because of a compile error: JavaScript parse error at line (26) column (25) problem = missing name after . operator (<refname>; line 26)"

Could you please provide URL. Also, please let me know where you are using this.

 

you can try below code without Object.

URL -- https://example.com/incident.do?id=12345&table=incident&user=admin

After removing id and table -- https://example.com/incident.do?user=admin

 

var url = top.location.href; /

var queryString = url.split('?')[1] || ''; /

var queryParams = queryString.split('&').filter(param => {

return !param.startsWith('id=') && !param.startsWith('table=');

});

var newUrl = url.split('?')[0] + (queryParams.length ? '?' + queryParams.join('&') : '');

 

Try with below code

 

var url = top.location.href; 

var queryString = url.split('?')[1] || ''; 

var queryParams = queryString.split('&').filter(param => {

return !param.startsWith('id=') && !param.startsWith('table=');

});

var newUrl = url.split('?')[0] + (queryParams.length ? '?' + queryParams.join('&') : '');

 

 

HOPE THIS HELP... Please MARK CORRECT OR HELPFUL!!!!