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

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

Thank you! Finally got it working. I did have an errant ? in there. Final code for others:

UI Action 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);

 

OnLoad client script

function onLoad() {
    g_form.setMandatory('model_id', true);
	g_form.setMandatory('location', true);
    var glideURL = new GlideURL();
    glideURL.setFromCurrent();
    var comp = glideURL.getParam("sysparm_Comp");
	if (comp) {
        g_form.setValue('company', comp);
    }
    var CI = glideURL.getParam("sysparm_CIname");
	if (CI) {
        g_form.setValue('name', CI);
    }

 

The setMandatory is still not working but it looks like that's due to it being on a global table