Recently faced an issue with the setFocus() function in CRM 2011 application. We were doing a validation in a field on change event and if the validation went wrong then we were supposed to show a informative message to the user and then set the focus to the same field.
We have tried below code to set the focus but failed.
Xrm.Page.getControl("fieldName").setFocus(true);
event.returnValue = false;
return;
Later when checked on the web got a blog which was talking about passing the context as a parameter and using the same as below
Xrm.Page.getControl(context.getEventSource().getName()).SetFocus(true);
Unfortunately the same didn't worked for me. I am still not sure what I did wrong in the above approach. You can check the comment section here for this approach.
Then in another blog we got another method which was achieving my requirement through an illusion of setting focus on my field after setting the focus to another field. So the code would look like this.
Xrm.Page.getControl("another_field").setFocus(true);
Xrm.Page.getControl("actual_field_to_focus").setFocus(true);
event.returnValue = false;
return;
This worked for me anyway. For your reference on the actual blog please check here.
We have tried below code to set the focus but failed.
Xrm.Page.getControl("fieldName").setFocus(true);
event.returnValue = false;
return;
Later when checked on the web got a blog which was talking about passing the context as a parameter and using the same as below
Xrm.Page.getControl(context.getEventSource().getName()).SetFocus(true);
Unfortunately the same didn't worked for me. I am still not sure what I did wrong in the above approach. You can check the comment section here for this approach.
Then in another blog we got another method which was achieving my requirement through an illusion of setting focus on my field after setting the focus to another field. So the code would look like this.
Xrm.Page.getControl("another_field").setFocus(true);
Xrm.Page.getControl("actual_field_to_focus").setFocus(true);
event.returnValue = false;
return;
This worked for me anyway. For your reference on the actual blog please check here.