{"id":4650,"date":"2017-03-07T16:44:04","date_gmt":"2017-03-07T11:14:04","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=4650"},"modified":"2022-09-02T15:34:56","modified_gmt":"2022-09-02T10:04:56","slug":"plugin-pre-validation-operation-to-show-an-error-message-as-well-as-log-the-error","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2017\/03\/plugin-pre-validation-operation-to-show-an-error-message-as-well-as-log-the-error\/","title":{"rendered":"Plugin Pre-validation Operation to Show an Error Message as well as Log the Error"},"content":{"rendered":"<p style=\"text-align: justify;\"><strong>Introduction:<\/strong><\/p>\n<p style=\"text-align: justify;\">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 \u2018Approved\u2019 by showing an error message and sending an email to the owner of the account<\/p>\n<p style=\"text-align: justify;\"><strong>Issue?<\/strong><\/p>\n<p style=\"text-align: justify;\">As we know, to show an error message using plugin, we have to use InvalidPluginExecutionException.<\/p>\n<p style=\"text-align: justify;\">However, when we use InvalidPluginExecutionException it rollbacks any additional code executed on \u2018Pre-operation\u2019 and \u2018Post-operation\u2019 irrespective of it being \u2018Synchronous\u2019 or \u2018Asynchronous\u2019.<\/p>\n<p style=\"text-align: justify;\">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.<\/p>\n<p style=\"text-align: justify;\">Throwing an exception in the plugin registered on \u201cPre-Operation\u201d or \u201cPost-Operation\u201d would also prevent the additional logging of the error message that was required.<\/p>\n<p style=\"text-align: justify;\"><strong>Solution:<\/strong><\/p>\n<p style=\"text-align: justify;\">We found an alternate way to achieve this by registering plugin on \u201cPre-Validation\u201d operation.<\/p>\n<p style=\"text-align: justify;\">Pre-validation operation allows processing the actions written, before throwing invalid plugin exception.<\/p>\n<p style=\"text-align: justify;\">We added a new plugin step and registered it as seen in the below screenshot:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter  wp-image-4652\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2017\/03\/Plugin-Pre-validation-operation-to-Show-an-Error-Message-as-well-as-log-the-error.jpg\" alt=\"Plugin Pre-validation operation to Show an Error Message\" width=\"813\" height=\"513\" \/><\/p>\n<p>We have also written the code mentioned below to process actions before throwing invalid plugin exception:<\/p>\n<pre class=\"lang:default decode:true \">try{\r\nbool isApproved = false;\r\n                if (context.MessageName.ToLower() == \"associate\")\r\n                {\r\n                    \/\/validate relationship\r\n                    if (context.InputParameters.Contains(\"Relationship\"))\r\n                    {\r\n                       string relationship = context.InputParameters[\"Relationship\"].ToString();\r\n\r\n                    }\r\n                    \/\/check relationship\r\n                    if (relationship != \"relationshipname\")\r\n                    {\r\n                        return;\r\n                    }\r\n                    \r\n\/\/business logic execution\r\n\r\nif (!isApproved)\r\n                        {\r\n\t\t\t\/\/send email code\r\n\r\n\t\t\t\/\/throw error to prevent association\r\n\r\n\t\t\tthrow new InvalidPluginExecutionException(\" Account Status Reason needs to be Approved !!\");\t\r\n                 }\r\n\r\n                    }\r\n                }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><strong>Conclusion: <\/strong>Code in \u2018Pre-validation\u2019 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.<\/p>\n<h2 style=\"text-align: left;\"><div class=\"su-heading su-heading-style-default su-heading-align-center\" id=\"\" style=\"font-size:15px;margin-bottom:5px\"><div class=\"su-heading-inner\">Generate Your Own New Leads Within Microsoft Dynamics 365 CRM<\/div><\/div><\/h2>\n<p><em>Contact us for a <a href=\"https:\/\/www.maplytics.com\/maplytics-download\/?utm_source=highvisits&amp;utm_medium=technicalblog&amp;utm_campaign=hMaplytics\" target=\"_blank\" rel=\"noopener\">demo<\/a> to know more about how <a href=\"https:\/\/www.maplytics.com\/?utm_source=highvisits&amp;utm_medium=technicalblog&amp;utm_campaign=hMaplytics\" target=\"_blank\" rel=\"noopener\">Maplytics<\/a> can help you to generate new leads from within Microsoft Dynamics 365 CRM.<\/em><\/p>\n<p><em><a href=\"https:\/\/www.maplytics.com\/?utm_source=highvisits&amp;utm_medium=technicalblog&amp;utm_campaign=hMaplytics\" target=\"_blank\" rel=\"noopener\">Maplytics<\/a> is a 5-star rated, preferred business app on the <a href=\"https:\/\/appsource.microsoft.com\/en-us\/product\/dynamics-365\/inogic.f6f3c73f-29de-4fa8-a396-87ea8a07b6c4?tab=Overview\" target=\"_blank\" rel=\"noopener\">Microsoft AppSource<\/a> that is Certified for Microsoft Dynamics 365 (CfMD) and comes with powerful features like Appointment Planning, Sales Routing, Territory Management, Heat Maps, Geo-analytical Dashboards and more that empower organizations to add more value to their CRM data, improve sales &amp; service processes, and achieve high ROI.<\/em><\/p>\n<p><em>Get your <a href=\"https:\/\/www.maplytics.com\/maplytics-download\/?utm_source=highvisits&amp;utm_medium=technicalblog&amp;utm_campaign=hMaplytics\" target=\"_blank\" rel=\"noopener\">free trial<\/a> from our Website or <a href=\"https:\/\/appsource.microsoft.com\/en-us\/product\/dynamics-365\/inogic.f6f3c73f-29de-4fa8-a396-87ea8a07b6c4?tab=Overview\" target=\"_blank\" rel=\"noopener\">Microsoft AppSource<\/a>!<\/em><\/p>\n<p><em>&#8216;If data is the new oil, location intelligence is ??\u201d<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 \u2018Approved\u2019 by showing an error message and sending an\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2017\/03\/plugin-pre-validation-operation-to-show-an-error-message-as-well-as-log-the-error\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":4659,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[16,19,24,42],"tags":[1326],"class_list":["post-4650","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dynamics-365","category-dynamics-crm","category-dynamics-crm-2016","category-plugin","tag-plugin-pre-validation-operation"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/4650","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=4650"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/4650\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/4659"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=4650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=4650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=4650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}