Not able retrieve URL Parameters through onload client script IN Portal

R Akash
Kilo Expert

Hello,

I have a requirement where i need to populate category for one of the catalog item on load where values are coming as URL Parameters.

I have written onLoad client script to get URL Parameters, but its not working in portal.

Onload URL: 

/fuse?id=sc_cat_item&sys_id=34b896be1b6320104c6a54ea234bcbe2&sysparm_category=Benifits

client script:

function onLoad() {
//Type appropriate comment here, and begin script below
var category=getParmVal("sysparm_category");
alert(category);
}

Do "getParmVal" will work in portal, if not is there any way to access these parameters onload of catalog item in portal?

 

 

Thanks

Akash

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Akash,

I've used the following script. In the script, I'm setting the value to field named "field1" on the form.

function onLoad() {
    alert("hi");
    g_form.setValue('field1', getParameterValue('sysparm_category'));


    function getParameterValue(name) {
        var url = top.location.href;
        var value = new URLSearchParams(url).get(name);
        if (value) {
            return value;
        }
        if (!value) {
            try {
                var gUrl = new GlideURL();
                gUrl.setFromCurrent();
                value = gUrl.getParam(name);
                if (typeof value == 'undefined') {
                    return '';
                } else {
                    return value;
                }
            } catch (e) {}
        }
    }
}

Execution:

sp?id=sc_cat_item&sys_id=<sys_id of form>&sysparm_category=Benifits

find_real_file.png

View solution in original post

5 REPLIES 5

Chetan Mahajan
Kilo Sage
Kilo Sage

Hi,

                 Please go through below mail thread, it may help you.

https://community.servicenow.com/community?id=community_question&sys_id=dab68b65db1cdbc01dcaf3231f9619cf

https://servicenowguru.com/scripting/client-scripts-scripting/parse-url-parameters-client-script/

 

Kindly mark correct/helpful, if its helpful 

Filipe Cruz
Kilo Sage
Kilo Sage

Hello R Akash,

You need to declare the function getParmVal:

var myparm = getParmVal('sys_myparm');

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

 

So your client script should look like:

function onLoad() {
//Type appropriate comment here, and begin script below

var category=getParmVal("sysparm_category");
alert(category);

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

}

 

Hope this helps!

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Best Regards,

Filipe Cruz

Hello Filipe,

Added below Code in onload client script still no luck. On load of catalog item in portal, i am getting JavaScript error and alert message is also not coming.

 

find_real_file.png

Thanks

Akash

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Akash,

I've used the following script. In the script, I'm setting the value to field named "field1" on the form.

function onLoad() {
    alert("hi");
    g_form.setValue('field1', getParameterValue('sysparm_category'));


    function getParameterValue(name) {
        var url = top.location.href;
        var value = new URLSearchParams(url).get(name);
        if (value) {
            return value;
        }
        if (!value) {
            try {
                var gUrl = new GlideURL();
                gUrl.setFromCurrent();
                value = gUrl.getParam(name);
                if (typeof value == 'undefined') {
                    return '';
                } else {
                    return value;
                }
            } catch (e) {}
        }
    }
}

Execution:

sp?id=sc_cat_item&sys_id=<sys_id of form>&sysparm_category=Benifits

find_real_file.png