The CreatorCon Call for Content is officially open! Get started here.

How do I change the form or tab focus after a field changes?

russellj03
Giga Contributor

The system I work on displays a closure section when incidents are marked as closed, which is done with a drop-down menu vs. a UI action.   The section is off the screen, however, and it is confusing because users don't know it is there until they hit update and get an error popup.   I am trying to change the focus with a UI Policy or onChange Client Script, but nothing has worked so far.

In my Client Script, I know I have the field change detecting correctly because I have an alert popping up.   I have tried using the g_tabs2Sections API:

1) by putting in the section numbers directly (.setActive(2))

2) by looking up the section ID in the source and finding the index of that sysID (.findTabIndexByID(sysID)), which was returning 1.

Neither section number is setting the focus.   What am I doing wrong?

Here is the main part of the script:

var id = g_tabs2Sections.findTabIndexByID('section_tab.12345678...');

alert('id = ' + id);

g_tabs2Sections.setActive(id);

1 ACCEPTED SOLUTION

Hi Justin,



I don't know what's causing the issue, but you could try wrapping that part in a timeout:


setTimeout(function() {g_form.getElement('incident.close_notes').focus();},0);



That way it executes after the client script finishes- perhaps the element has to hold focus for the duration of the script execution. That sounds plausible.


View solution in original post

17 REPLIES 17

venkatiyer1
Giga Guru

Hi Justin,



Can you please attach a screenshot of the page or give us some more detail as the above code should work correctly. Will need it to fix the issue? Just to ensure that there is no mismatch i even tried using your code on the form sections and it worked. So either it is not a tab section or there should be some other issue.



Regards,


Venkat


Hi Venkat,



Sorry, my instance is self-hosted and I can't provide screenshots.



The section I'm looking for is called "Closure Information".   When I look at the source, I find a span tag with an ID in it.   Putting that into the findTabIndexByID function returns a value of 1.   But regardless of what value I put in the setActive() function, nothing happens.



Thanks,


Justin


coryseering
ServiceNow Employee
ServiceNow Employee

Hi Justin,



Passing in the index of the tab is working perfectly in my local testing.


Are you getting any errors in your JavaScript console, when you try it?



What if you open the form, open your JS console, and run g_tabs2Sections.setActive(1); directly in the command prompt- did the tab change from the first one to the second one? Did you get an error?


Hi Cory,



When I open the JS console and update the ticket, no errors are showing up.   When I run g_tabs2Sections.setActive(1); at the command prompt, I get an error that g_tabs2Sections is not defined.   I get the same error when I run the command that was successful in the Client Script:   g_tabs2Sections.findTabIndexByID('section_tab.1234...');



Thanks,


Justin