How to extract the id from URL

ursnani
Giga Guru

Hi All,

I am using a Business Rule to Extract the URL parameters and I was able to get the output, but now problem is i have to extract the id from the URL.

I have used "gs.action.getGlideURI().getMap().get('sysparm_query')" to get the parameters of sysparm_query.

in that Query i have like u_field1=12345678909876543212345678^u_field2=nul.

now my requieremnt is to extract the id of Field1.

Can someone please help with this.

Thanks.

1 ACCEPTED SOLUTION

ChrisBurks
Mega Sage

Another way of doing this and making it a reusable script with access to any parameters retrieved is as the following:

function parseSysparmQuery(parameters){
	var params = parameters + "";
	return params.split("^").reduce(function(acc,curr){
		var arr = curr.split("=");
		acc[arr[0]] = arr[1];
		return acc;
	},{})
}

Use it as follows:

var sysparmQuery = gs.action.getGlideURI().getMap().get('sysparm_query');
var getParam = parseSysparmQuery(sysparmQuery);

gs.info("u_field1: " + getParam.u_field1);
gs.info("u_field2: " + getParam.u_field2);

 

It could be included in a Script Include allowing it to be used in many business rules.

View solution in original post

11 REPLIES 11

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Will your fields remain the same i.e. starting string of sysparm_query will always be u_field1? 

if yes then you can do string manipulation and retrieve that value

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

yes they reamin same all the time as u_field1

Can you please explain how to do the manipulation ?

 

I tried like this also

var str = gs.action.getGlideURI().getMap().get('sysparm_query').toString();

output is same "u_field1=12345678909876543212345678^u_field2=nul"

but after that how to split the values i tried inthe following way

str.split("=");

 

but didnt work it is coming as "Ljava.lang.String;@be669e"

 

Try assigning gs.action.getGlideURI().getMap().get('sysparm_query') and concatenating with empty quotes in a var. 
Then on a new line convert to an array with split("^") as there may be more than one criteria.
Here is an example of breaking each criteria and it' s value into a separate object and placing in an array removing 'EQ' if at the end

var q = gs.action.getGlideURI().getMap().get('sysparm_query') + "";
	q = q.split("^")
	q.filter(function(item){
	   return item !== 'EQ';
	}).map(function(item){

	        var arr = item.split("=");
	        var obj = {field: arr[0], value: arr[1]}
		  gs.log(obj.field + " : " + obj.value + " :list", "ACTION")
	})

//q output will be an array of objects of {field: 'name of field', value: 'field value'}