Ui action redirect to Catalog Item form and populate the fields

KrutikaH
Tera Contributor

Hello ServiceNow community developers,

 

 Scenario: We have one table called Pipeline request under that when we click on Ui action (travel plan) so it will redirect to the catalog item page(Create Travel Plan Fulfiller Only) and auto populate the fields of pipeline request table as we have 4 fields common in both the table so we have to auto populate and able to Submit the catalog Item form.

 

I create one UI action So redirect to catalog form is working with this script but auto populate of fields and submittion is not working.

 

UI action Script:

var catalogItemName = "Fulfiller Only - Travel Request";
var catalogItemSysId = "";
// Retrieve Catalog Item Sys ID
var gr = new GlideRecord('sc_cat_item');
gr.addQuery('name', catalogItemName);
gr.query();

if (gr.next()) {
    catalogItemSysId = gr.getValue('sys_id');
}
// Get field values from the current form
var travelerEID = current.getValue("u_traveler_eid");
var homeCountryLocation = current.getValue("u_home_country_location");
var travelAssignmentCategory = current.getValue("u_travel_assignment_category");
var chargeCode = current.getValue("u_charge_code");

gs.addInfoMessage("my msg value " + travelerEID + " === " + homeCountryLocation + "  =====  " + travelAssignmentCategory + "  ==  " + chargeCode);
// Construct the URL for redirection
var catalogURL = "/com.glideapp.servicecatalog_cat_item_view.do?v=1&sysparm_id="+ catalogItemSysId;

catalogURL += "&sysparm_traveler_eid=" + encodeURIComponent(travelerEID);
catalogURL += "&sysparm_home_country_location=" + encodeURIComponent(homeCountryLocation);
catalogURL += "&sysparm_travel_assignment_category=" + encodeURIComponent(travelAssignmentCategory);
catalogURL += "&sysparm_charge_code=" + encodeURIComponent(chargeCode); // Passing charge code

// Redirect the user
gs.addInfoMessage("Redirecting to Create Travel Plan...");
action.setRedirectURL(catalogURL);
 
 
 
 
 
Thanks for your help
krutika
 

 

2 REPLIES 2

Shruti
Mega Sage
Mega Sage

Hi 

Write a onLoad client script to autopopulate the values

 

function onLoad() {

    var travelerEID = getParameterValue('sysparm_traveler_eid');
    var homeCountryLocation = getParameterValue('sysparm_home_country_location');
    var travelAssignmentCategory = getParameterValue('sysparm_travel_assignment_category');
    var chargeCode = getParameterValue('sysparm_charge_code');

    g_form.setValue('<variable_name>', travelerEID); // replace variable name
    g_form.setValue('<variable_name>', homeCountryLocation); // replace variable name
    g_form.setValue('<variable_name>', travelAssignmentCategory); // replace variable name
    g_form.setValue('<variable_name>', chargeCode); // replace variable name

}

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;
    }
}

Ankur Bawiskar
Tera Patron
Tera Patron

@KrutikaH 

for auto populating you should write onLoad catalog client script which gets the value from URL and set it

refer below links and they should help you

https://www.servicenow.com/community/virtual-agent-forum/autopopulate-variable-value-using-ui-action...

https://www.servicenow.com/community/developer-forum/redirect-from-a-ui-action-to-a-catalog-item-and...

https://www.servicenow.com/community/itsm-forum/how-to-populate-incident-number-in-service-catalog-v...

If my response helped please mark it correct and close the thread so that it benefits future readers.

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