Get selected items from related list.

Ivar Donado
Mega Sage

Hi guys,

I got a question, there's one coworker who's trying to add some extra functionality to the "Create Change" button on the Vulnerability group form. In that form there's a related list of "vulnerable items" and he's trying to somehow grab what records are selected on that list through a script. I've been researching for a while but have only found information about using g_list.getChecked(), which only works directly on the list. I don't really think there's a way to grab that information as a related list is more of a "view" of an existing list with it's own query.

If you have any suggestions, I'll be thankful to see them ^^

1 ACCEPTED SOLUTION

Ivar Donado
Mega Sage

We ended up finding a solution. It consisted in capturing the related list in an object so we could use GetChecked() on it by using getByName([related-list-name]).

 

var list = GlideList2.getByName("relatedListName");

var itemArray = list.getChecked();

 

itemArray would contain sys_ids of the selected items.

View solution in original post

5 REPLIES 5

Brad Tilton
ServiceNow Employee
ServiceNow Employee

So the behavior would be that someone would scroll down to the list, check a bunch of items, then scroll back up and use the Create Change button? I think you're right that it would be difficult to get the checked items on the related list from the change button. You could instead run a ui action on that list that could access the checked boxes directly, or create some sort of popup when they hit create change where they can select the vulnerable items.

Ivar Donado
Mega Sage

We ended up finding a solution. It consisted in capturing the related list in an object so we could use GetChecked() on it by using getByName([related-list-name]).

 

var list = GlideList2.getByName("relatedListName");

var itemArray = list.getChecked();

 

itemArray would contain sys_ids of the selected items.

Hi Ivar,

I have tried to get the selected records from a related list using the following script:

function test() {
    alert(g_form.getRelatedListNames());
    try {
        var list = GlideList2.getByName('REL:95ba1bf51bf0bc10fccfa9f6624bcbcb_list');
        alert(list);
        var itemArray = list.getChecked();
        alert(JSON.stringify(itemArray));
    } catch (ex) {
        alert(ex);
    }
}

 

But the list is shown as null and I am getting the TypeError: Cannot read property 'getChecked' from null.
And I am also having the doubt about the "relatedListName" used in this. Am I using the correct name.?

If yes, then will you be able to help me on this.?

Thanks,
Kaushal

Hi Kaushal,

 

Use the below code:

var selected = g_list.getChecked().split(',');
alert(selected);
 
It will return you the selected records.