Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send alert to user via plugin without using InvalidPluginExecutionException?

I am currently using InvalidPluginExecutionException to send the message to the user, but it turns out that the message is in English "Business Process Error" beyond which the error box appears the button "download log file". This is not an error because the user is trying to duplicate a record, as can be seen in the code. Is there other way without having to use InvalidPluginExecutionException to show an alert?

QueryExpression query1 = new QueryExpression();
query1.ColumnSet = new ColumnSet(true);
query1.EntityName = "new_appraisers";

EntityCollection ec = service.RetrieveMultiple(query1);

if (ec.Entities.Count <= 0)
{
    log.Tb_Log_Create("Appraiser created");
}
else
{
    foreach (Entity app in ec.Entities)
    {
        if (app["fcg_appraiser"].ToString() == name)
        {
            log.Tb_Log_Create("appraiser allready exist");

            throw new InvalidPluginExecutionException("The name allready exists");
        }

        if (app["new_login"].ToString() == login)
        {
            log.Tb_Log_Create("appraiser allready exist");

            throw new InvalidPluginExecutionException("The login allready exists.");
        }
    } 
}
like image 276
Hugo Silva Avatar asked May 16 '13 17:05

Hugo Silva


People also ask

What is Ipluginexecutioncontext in MS CRM?

Defines the contextual information passed to a plug-in at run-time. Contains information that describes the run-time environment that the plug-in is executing in, information related to the execution pipeline, and entity business information.

What is message in plugin registration tool?

Messages are the events on which the plugin (or business logic) is registered. For example, you can register a plugin on Create Message of Contact entity. This would fire the business logic whenever a new Contact record is created.

How will you show custom formatted synchronous plugin exception error to the user?

For synchronous plug-ins, you can optionally display a custom error message in the error dialog of the web application by having your plug-in throw an InvalidPluginExecutionException exception with the custom message string as the exception Message property value.


2 Answers

The only method to display a message box to the user from a plugin is using an exception from the validation stage. You could use javascript however, perform a simple OData query on the On_Save event of the form, and display an alert box with whatever information you'd like, and cancel the saving of the form.

This would allow you to display whatever custom message you'd like, and keep the plugin from firing and displaying the download file dialog.

like image 181
Daryl Avatar answered Oct 27 '22 00:10

Daryl


I may be little late, however, in more recent versions of CRM there are several possibilites to achieve what you want. The better ones beeing:

  1. Business Rules
  2. Validation using JS and notifying the user using
    • Form Notifications or
    • Control Notifications

I hope Microsoft doesn't read this but...

You can also use a synchronous Plugin and be happy with the Business Process Error Dialog popping off. I just found out that this Dialog is hackable to some degree. Just return HTML in the Exeptions Message like so:

throw new InvalidPluginExecutionException(
@"<img height='16px' src='http://emojione.com/wp-content/uploads/assets/emojis/1f644.svg'> <strong>Oh snap!</strong>

It seems the record can not be saved in its current state.    

");

Which results in sth. like this:

enter image description here

like image 20
nozzleman Avatar answered Oct 26 '22 22:10

nozzleman