Catalog Client Script not setting the reference variable using URL parameter (value keeps reverting)

SwethanaB
Mega Contributor

Hi team,

I’m working on a Catalog Client Script (onLoad) where I need to populate a reference variable (configuration_item) based on a URL parameter ci=1.

 
OnLoad script:

function onLoad() {
    var ci = g_form.getParameter('ci');
    g_form.clearValue('configuration_item');
    if (ci === '1') {
        g_form.setValue('configuration_item', 'sys_id');
    }
}
 
The variable still loads the previously used value, even after clearValue().
6 REPLIES 6

vaishali231
Tera Guru

hey @SwethanaB 
In Service Catalog, g_form.getParameter() does not reliably work for URL parameters. Also, reference variables may retain the last selected value due to cart/session caching, which is why clearValue() alone does not resolve the issue.

Instead, use g_request.getParameter() and pass a separate parameter for the CI sys_id (do not reuse sys_id, as that belongs to the catalog item).

URL format:

https://<instance>.service-now.com/<catalog_page>?id=sc_cat_item&sys_id=<catalog_item_sys_id>&ci=1&ci_sys_id=<ci_sys_id>

Catalog Client Script (onLoad):

function onLoad() {

// Always clear first to avoid cached value
g_form.setValue('configuration_item', '');

var ciFlag = g_request.getParameter('ci');
var ciSysId = g_request.getParameter('ci_sys_id');

if (ciFlag == '1' && ciSysId) {

// Small delay ensures it runs after form rendering
setTimeout(function() {
g_form.setValue('configuration_item', ciSysId);
}, 100);
}
}

*********************************************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

 

Regards

Vaishali Singh

 

 

Hello Vaishali,

 

Thanks for the response!

I have tried the solution you provided but still I am getting the below error and it is taking the previously used data.

There is a JavaScript error in your browser console

Thanks!

hey @SwethanaB 

could you please share screenshot ?

Ankur Bawiskar
Tera Patron

@SwethanaB 

that's not going to work

This will work in portal

function onLoad() {

    var url = top.location.href;
    var sysId = new URLSearchParams(url).get("sys_id");
    alert(sysId);

    var ci = new URLSearchParams(url).get("ci");
    if (ci == '1')
        g_form.setValue('configuration_item', sysId); // check variable name is correct
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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