Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

UI Action to auto navigate to record producer passing values in URL - not working

CA7
Tera Contributor

I have a ui action that calls a record producer and passes a value to one of the variables and launches the record producer. Attempting to auto-populate a variable onLoad from variables/values available in the ritm/sc_task tables.

 

I am getting an error message when the record producer's onLoad Client script is called:

ReferenceError: RP is not defined
at presetFields (com.glideapp.service…sset_manager:865:15)
at onLoad_57060aeb87b40610739742e8cebb35bb (com.glideapp.service…asset_manager:870:1)
at com.glideapp.service…asset_manager:877:1
at js_includes_doctype.jsx:557:4
at runBeforeRender (js_includes_doctype.jsx:474:5)
at z_last_include.jsx:33:2
at z_last_include.jsx:39:3

-------------------------------

UI Action:

    var reqBy = 'asset_manager';
    var url = 'com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=1297d75ba1617010fa9b20d39126489f';
        url += '&sysparm_requested_by=' + reqBy;
    g_navigation.open(url, '_blank');
 
-----------------------
On the Record Producer (Asset Reclamation Request ... ham and sam)
... onLoad client script:
function presetFields() {
 var reqBy = RP.getParameterValue('requested_by');
 RP.setValue('requested_by', reqBy);
};
presetFields();
 
-------- 
note the variable: CA7_0-1710358718362.png

 

 

The url it generates is: /com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=1297d75ba1617010fa9b20d39126489f&sysparm_requested_by=asset_manager

 

Any help much appreciated.

2 REPLIES 2

Shruti
Mega Sage
Mega Sage

Hi,

Use this code in client script

function onLoad() {

    var reqBy = getParameterValue('sysparm_requested_by');
    
    g_form.setValue('requested_by', reqBy);
}

function getParameterValue(name) {
    var url = top.location.href;
    var value = new URLSearchParams(url).get(name);
    if (value) {
        return value;
    }
    if (!value) {
        var gUrl = new GlideURL();
        gUrl.setFromCurrent();
        value = gUrl.getParam("sysparm_id");
        return value;
    }
}

Amit Pandey
Kilo Sage

Hi @CA7 

 

Can you modify the client script and UI Action script as follows-

 

onLoad Client Script-

function presetFields() {
    // Use g_form.getParameter() to access URL parameters
    var reqBy = g_form.getParameter('sysparm_requested_by'); 
    // Check if the parameter exists and is not null
    if (reqBy) {
        // Use g_form.setValue() to set the field value
        g_form.setValue('requested_by', reqBy);
    }
}

// Call the function to preset fields when the form loads
presetFields();

UI Action-  

var reqBy = 'asset_manager';
var url = 'com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=1297d75ba1617010fa9b20d39126489f';
url += '&sysparm_requested_by=' + reqBy;
g_navigation.open(url, '_blank');