Plugin Pre-validation Operation to Show an Error Message as well as Log the Error

By | March 7, 2017


We had a business requirement, where we created a custom entity with a grid of Associated Accounts. Here, we wanted to restrict the Account while adding Accounts in the Associated View. We wanted to do this when the Account does not have the status reason ‘Approved’ by showing an error message and sending an email to the owner of the account


As we know, to show an error message using plugin, we have to use InvalidPluginExecutionException.

However, when we use InvalidPluginExecutionException it rollbacks any additional code executed on ‘Pre-operation’ and ‘Post-operation’ irrespective of it being ‘Synchronous’ or ‘Asynchronous’.

The need over here was not just to throw an error and notify the user but also send an email/log an error in the background.

Throwing an exception in the plugin registered on “Pre-Operation” or “Post-Operation” would also prevent the additional logging of the error message that was required.


We found an alternate way to achieve this by registering plugin on “Pre-Validation” operation.

Pre-validation operation allows processing the actions written, before throwing invalid plugin exception.

We added a new plugin step and registered it as seen in the below screenshot:

Plugin Pre-validation operation to Show an Error Message

We have also written the code mentioned below to process actions before throwing invalid plugin exception:

bool isApproved = false;
                if (context.MessageName.ToLower() == "associate")
                    //validate relationship
                    if (context.InputParameters.Contains("Relationship"))
                       string relationship = context.InputParameters["Relationship"].ToString();

                    //check relationship
                    if (relationship != "relationshipname")
//business logic execution

if (!isApproved)
			//send email code

			//throw error to prevent association

			throw new InvalidPluginExecutionException(" Account Status Reason needs to be Approved !!");	



Conclusion: Code in ‘Pre-validation’ is executed outside the transaction and therefore any updates made to the database in this stage persists and can therefore be used for error logging mechanism that needs to be implemented.

QuickBooks Integration with Dynamics 365/CRM – Get the Best of Both Worlds with InoLink!

3 thoughts on “Plugin Pre-validation Operation to Show an Error Message as well as Log the Error

  1. viswa

    Hello Inogic,
    How can i stop the record creation from the pre-validate plugin without throwing the Invalid plugin execution Exception ?

    Thanks in advance.

    1. Inogic Post author

      We are not sure about your requirement, but currently, there is no such a way to stop creating record using plugin without throwing exception.

      Either way is to write javascript On Save event of record and use preventDefault in script to restrict user from saving record.

      function My_PreventSaveFunction(eContext) {

      Hope this helps.



Leave a Reply

Your email address will not be published. Required fields are marked *