Default Focus Field on Incident Form Load

rosa8
Mega Contributor

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();
}
}

14 REPLIES 14

I ended up using:



document.getElementById("IO:<variable sys_id here>").focus();


I'd suggest you put that line within a try...catch block like so:



try {


              document.getElementById("IO:<variable sys_id here>").focus();


      } catch(err) {};



That will protect you in case of an error.


Working like a charm, thanks Jim!


Good to hear, and you are welcome.


Hello Jim,


How this can be implemented in Service Portal?


I need to focus on a field on change of a   variable.



document.getElementById won't work in Service Portal