Refresh a related list on field change

oharel
Kilo Sage

Hi guys,

Is there any way to refresh a related list when a field changes on the form?

For example: if I change the category on an incident form, I would like the related list category-articles to update, without saving the form first.

I tried an onChange client script with the following:

GlideList2.get(g_form.getTableName() + '.REL:31336814db772200e4257cbdae9619f4').setFilterAndRefresh('');

But I am not sure it can work without saving the form, which sort of makes this irrelevant.

Ideas?

harel

1 ACCEPTED SOLUTION

Mike Allen
Mega Sage

The wiki says that g_list is used in UI Context Menus and UI Actions:



GlideList2 (g list) - ServiceNow Wiki


View solution in original post

22 REPLIES 22

We're almost there. I successfully tested the GlideList2 using JavaScript Executor before posting it earlier not realizing the ID is dynamic - it takes on a new value every time the page is reloaded. So we'll have to locate the Embedded List using something that's static.



Below is a revised script, tested successfully in Client Script in Helsinki. Because GlideList2 doesn't provide a means to locate a list, we'll have to resort to using a DOM selector (tested successfully for both Embedded and Related Lists😞



function onChange(control, oldValue, newValue, isLoading, isTemplate) {  
  if (isLoading || newValue === '') return;



  var listLabel = 'incident lookup-KB';
  var list = $$('div.tabs2_list[tab_caption="' + listLabel + '"] .list_div')[0];   // locate the list
  if (list) GlideList2.get(list.id).setFilterAndRefresh('category=' + newValue);


}



I added the "if" condition to demonstrate how you can determine if the list was located. You may want to add additional error handling/logging as needed. As mentioned before, you'll need to modify the argument 'category=' + newValue to suit your needs.



I confirmed that every time you change Category, the list is refreshed based on the query without having to reload/save the form. One side effect I observed is that, because the query is persistent by design (it gets saved for the form even if the form itself is not saved), the list may get out of sync with Category if someone changes Category but not save the form. For example, if I change Category from Hardware to Network, the list now shows Network-related KB articles. But I decide not to save the form. Next time someone opens the form, it'll show Network-related articles even though Category = Hardware. One workaround is to comment out Line 2 so the list is refreshed on page load at the expense of added load time.



As you're already aware, working directly with DOM always carries the risk of being "brittle", that is, if SN changes the DOM, say, with a new UI release, the selector may no longer work, so please proceed with caution.


Thanks for the effort, John.


And for that reason (SN changing the DOM) I try my best not to use DOM manipulation.



harel


Hi harel/John,



I need to have filters/breadcrumbs on embedded list. I created one embedded list through Relationship. I have placed the embedded list on the form. However that list does not show filters and breadcrumbs.



Please let me know who to add them in embedded list.



Thanks,


Punit