Get 'id' value of service portal URL in catalog client script

Kalesh A1
Tera Contributor

For example if I have a URL that looks like below

https://xxx.service-now.com/sp?id=I_NEED_THIS_VALUE&sys_id=XXXXXXXXXXXXXXXXX&sysparm_category=XXXXXX...

 

From this url is it possible to get value of 'id' in catalog client script

1 ACCEPTED SOLUTION

Vishal Birajdar
Giga Sage

Hi @Kalesh A1 

 

Yes you can get it.

onLoad clinet script 

     //get URL
    var gUrl = top.location.href;
alert(gUrl);
var getId = gUrl.split('?')[1].split('&')[0].split('=')[1];
alert(getId);

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

7 REPLIES 7

Peter Bodelier
Giga Sage

Hi @Kalesh A1,

 

There may be multiple ways:

var url = document.URL.parseQuery();
    if (url['id']){
 var id = decodeURI(url['id']);
}
var gUrl = new GlideURL();
gUrl.setFromCurrent();
var id = decodeURI(gUrl.getParam('id'));

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

1. For first code snippet it gave me a console error:

KaleshA1_0-1695352801647.png

2. Second code snippet gave me a different JavaScript console error

KaleshA1_1-1695352916925.png

 

 

Hi @Kalesh A1,

 

Sorry, I was thinking backend 🙂

 

Try this:

function onLoad() {

alert(getParameterValue('id'))
}   
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]);  
}  
}

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Aman Kumar S
Kilo Patron

Hi @Kalesh A1 

Please refer to below link for the code and explanation:

http://servicenowguru.com/scripting/client-scripts-scripting/parse-url-parameters-client-script/

 

 

Best Regards
Aman Kumar