how do i populate values for new form based on previous dropdown selection using client state

aryan_seth
Tera Contributor

In the Workspace,
I have a submit button,  when clicked  it is redirecting to a new form , but on the new form I want 3 fields to be pre populated based on the 3 dropdowns selected before clicking the submit button.

aryan_seth_1-1745765212751.pngaryan_seth_2-1745765261952.png

 

 

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@aryan_seth 

which is the first form? what's the 2nd form? how are you redirecting to 2nd form after 1st form submission? there only you can add the code to pre-populate

in the 2nd table are you populating any reference field which holds record of 1st form?

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

aryan_seth_0-1745766707427.png

this is the button clicked on workspace, which opens the next modal which is,

aryan_seth_1-1745766768573.png

here i have 3 fields from cmdb_ci which is dropdown, and when the form opens(depending on the category) for eg category incident opens incident new form,
there are these 3 fields should be pre populated on the prevoius selection

 

VikMach
Mega Sage

Hi @Aryan,
First step - On your submit button that is redirecting to new form, I believe there is already a script written to redirect to new form on new browser window. You need to append that URL like example below.
For example you want to redirect user to new Incident form with pre-populated values. Create a URL with those values as URL parameters. The URL will capture those values and when new form loads the browser will have URL with passed in parameters.

var assetCat = g_form.getValue("asset_category");
var assetType = g_form.getValue("asset_type");
var assetSubType = g_form.getValue("asset_sub_type");
var formUrl = "/incident.do?&sysparm_asset_category=" + assetCat + "&sysparm_asset_type=" + assetType + "&sysparm_asset_sub_type=" + assetSubType;


Now, second step. Create one onLoad client script for that form which will fetch the URL parameters and pre-populate in the new form when the new form loads.

Example script will look like below. Adjust/modify the script as per your needs. 

function onLoad() {

// Use getParameterValue() function to get and set the values on the new opened form.

    var asset_cat = getParameterValue('sysparm_asset_category');
    var asset_type = getParameterValue('sysparm_asset_type');
    var asset_subType = getParameterValue('sysparm_asset_sub_type');

// You can directly insert the value in the new fields or create some condition if you have before setting the new field values from URL Parameter.

g_form.setValue('asset_category', asset_cat); // Replcae asset_category with original field name on your instance
g_form.setValue('asset_type', asset_type); // Replcae asset_type with original field name on your instance
g_form.setValue('asset_subType', asset_subType); // Replcae asset_subType with original field name on your instance

}

// USE THIS FUNCTION AS IS. THIS FUNCTION FETCHES THE URL PARAMETERS FROM YOUR CURRENT BROWSER WINDOW.

function getParameterValue(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(top.location);
    if (results == null) {
        return "";
    } else {
        return decodeURI(results[1]);
    }
}


Hope this helps.
Let me know if this was helpful.


PS - You might have to adjust the script as per your requirement to make it work. It is just an illustration on how to achieve it. We have similar functionality in our instance and it works like a charm. Don't alter "getParameterValue()" function.

About the "getParameterValue()" function, read more by Brad Tilton at - https://www.servicenow.com/community/developer-blog/3-ways-to-populate-values-in-servicenow-via-the-...



Regards,
Vikas K