Reload a Form or Related list from a Client Script

https://www.servicenowguru.com/scripting/client-scripts-scripting/reload-form-related-list-client-script/


his post comes in response to a forum question about how to reload a form or related list from a client script. This can come in extremely handy in the right situation. In the past, I’ve used scripts like these to refresh information on the form in response to a back-end server update of some sort. Here are a couple of code examples that show how to trigger these reload actions.


Reload a form

Reloading a form is simple, and it’s already built-in with the ‘Reload form’ context menu action.
Reload Form Action

The client script to reload a form looks like this…

reloadWindow(window);

Refresh a related list

Refreshing a related list is a bit more complex since you need to correctly identify the related list you wish to refresh. You can do that by right-clicking the related list header and selecting ‘Personalize->List Control’. The list control will show you the name of the related list that you need to use. In the screenshot below, the name of the ‘Affected CIs’ related list is ‘task_ci.task’.
List Control

Once you’ve identified the name of the related list, you can refresh it from any client-side script by using the following code (adding your list name where appropriate).

GlideList2.get(g_form.getTableName() + '.YOUR_RELATED_LIST_NAME_HERE').setFilterAndRefresh('');

So if you wanted to set up a client-side ‘Refresh’ UI action for the ‘Affected CIs’ related list, you could do so with the following setup. Notice the use of ‘task_ci.task’ to identify the related list in the ‘Script’ section.

‘Refresh’ related list UI action

Name: Refresh

Table: task_ci

Client: true

List banner button: true

Onclick: refreshAffectedCIs()

Condition: RP.isRelatedList()

Script:

function refreshAffectedCIs(){
   GlideList2.get(g_form.getTableName() + '.task_ci.task').setFilterAndRefresh('');   
}

Refresh an embedded related list

It’s also possible to refresh an embedded related list.

try {
      var elementLookup = $$('div.tabs2_list');
      var listName = "Affected CIs";
      for (i = 0; i != elementLookup.length; i++) {
         if (elementLookup[i].innerHTML.indexOf(listName) != -1) {
            var listHTML = elementLookup[i].id.split('_');
            var listID = listHTML[0];
            nowRefresh(listID);
         }
      }
}
catch (err) {
   //alert(err.message);
}
function nowRefresh(id) {
   GlideList2.get(id).refresh('');
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章