Default Focus Field on Incident Form Load
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-16-2012 01:07 PM
I have a request to set the default focus field on the Incident form to Additional Comments after the initial save. I'm thinking this should be an easy task but I'm not having any luck.
I was able to get it to partially work on an onChange Client script on the Clock No. field but it has some issues when creating a new incident. The code is as follows.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading && newValue == '') {
return;
}
if (newValue != ''){
var field = g_form.getControl('comments');
field.focus();
}
}
When I try to use the onLoad Client Script I can't get it to work at all. I get the alerts to pop up but the focus is still at the caller_id field.
function onLoad() {
var clock = g_form.getValue('caller_id');
alert(clock);
if(clock != ''){
alert('Running if statement.');
var field = g_form.getControl('comments');
field.focus();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-16-2012 02:37 PM
function onLoad() {
if(g_form.isNewRecord()) {
setTimeout("var refocus = document.getElementById('sys_display.incident.caller_id');refocus.focus();",0);
} else {
setTimeout("var refocus = document.getElementById('incident.comments');refocus.focus();",0);
}
}
It will set focus to the Caller ID field for a new Incident or the Additional Comments field when updating an existing one. It is running on demo13 at the moment.
Check out the following Wiki article: https://wiki.servicenow.com/index.php?title=Modifying_Form_Focus. The article states it does not work for reference fields, but then gives a working example of it for the caller_id field. The trick is to add the "sys_display." at the beginning. I've been using it for a long time now without any problems.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-20-2012 12:02 PM
Thanks for the info. I was able to get this to work with a little tweaking. The new syntax editor was giving me warning messages.
function onLoad() {
if(g_form.isNewRecord()){
return;
}
else {
window.setTimeout(cursor,0);
}
}
function cursor(){
var refocus = document.getElementById('incident.comments');
refocus.focus();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2015 07:59 AM
Jim do you know if something similar would work with an onChange script. I was hoping to be able to change the focus to 'close notes' after changing the Incident State (incident_state) to 'Closed Mail' which has a value of 8?
TIA
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2015 01:33 PM
This code, in an onChange script, should do the trick for you:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == oldValue || newValue != 😎 {
return;
}
window.setTimeout(u_highlightCloseNotes,0);
}
function u_highlightCloseNotes() {
try {
g_tabs2Sections.setActive(4);
document.getElementById('incident.close_notes').focus();
} catch(err) {};
}
You might have to change the "4" in line 10 to another number, depending on which section the Close notes field is on. It sets the fifth tab (numbering starts at 0) as the active one first. Or, you may not even need it at all if the field is on the main header "section".