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

Sending message from plugin to form not working for empty values

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

这是我在预更新和 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;
}

在我的表单中,我将此事件处理程序放在 new_approval_statusonChange 事件上:

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);

};

new_approval_status 从 null、空变为某物时,这会很好地触发。但是它不会反过来触发,一个字符串到一个空字符串或null。

在我的插件中,我尝试将 new_approval_status 设置为 string.Emptynull,但事件不会以这种方式触发。

有什么想法吗?

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

这也可以保证您的代码正常工作 ("is the field empty because it's supposed to be, or something is missing and it isn't being filled ?")