Populate Fields When Clicking New in Related List - Client Script
About two years ago I wrote an article on this topic when I was relatively new to ServiceNow. I couldn't find a concise answer on how to automatically fill in values when I click the new button in a related list. Looking back at my old solution, it was pretty ugly. And since I was looking at it, it seemed like a great time to revisit the script and give the process some attention.
There are lots of ways you could do this. One of which might be redefining the New button UI Action to send some info in the URL. Doing it that way would probably be a smoother experience for the end user if you only need to fill in a couple of fields at the cost of modifying an out-of-box UI Action.
The simplest choice is probably to use a client script. So long as there is a reference field that is automatically filled in when you click "New" on your related list, you can get the information from that reference and populate your fields. Here's an example on Catalog Task (sc_task) to pull data from a Requested Item (sc_req_item):
function onLoad() {
if (!g_form.isNewRecord() || g_form.getValue('request_item' == '')) return;
var ritm = g_form.getReference('request_item', ritmCallback);
function ritmCallback(gr) {
if (gr) {
var passFields = [
{from: 'company', to: 'company'},
{from: 'task_for', to: 'task_for'},
{from: 'location', to: 'location'},
{from: 'configuration_item', to: 'cmdb_ci'},
{from: 'assignment_group', to: 'assignment_group'},
{from: 'assigned_to', to: 'assigned_to'},
{from: 'priority', to: 'priority'},
];
for (var i = 0; i < passFields.length; i++) {
var passField = passFields[i];
if (g_form.hasField(passField.to)) {
g_form.setValue(passField.to, gr.getValue(passField.from));
}
}
}
}
}
When the form loads, we check to make sure it is a new record and that our reference field is filled in. If it is, we pull that record using getReference and pass the record to the callback script. Using a callback script is important as it allows this script to process asynchronously.
We determine which fields we want to copy in the "passFields" array of objects. In each object, we declare which field we want to get data from on the parent record, and which field we want to populate with that information on the new child record. Note how the field for configuration item is different on SCTASK than it is RITM.
Then we just loop through each of the fields we want and populate the values. The user will see the values populate one-by-one as the script iterates through each field which may not be the desired experience.
One other solution that you could try would be to develop a custom AJAX script and use GlideAjax to bring your data in. This might have a slight performance benefit as your GlideAjax can be configured to return only the fields that you need instead of returning an entire gliderecord.
Another solution is to use the scratchpad. Create an onDisplay business rule that stores the data you'll want in the scratchpad then use a client script to populate the form with that data. That should also theoretically be slightly faster, but I didn't see any significant difference in load times when I tested that option.
I see a lot of questions about this floating around the community, but I didn't see an article or straightforward solution in the questions. Hopefully this saves somebody a headache or two!
https://www.servicenow.com/community/now-platform-articles/populate-fields-when-clicking-new-in-related-list-client-script/ta-p/2307579