The CreatorCon Call for Content is officially open! Get started here.

How to retrieve URL parameters in a scoped app in an onLoad client script

Sheryl Lyke2
Tera Guru

Within a scoped app, I have a UI action on a custom table form with the following code:

 var nameCI = current.name;
 var comp = current.invoice_parent.company;
 var url = 'cmdb_ci.do?sys_id=-1&sysparm_view=contract_management&sysparm_view_forced=true?sysparm_CIname=' + nameCI + '?sysparm_Comp=' + comp;

 action.setRedirectURL(url);
 action.setReturnURL(current);

This works. Then I have an onLoad client script where I want to take the parameter values being sent in the url to populate fields. I've seen similar code working so I don't know if it's because I'm in a scoped app but it's not working. It doesn't populate. Even the addinfoMessage or setMandatory pieces don't work. I had an addInfoMessage at the start and that did work so I know the script is triggering. Any idea why I can't use the getParameterValue function here? Or does someone have another way to send values from a UI action to a client script in a scoped app? I've tried setting isolate script to true and this also does not have any effect. Thanks!

function onLoad() {
	g_form.setMandatory('model_id', true);
        var nm = getParameterValue('sysparm_CIname');
   
    if (nm) {
        g_form.setValue('name', nm);
    } else {
        g_form.addInfoMessage('no name');
    }
    var comp = getParameterValue('sysparm_Comp');
    if (comp) {
        g_form.setValue('company', comp);
    }
}

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

Can you try commenting out this line in your UI action

 

//action.setReturnURL(current);

See if the url alert goes to cmdb_ci

 

Also can you check the url formation. it has some extra '?'

 

It should be

var url = 'cmdb_ci.do?sys_id=-1&sysparm_view=contract_management&sysparm_view_forced=true&sysparm_CIname=' + nameCI + '&sysparm_Comp=' + comp;

 

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

View solution in original post

16 REPLIES 16

I was so hopeful for this to work! I tried both but it's still not pulling in the parameters.

 

find_real_file.png

function onLoad() {
    g_form.setMandatory('model_id', true);

    var glideURL = new GlideURL();
    glideURL.setFromCurrent();
    var comp = glideURL.getParam("sysparm_Comp");
    var CI = glideURL.getParam("sysparm_CIname");

    if (comp) {
        g_form.setValue('company', comp);
    } else {
        g_form.addInfoMessage('test' + CI );
    }

    var url = top.location.href;
    var comp2 = new URLSearchParams(url).get("sysparm_Comp");

    if (comp2) {
        g_form.setValue('company', comp2);
    } else {
		g_form.addInfoMessage('msg2 ' + comp2);
    }


}

 

nav_to.do?uri=%2Fcmdb_ci.do%3Fsys_id%3D-1%26sysparm_view%3Dcontract_management%26sysparm_view_forced%3Dtrue%3Fsysparm_CIname%3DNETAPP%2520PARTNERCHOICE%25204HR%2520PARTS%2520REPL%2520AND%3Fsysparm_Comp%3Dec251e202b3bb0002918764317da157b

 

I'm attaching the info messages which show no value is getting picked up. You can see in the url that sysparm_CIname adn sysparm_Comp are being sent

Hi Sheryl,

Can you try to run the code as below; I believe either of that should work

I am doing alert for the url as well; check whether it is coming properly or not

function onLoad() {
    g_form.setMandatory('model_id', true);

    var glideURL = new GlideURL();
    glideURL.setFromCurrent();
    var comp = glideURL.getParam("sysparm_Comp");
    var CI = glideURL.getParam("sysparm_CIname");

    alert('Comp is: ' + comp);
    alert('CI is: ' + CI);


    var url = top.location.href;

    alert('URL is: ' + url);

    var comp2 = new URLSearchParams(url).get("sysparm_Comp");
    var CI2 = new URLSearchParams(url).get("sysparm_CIname");
    alert('Comp2 is: ' + comp2);
    alert('CI2 is: ' + CI2);

}
    

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Same results unfortunately 

Sheryl,

The URL does not have the parameters you are expecting

Hence they are undefined & null.

//try adding these lines as parameter 'sysparm_record_target' is in the url
var target = glideURL.getParam("sysparm_record_target");
alert('target : ' + target);

 

 

find_real_file.png

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Thanks for sticking with me!! I see... it's picking up the url before it goes to the new record. Why would that be? The client script is onLoad for the cmdb_ci table? 

Here's the result for the target alert

find_real_file.png