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 ?