Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SetFocus not sticking in CRM 4.0

146 views
Skip to first unread message

Nick Doelman

unread,
Feb 25, 2008, 7:00:03 PM2/25/08
to
The following code works in CRM 3.0 but not CRM 4.0, specifically the
"setfocus" does not keep the cursor in the field, it hops to the next field.

Any one run into this and ideas?

var oField = event.srcElement;
var phonedigits;

phonedigits = oField.DataValue.length;

if (phonedigits < 10)
{
alert("Phone number must have at least 10 digits")
oField.SetFocus();
return = false;
}

--
Nick Doelman
www.readybms.com

Ranjitsingh Raghuvanshi

unread,
Feb 26, 2008, 2:04:01 AM2/26/08
to
Hi Nick,

//Use the below code to setFocus the Field
// Set focus to the field

crmForm.all.SOME_FIELD_ID.SetFocus();

--
Please mark this post as helpful, if it resolves your query.


Thanks & Regards,
Ranjitsingh Raghuvanshi

MS Small Business CRM Certified Professional.
http://mscrm-developer.blogspot.com/

Nick Doelman

unread,
Feb 26, 2008, 8:24:00 AM2/26/08
to
Thanks for the reply, but unfortunately the answer is not that obvious.

My code was designed for portability, that is why I used the variable for
the field, however that is not the issue.

Replacing my code with this:

if (crmForm.all.telephone1.DataValue.length < 10)

{
alert("Phone number must have at least 10 digits")

crmForm.all.telephone1.SetFocus();
return false;
}


Does the following in CRM 3.0:
-alerts the user that the phone number they are entering needs to be at
least 10 digits
-sets the focus *back* to the telephone1 field
-stops cursor from moving to the next field, cursor remains in telephone1
field

Does the following in CRM 4.0:
-alerts the user that the phone number they are entering needs to be at
least 10 digits
-sets the focus *back* to the telephone1 field
-the cursor move to the next field anyway, cursor is now in the next field
on the form in the tab order

The setFocus() is working, but in CRM 4.0 the action of tabbing to next
field is not cancelled.

It appears that CRM 4.0 is interpreting jscript code slightly differently in
certain cases (e.g. "eventReturn = false" no longer works in onchange events)

Ultimately what I want is if the user does not enter correct data, leave the
focus in the field until they do.

Thanks!
Nick

--
Nick Doelman
www.readybms.com

Ian Oldbury

unread,
Mar 13, 2008, 10:26:00 AM3/13/08
to
Hi Nick,

i've found exactly the same problem that you're experiencing, have you found
any answers?

thanks
ian

Nick Doelman

unread,
Mar 13, 2008, 10:33:01 AM3/13/08
to
Hi Ian

Unfortunately not, I ended up putting the verfication logic in the "onsave"
event that did put the focus back to that field, but I would have rather let
the user fix right then and there and had the focus reset immediately.

I wish I had the answer!

Nick
--
Nick Doelman
www.readybms.com

Ian Oldbury

unread,
Mar 13, 2008, 10:50:06 AM3/13/08
to
cheers for your update, if/ when the answer appears i'll let you know.

ian

larrytrimble

unread,
Sep 24, 2009, 1:24:05 PM9/24/09
to
Was frustrated that I couldn't find an answer for this issue so I started testing things to evaluate the CRM behaviour. I believe I have a solution...it's working for me anyway.

I'm also working on validation of a phone number field and don't want the user to be able to proceed unless the field is blank, or it follows my validation rules.

This is my full solution for validating a 10 or 7 character phone number. Original source was obtained from elsewhere on the net...just added the extra SetFocus line as shown...

// Grab the value from the field that generated the onChange event
var oField = event.srcElement;

// We're cool if the field is empty, otherwise validate it
if (oField.DataValue != "undefined" && oField.DataValue != null)
{
// Strip non-numerics
var sTmp = oField.DataValue.replace(/[^0-9]/g, "");

// Count the digits in the string...we need 10 to mask the phone number properly
switch (sTmp.length)
{
case 10:
oField.DataValue = "(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-"
+ sTmp.substr(6, 4);
break;

// Could easily validate 7 digit numbers as well
// case 7:
// oField.DataValue = sTmp.substr(0, 3) + "-" + sTmp.substr(3, 4);
// break;

// No match on 10 or 7 digit numbers...throw an error.
default:
alert("Phone number must contain exactly 10 digits");
// the next two lines are a workaround for CRM which doesn't set the Focus properly.
// First set focus to some other field on the form...we chose firstname here
// Then, set focus to the form field that began this onChange event
// declared oField
crmForm.all.firstname.SetFocus();
oField.SetFocus();
break;
}
}


Very weird, but the result is what I wanted. The focus REMAINS on the fidle that started the onChange, with all the text preselected.


Hope it works for you..

JB


IanOldbur wrote:

RE: SetFocus not sticking in CRM 4.0
13-Mar-08

cheers for your update, if/ when the answer appears i'll let you know.

ian

"Nick Doelman" wrote:

EggHeadCafe - Software Developer Portal of Choice
WPF And The Model View View Model Pattern
http://www.eggheadcafe.com/tutorials/aspnet/ec832ac7-6e4c-4ea8-81ab-7374d3da3425/wpf-and-the-model-view-vi.aspx

larrytrimble

unread,
Sep 24, 2009, 1:33:05 PM9/24/09
to
Sorry for the odd spacing in my post...No idea what caused that.

Larry Trimble wrote:

SetFocus not sticking in CRM 4.0 - Workaround
24-Sep-09

JB

EggHeadCafe - Software Developer Portal of Choice

Nicolas Pagès

unread,
Nov 2, 2009, 6:40:01 AM11/2/09
to
Hi,

Here's a simple workaround that works fine for me :

var oField = crmForm.all.crm_myfield;
oField.onblur = function() {
oField.SetFocus();
oField.onblur = null;
}
oField.SetFocus();

The onblur event is triggered when an element loses the focus.
So, crmForm loads, I call SetFocus, the field receives the focus and after
one milisec loses it. So the bug has been executed. Then, onblur is called,
and the field gets the focus again. To allow the user to click on other
field, remove the onblur event.

It works at my customer!

--
Nicolas Pagès

0 new messages