Access query parameter in client script

Mohd1
Mega Contributor

Question1: I am trying to access sysparm_query parameter from the url in the client script. I know I can use document.URL.parseQuery() and access the sysparm_query key value from the dictionary.

sysparm_query=active=true^location=abc

For above example, the dictionary returns active=true^location=abc which I can further split by AND(^) operator and loop to retrieve parameters. I am wondering if there is a better way of accessing the individual parameters of sysparm_query.

Question 2: I also want to know, how servicenow retrieves these parameters to populate them in forms like create incident. Example - If i pass caller or description to incident table in sysparm_query, the form auto populates all the values. I was checking all the out of the box clients scripts but couldn't find any which would do that.

3 REPLIES 3

Harsh Vardhan
Giga Patron

try now.

 

Tested and working. 

 

function onLoad() {
   //Type appropriate comment here, and begin script below
	
   var myparm = getParmVal('sysparm_query');
	alert(myparm);

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

}

Harsh Vardhan
Giga Patron

opps , i did not read your question and posted my first reply, by the way once you will get the sysparm_query value then as you had already mentioned you can use split on AND ( split('^')  ) operator so you will get the values, so are you having any challenge by using this approach.

 

coming back to 2nd question , how are you building your encoded query ? 

sample query: 

'&sysparm_query='+'active='+current.active+'^location='+current.location;

 

Thank you for your response. The split logic will work, but I am curious if there is a better way of doing this instead of splitting it up and then iterating through the array to find out individual params.

For the second question, I just want to know how servicenow handles the exact same thing for other parameters. Like if you append user and description to incident form, servicenow pulls the information automatically and populates the respective field. I am using UTF-8 url encoding but that is not an issue here.