Quantcast
Channel: Sam's Activities
Viewing all articles
Browse latest Browse all 3363

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

$
0
0

Introduction:

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

Issue?

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.

Solution:

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:

try{
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")
                    {
                        return;
                    }

//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!


Viewing all articles
Browse latest Browse all 3363

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>