Sending message from plugin to form not working for empty values

牧云@^-^@ 提交于 2019-12-25 07:47:03

问题


I'm sending a custom message from a plugin (that does some validation) back to the CRM form.

Here's my plugin code that executes on pre-Update and 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;

}

The simple method that sets the attributes' value...

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

And in my form, I have put this event handler on the onChange event of the 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);

};

This triggers fine when new_approval_status goes from null, empty to something. But it doesn't trigger on the other way around, a string to an empty string or null.

In my plugin, I've tried setting new_approval_status to string.Empty or null but the event doesn't trigger that way around.

Any ideas ?


回答1:


You could set your field to something like OK instead.

This also allows to guarantee your code works ("is the field empty because it's supposed to be, or something is missing and it isn't being filled ?")



来源:https://stackoverflow.com/questions/43259829/sending-message-from-plugin-to-form-not-working-for-empty-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!