web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Pre Validation Plugin Code.

(0) ShareShare
ReportReport
Posted on by

Hello Experts,

Can you explain what scenarios pre validation plugin to create.

If any one have sample code please shares that will help.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Gopalan Bhuvanesh Profile Picture
    11,401 on at
  • Suggested answer
    ashlega Profile Picture
    34,477 on at

    To me, the most important part, from the practical standpoint, is that pre-validation does not run in the same transaction. 

    For example.. If you had a "post create" plugin and if failed, any changes you made in the "pre-create" or "post create" by that moment would be rolled back. That's not so for "pre validate", though. In that sense, if you do anything there, the changes are "permanent".

    Sometimes, that actually affects the results of the SDK calls. For example, you know how you can create IOrganizationService for different users? In the latest version, it seems SetActiveProcess calls can be used for different users only if you do it in the PreValidate. Otherwise, it always applies to the user for whom the plugins started. That's probably a rare scenario, but it's good to keep in mind.

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi Jamal,

    Plugins registered in pre-validation stage may execute outside the database transaction. Registered plugin run before the form is validated. It is useful if you want to implement business logic before the actual validation starts.  i.e., Changes made in plugin won’t be saved if the validation of the main system plugins complain because the changes are outside the database transaction. 

    For example - Deletion cascades happen prior to pre-operation, therefore if you need any information about the child records, the delete plugin must be pre-validation.

    You could try some plugin code and register the step on pre-validation.

  • FidelMartin Profile Picture
    128 on at
    Hello,
     

    By using Plugin Registration’s Pre-Validation stage, we can validate or modify before the main operation occurs. Pre-validation stage will occur before the main operation executes.

    Here, we have given one example of how we can use the Pre-validation stage in plugin registration tools.

    For Example, we have given the Pre-validation on the Account entity, so whenever any account record is updated, it will update the Fax number as shown below.

    Follow the below steps :

    Step 1: Add the below code in your code editor for the plugin.

    using System; using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xrm.Sdk;
    namespace Plugindemo
    {
    public class Prevlidationclass : IPlugin
    {
    public void Execute(IServiceProvider serviceProvider)
    {
    ITracingService tracingService =
    (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    IPluginExecutionContext context = (IPluginExecutionContext)
    serviceProvider.GetService(typeof(IPluginExecutionContext));
    if (context.InputParameters.Contains("Target") &&
    context.InputParameters["Target"] is Entity)
    {
    Entity entity = (Entity)context.InputParameters["Target"];
    try
    {
    if (entity.LogicalName == "account")
    {
    if (entity.Attributes.Contains("fax") == false)
    {
    entity.Attributes.Add("fax", "1122344343");
    }
    }
    }
    catch (Exception Ex)
    {
    //tracingService.Trace("Myplugin:{0}", Ex.ToString());
    throw new NotImplementedException(Ex.Message);
    }
    }
    }
    }

    If you want to know how to open the Plugin Registration Tool and connect the CRM where you want to add pre-validation operation please refer this blog

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans