简体   繁体   English

如何在 MS CRM 2015 的 OnChange 中将字段文本值大写?

[英]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.我对 CRM 开发还很陌生,我正在尝试自定义我的帐户表单以将 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.但是,当我更改测试帐户中的值时,它告诉我不支持 getValue() 方法。 Everything I've found tells me to use getValue().我发现的一切都告诉我使用 getValue()。 Im at a loss.我不知所措。

Any help would be appreciated.任何帮助,将不胜感激。 Thanks谢谢

If you're getting a getValue is not supported error, double check that the value for fieldName is actually a field on the form.如果您收到 getValue is not supported 错误,请仔细检查 fieldName 的值是否实际上是表单上的一个字段。 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.更新:当您通过表单编辑器将字段连接到 JS 函数时,CRM 会传递一个事件上下文作为第一个参数。 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以下是有关上下文的更多信息: https : //msdn.microsoft.com/en-us/library/gg328130.aspx

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.另请提供一张屏幕截图,显示您如何使用/注册此处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM