Carlos Farmer
Carlos Farmer

Reputation: 67

How can I capitalize field text values at OnChange in MS CRM 2015?

I'm fairly new to CRM development and I'm trying to customize my account form to Capitalize any text field at onChange. I'm currently working with this function that I found online:

function UpperCaseField(fieldName)
{
var value = Xrm.Page.getAttribute(fieldName).getValue();
if (value != null)
{
   Xrm.page,getAttribute(fieldName).setValue(value.toUpperCase());
}
}

However, when I change a value in my test account it tells me that the method getValue() is not supported. Everything I've found tells me to use getValue(). Im at a loss.

Any help would be appreciated. Thanks

Upvotes: 2

Views: 1341

Answers (2)

Polshgiant
Polshgiant

Reputation: 3664

If you're getting a getValue is not supported error, double check that the value for fieldName is actually a field on the form. It's best to code more defensively, like this:

function UpperCaseField(fieldName)
{
    var attr = Xrm.Page.getAttribute(fieldName);
    if (!attr) { 
        console.log(fieldName + " not found"); 
        return; 
    }

    var value = attr.getValue();
    if (value != null)
    {
       attr.setValue(value.toUpperCase());
    }
}

Update: When you connect your fields to JS functions via the form editor, CRM passes an event context as the first parameter. Here's what the code would look like in that case:

function UpperCaseField(context)
{
    var fieldName == context.getEventSource().getName();
    var attr = Xrm.Page.getAttribute(fieldName);
    if (!attr) { 
        console.log(fieldName + " not found"); 
        return; 
    }

    var value = attr.getValue();
    if (value != null)
    {
       attr.setValue(value.toUpperCase());
    }
}

Here's more info about the context: https://msdn.microsoft.com/en-us/library/gg328130.aspx

Upvotes: 1

Andrew Butenko
Andrew Butenko

Reputation: 5446

Replace line

Xrm.page,getAttribute(fieldName).setValue(value.toUpperCase());

with line

Xrm.Page.getAttribute(fieldName).setValue(value.toUpperCase());

Also please provide a screenshot that shows how you use/register this handler.

Upvotes: 0

Related Questions