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

vkachineni
Kilo Sage

Check the isolate script. it needs to be false on the client script when using document object.

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, it was set to false when I created it. Switching to true didn't work either

Can you put the alert following line

var url = document.URL.parseQuery();
alert("@@@url =" + JSON.stringify(url));

 

Please post the message.

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 your help. No alert shows and then the addInfoMessage no longer shows either