Focus on tab in form view

Assuming you are working with ServiceNow or have employees/colleagues who use the system daily, then you might have experienced some frustration that you have to fill in mandatory fields on a section in a form view that is within a tab but the system does not focus on this tab.

In general, ServiceNow does not focus on a specific tab when you e.g. change the status or do something else. The system remembers the tab you selected the last time.

How to set the focus on a tab via Client Script?

Adjust the behavior of the ServiceNow form view and make it more user-friendly. Create a client script that will select the tab you want to highlight. As an example, we are using the incident form.

The default behavior of the ServiceNow Incident Management form is:

  • You change the status to “Resolved”
  • Resolution Informatin fields will get mandatory
  • The focus is on the current tab
  • You save the Incident form
  • Display an error message that mandatory fields are empty

The behavior we would like to achieve:

  • You change the status to “Resolved”
  • Resolution Informatin fields will get mandatory
  • The focused tab is the one containing the resolution information fields
  • You save the Incident form
  • You do not get an error message

Client Script Type: onChange
Field Name: State

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

    //Type appropriate comment here, and begin script below
    if ((g_form.getValue('state') == '6' || g_form.getValue('state') == '7') && g_form.getValue('close_notes') == '') {

        var tabIndex = g_tabs2Sections.findTabIndexByName('resolution_information');
        g_tabs2Sections.setActive(tabIndex);
    }

}

This script can be used for other situations as well and not only on the incident form. Important is how you use the [g_tabs2Sections.SetActive()]. This code requires as input an index of the tab: 0, 1, 2, etc. In my example I was not sure about the index, therefore I did a query to get the index of the tab I want to have “Resolution Information”. This also helps in case some other developer is adding another tab to the form.

To have the [g_tabs2Sections.findTabIndexByName()] work properly, add as value parameter the tab name in lower letters and for each blank add an underscore!

And now have fun with this code and make your users, especially Service Desk employees in Incident Management happy.

1 thought on “Focus on tab in form view”

  1. Great!
    it worked for me.
    citing the ServiceNow reference, you can get the name of the sections using the client method “setSectionDisplay(). In general, the following convention is used:

    The section name is lower case with an underscore replacing the first space in the name, and with the remaining spaces being removed, for example “Section Four is Here” becomes “section_fourishere”. Other non-alphanumeric characters, such as ampersand (&), are removed. Section names can be found by using the getSectionNames() method.

    Reply

Leave a Comment