how do i populate values for new form based on previous dropdown selection using client state
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2025 07:49 AM - edited 04-27-2025 07:50 AM
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.
- Labels:
-
Agent Workspace
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2025 08:08 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2025 08:18 AM
this is the button clicked on workspace, which opens the next modal which is,
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2025 12:53 PM - edited 04-27-2025 09:22 PM
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