Jon G1
Kilo Sage

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!

Comments
Jon G1
Kilo Sage

UPDATE: @Chuck Tomasi Posted a great video recently on the ServiceNow Dev YouTube channel about a potentially MUCH easier way to do this: https://www.youtube.com/watch?v=spWJe-icus0

 

I had no idea about this before, but it seems that the fields are filled in on a new record from a related list based on the filter parameters on that related list!  I briefly mentioned leveraging the URL above, but I didn't realize that this is where ServiceNow was getting it's data.  You can manipulate this method to easily include the fields you want to copy over depending on your use case.  It's likely much easier than trying to do it via a client script so long as this filter method fits your needs.  Go check out the YouTube video for more details!

Keel
Tera Contributor

Thanks for this. It was helpful for a similar use case we had.

 

However, I just wanted to point out a small typo in your code.

 

This part:

if (!g_form.isNewRecord() || g_form.getValue('request_item' == '')) return;

Should be like this:

if (!g_form.isNewRecord() || g_form.getValue('request_item') == '') return;

 Now the script will properly cancel itself if the request_item field is empty.

 

Cheers!

Jon G1
Kilo Sage

Thanks for the update @Keel - you're absolutely right.  It doesn't look like I can edit the original post, but this is valuable information!

swatisolanki
Tera Contributor

Hi @Jon G1 

 

The video link you posted - https://www.youtube.com/watch?v=spWJe-icus0 - seems unavailable now.

Could you please help me with this. I am unable to do with script.

Jon G1
Kilo Sage

@swatisolankiI wonder if the video is region-locked, the link still works for me.

 

In the video, Chuck opens a Problem record, scrolls down to the Incidents related lists at the bottom, and highlights that the related list is just a pre-filtered list of incidents.  If you right click the filter breadcrumb in that related list, and open in a new window, you get exactly the filtered list of incidents that you would expect in a separate tab. Then, if you click the New button from that incidents list, the problem field will already be filled in on the new incident form.

 

So for example, you could have a list filtered to a specific short description such as "Test":
https://[yourinstance].service-now.com/incident_list.do?sysparm_query=short_description%3DTest&sysparm_view= 

You could take that same encoded query, but instead apply it to a new record. You'll want to specify sys_id=-1 in the URL so that SN knows you want a new record, then add the query. Make sure the query is URL encoded (%3D is the equal sign in this case). Your new URL would look something like this:

https://[yourinstance].service-now.com/incident.do?sys_id=-1&sysparm_query=short_description%3DTest

 

That link should pop open a new incident record with the short description already set to "Test". You can do that with pretty much any other field, so if you only need to pre-populate a few fields, the easiest way might just be to create a filtered list with your desired parameters, then copy the "sysparm_query=" part of the URL. And if you're comfortable enough with the process, you can use a script to create the URL including the query for you depending on your needs.

 

Hope this helps!

Version history
Last update:
‎01-18-2022 09:15 PM
Updated by: