Script to parse URL to get parameters šŸ¤·ā€ā™‚ļø

Luis Roman1
Tera Contributor

I need to parse the URL for a catalog item. I am using the following onload script but get browser error.

LuisRoman1_0-1677957165737.png

Can someone please help me overcome this problem. My experience with JavaScript is limited so collaboration will be helpful.

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 cat = getParmVal('sys_parm');
 if(cat){
 g_form.setValue('my_category_variable',cat);
/ }
 if(comments){
 g_form.setValue('my_comments_variable',comments);
 }
}

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

1 ACCEPTED SOLUTION

Based on what I'm guessing is wanted, it would be:

 

function onLoad () {
	var getParmVal = getSearchParams();
	var cat = getParmVal('sys_parm');
	var comments = getParmVal('???');

	if (cat) {
		g_form.setValue('my_category_variable', cat);
	}

	if (comments) {
		g_form.setValue('my_comments_variable', comments);
	}
}

function getSearchParams () {
	var url = new URL(self.location.href);
	var searchParams = url.searchParams;

	return function get (name) {
		return searchParams.get(name)
	};
}

 

But the parameter name (sys_parm) doesn't seem right and I don't know the parameter name for comments.

View solution in original post

9 REPLIES 9

You're welcome šŸ™‚

Thanks for marking the solution!

Arun Kumar D
Tera Contributor

Hello

I'm having a requirement to Create Standard Change from RITM and set Change parent as RITM reference.

I did created below UI action which takes me to the standard change templates landing page,

var url = new StdChangeUtils().getURLForTask(current, 'rfc');
url += '&ritm_sys_id=' + current.sys_id;
action.setRedirectURL(url);

 

In this script, i am injecting ritm_sys_id into the URL and trying extract it & populate the parent field on the change form with the RITM reference.

However the below onLoad client script is not working as expected. 

if (!gForm.isNewRecord()) return;

try {
var fullUrl = window.location.href;

var match = fullUrl.match(/[?&]ritm_sys_id=([0-9a-f]{32})/);
if (match && match[1]) {
var ritmSysId = match[1];
gForm.setValue('parent', ritmSysId);
}
} catch (e) {
console.error('Error extracting ritm_sys_id:', e);
}

Kindly suggest on this.

A reason for that not working is explained in the annotation on Client Script and even Catalog Client Script forms:2025-07-20.png

 

Disclaimer: there might be other errors too, have not analyzed the full script, stopped at windows..

Faizeal Mohamed
Tera Guru

Hi, 

 

Make sure isolate script should be false in client script and UI Type should be All.


Also in the script, below line has extra slash(/)

 

if(cat){
 g_form.setValue('my_category_variable',cat);
}


please make the above changes and try.

 

thanks,

Faizeal.

Option "Isolate script" does not apply to Portal.