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

Try var json = new global.JSON(); alert(json.stringify(url));
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

/*Tested on PDI. Thanks to Ankur*/

function getParameterValue(name) {
var glide_url = new GlideURL();
glide_url.setFromCurrent();
var target = glide_url.getParam(name);
if (target) {
return decodeURI(target);
} else {
return;
}
}

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

sayali udgave
Kilo Guru

Hi,

if on your client script showing info message in blue color then you have to set 'glide.script.block.client.globals' into  sys_properties.LIST and set value to false

 

and write

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

 

This works for me. Hope it will works for you as well

 

Thanks

Sayali

sn_si.glide.script.block.client.globals was set to false. Unfortunately this didn't work for me with this code

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sheryl,

document object won't work in scoped app

Can you update code as below and check once. this should work; try either of the approach

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 glideURL = new GlideURL();
glideURL.setFromCurrent();
var comp = glideURL.getParam("sysparm_Comp");

 if (comp) {
        g_form.setValue('company', comp);
    }


}

OR

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 url = top.location.href;
var comp = new URLSearchParams(url).get("sysparm_Comp");

 if (comp) {
        g_form.setValue('company', comp);
    }

}

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