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

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
Hi
Please refer the following:
community.dynamics.com/.../550141
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.
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.
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.