从插件发送消息到表单不适用于空值

时间:2017-04-06 15:42:36

标签: javascript c# dynamics-crm-2016

我正在从插件中发送自定义消息(进行一些验证)回到CRM表单。

这是我的插件代码,它在pre-Update和post-Create上执行:

//GetAccounts is a simple method to return accounts based in specified crtitias. 
//In Update event, it will add an extra filter to exclude the current account...
const string DupeFieldName = "new_approval_status";

if (xrmObjects.PluginContext.PrimaryEntityName == Xrm.Account.EntityLogicalName && xrmObjects.PluginContext.Depth == 1 && (xrmObjects.PluginContext.MessageName == "Update" || xrmObjects.PluginContext.MessageName == "Create"))
{
    Entity account;
    account = (Entity)xrmObjects.PluginContext.InputParameters["Target"];

    if (account.Attributes.Contains("name"))
    {
        if (GetAccounts(account, xrmObjects.PluginContext.MessageName, "name", account["name"], xrmObjects.Service).Entities.Count > 0)
        {
            SetDupeMessage(account, Name);
            return;
        }
    }

    if (account.Attributes.Contains("websiteurl"))
    {
        if (GetAccounts(account, xrmObjects.PluginContext.MessageName, "websiteurl", account["websiteurl"], xrmObjects.Service).Entities.Count > 0)
        {
            SetDupeMessage(account, WebSiteExist);
            return;
        }
    }

    if (account.Attributes.Contains("new_linkedin"))
    {
        if (GetAccounts(account, xrmObjects.PluginContext.MessageName, "new_linkedin", account["new_linkedin"], xrmObjects.Service).Entities.Count > 0)
        {
            SetDupeMessage(account, LinkedIn);
            return;
        }
    }


    account[DupeFieldName] = string.Empty;

}

设置属性值的简单方法......

private void SetDupeMessage(Entity account, string message)
{
    account[DupeFieldName] = message;
    account["new_approved"] = false;
}

在我的表单中,我已将此事件处理程序放在onChange的{​​{1}}事件中:

new_approval_status

function dupeDetected(context) { var dupeStatus = Xrm.Page.getAttribute('new_approval_status').getValue(); if (!dupeStatus || dupeStatus == '') { Notify.remove('duplicateWarning'); //Notify is a library that adds notification at form level... return; } var messageParts = dupeStatus.split('|'); var message = messageParts[1]; var fieldName = messageParts[0]; Notify.add(message, 'ERROR', 'duplicateWarning', null); }; 从null变为空时,这会触发正常。但它不会在另一方面触发,一个字符串为空字符串或null。

在我的插件中,我尝试将new_approval_status设置为new_approval_statusstring.Empty,但事件并未触发。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您可以将字段设置为类似OK的字段。

这也可以保证你的代码可以正常工作(#34;该字段是空的,因为它应该是,或者某些东西丢失而且它没有被填充?&#34;)< / p>