Catalog Item not prefilling the form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
I have a catalog item that needs to prefill two fields, the short description and description.
I have the link setup to send the data over like so.
/sp?id=sc_cat_item&sys_id=72ba83d1b35313007a6de81816a8dc54&sysparm_variables=%7B&var_short_description=NDA%20Only%20Request%26var_description%3DType%20in%20the%20name%20of%20the%20vendor%7D
On the receiving catalog item, I have a Catalog Client Script that is setup in the following way. When I am testing it, the form loads, but the data doesn't insert into the field. I think it's with the function starting line 18 (see code below) but not exactly sure what is going on there. Can anyone help?
function onLoad() {
// Wait for g_url to exist (Portal loads async)
var interval = setInterval(function() {
if (typeof g_url !== "undefined") {
// Stop checking
clearInterval(interval);
var sd = getParameterValue('var_short_description');
var desc = getParameterValue('var_description');
// g_form.addInfoMessage('var_short_description');
if (sd) g_form.setValue('short_description', sd);
if (desc) g_form.setValue('description', desc);
}
}, 200);
}
function getParameterValue() {
var objVariables = getParameterValue('sysparm_variables');
console.debug('sysparm_variables URI parameter value: ' + objVariables);
if (!objVariables || objVariables == '' || objVariables.indexOf('{') != 0) { //If variables is empty or not properly formatted, stop here. console.debug('aborting due to a missing or problematic sysparm_variables URI parameter');
return;
}
objVariables = JSON.parse(objVariables);
console.debug('sysparm_variables URI parameter value: ' + JSON.stringify(objVariables));
for (prop in objVariables) {
if (!objVariables.hasOwnProperty(prop)) {
break;
}
propVal = objVariables[prop];
console.debug('Processing property ' + prop + ' with value ' + propVal);
if (g_form.hasField(prop)) {
console.debug('Setting variable ' + prop + ' to value ' + propVal);
g_form.setValue(prop, propVal);
} else {
console.warn('Variable ' + prop + ', specified in the sysparm_variables URI parameter, does not exist on this catalog item form.');
}
}
}
function getParameterValue(name) {
var objVariables = getParameterValue('sysparm_variables');
console.debug('sysparm_variables URI parameter value: ' + objVariables);
if (!objVariables || objVariables == '' || objVariables.indexOf('{') != 0) { //If variables is empty or not properly formatted, stop here. console.debug('aborting due to a missing or problematic sysparm_variables URI parameter');
return;
} objVariables = JSON.parse(objVariables);
console.debug('sysparm_variables URI parameter value: ' + JSON.stringify(objVariables));
for (prop in objVariables) { if (!objVariables.hasOwnProperty(prop)) { break;
} propVal = objVariables[prop];
console.debug('Processing property ' + prop + ' with value ' + propVal);
if (g_form.hasField(prop)) { console.debug('Setting variable ' + prop + ' to value ' + propVal);
g_form.setValue(prop, propVal);
} else { console.warn('Variable ' + prop + ', specified in the sysparm_variables URI parameter, does not exist on this catalog item form.');
} } } function getParameterValue(name) { name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
//Check if portal (gel=undefined) or classic UI, and get the HREF data appropriately in either case. var hrefPath = (typeof gel == 'undefined') ? this.location.href : top.location.href;
var results = regex.exec(hrefPath);
console.debug('HREF: ' + hrefPath);
if (results == null) { return "";
} else { return unescape(results[1]);
} }
// }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Hi @Aaron Duncan,
the line 22 - what conditions are you checking there? on the first sight it displays the red bracket:
if (!objVariables || objVariables == '' || objVariables.indexOf('{') != 0) { //If variables is empty or not properly formatted, stop here. console.debug('aborting due to a missing or problematic sysparm_variables URI parameter');
COuldn't it be it? If you simplify the condition to be always satisfied to see if that code works? And eventually play with the conditions, the log gives you what?
100 % GlideFather experience and 0 % generative AI
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
There are multiple issues
Issue 1 — The URL is malformed
The URL :
...&sysparm_variables=%7B&var_short_description=NDA%20Only%20Request%26var_description%3DType%20in%20the%20name%20of%20the%20vendor%7D
When decoded, sysparm_variables only gets { as its value (the raw & after %7B starts a new param). Then %26 and %3D inside the value of var_short_description are encoded ampersand/equals — so var_description never becomes its own URL parameter. It all gets mashed into var_short_description's value.
If you want the sysparm_variables JSON approach, the URL should be:
/sp?id=sc_cat_item&sys_id=72ba83d1b35313007a6de81816a8dc54&sysparm_variables=%7B%22var_short_description%22%3A%22NDA%20Only%20Request%22%2C%22var_description%22%3A%22Type%20in%20the%20name%20of%20the%20vendor%22%7D
That decodes to: sysparm_variables={"var_short_description":"NDA Only Request","var_description":"Type in the name of the vendor"}
If you want individual URL params instead:
/sp?id=sc_cat_item&sys_id=72ba83d1b35313007a6de81816a8dc54&var_short_description=NDA%20Only%20Request&var_description=Type%20in%20the%20name%20of%20the%20vendor
Issue 2 — getParameterValue is defined three times (function name collision)
JavaScript function hoisting means the last definition of getParameterValue(name) (the regex-based one) wins. The middle parameterless definition that parses sysparm_variables as JSON is completely dead code — it never executes. Worse, that middle version calls getParameterValue('sysparm_variables') inside itself, which would cause infinite recursion if it ever did run.
Issue 3 — Wrong field names in g_form.setValue
Lines 12–13 use:
g_form.setValue('short_description', sd);
g_form.setValue('description', desc);
On a Catalog Item form (table sc_cat_item) in Service Portal, the settable fields are the catalog variable names, not the underlying task fields. Since their variables are named var_short_description and var_description, it should be:
g_form.setValue('var_short_description', sd);
g_form.setValue('var_description', desc);
Issue 4 — g_url may not exist in Service Portal
The script polls for g_url to detect when the portal is ready, but g_url is a classic UI / CMS object. In Service Portal, it may never be defined depending on the version, so the setInterval could either spin forever or just happen to work if some other script defines it.
Clean solution — pick one approach:
Approach A: Individual URL params (simpler)
URL:
/sp?id=sc_cat_item&sys_id=72ba83d1b35313007a6de81816a8dc54&var_short_description=NDA%20Only%20Request&var_description=Type%20in%20the%20name%20of%20the%20vendor
Client Script (onLoad, Mobile/Service Portal, Catalog Item: General Legal Request):
function onLoad() {
var defined = (typeof window !== 'undefined');
if (!defined) return;
var sd = getURLParam('var_short_description');
var desc = getURLParam('var_description');
if (sd) g_form.setValue('var_short_description', sd);
if (desc) g_form.setValue('var_description', desc);
}
function getURLParam(name) {
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(window.location.href);
return results ? decodeURIComponent(results[1]) : '';
}
Approach B: sysparm_variables JSON
URL (properly encoded JSON):
/sp?id=sc_cat_item&sys_id=...&sysparm_variables=%7B%22var_short_description%22%3A%22NDA%20Only%20Request%22%2C%22var_description%22%3A%22Type%20in%20the%20name%20of%20the%20vendor%22%7D
Client Script:
function onLoad() {
var raw = getURLParam('sysparm_variables');
if (!raw || raw.indexOf('{') !== 0) return;
try {
var vars = JSON.parse(raw);
for (var prop in vars) {
if (vars.hasOwnProperty(prop) && g_form.hasField(prop)) {
g_form.setValue(prop, vars[prop]);
}
}
} catch (e) {
console.error('Failed to parse sysparm_variables: ' + e);
}
}
function getURLParam(name) {
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(window.location.href);
return results ? decodeURIComponent(results[1]) : '';
}
Also worth noting: unescape() in the original code is deprecated — decodeURIComponent() is the correct replacement and handles UTF-8 properly.
The Catalog Client Script config in the screenshot (table: Catalog Item, type: onLoad, UI type: Mobile/Service Portal, catalog item: General Legal Request) all looks correct — the problems are entirely in the script logic and the URL encoding.
