Dynamics 365 – Pre-validation Plugin

2 Comments

As per the Event Execution Pipeline for plugins in Dynamics 365, here we will look at a pre-validation plugin.

These plugins execute before the main system operation and outside the database transaction. Pre-validation runs before validation occurs. You can also modify values at this stage, though this may be generally done in pre-operation. If you are creating a record, as the record is not created at this point, you will not have a record GUID.

In Visual Studio, create a new Class Library:

Through NuGet, add the latest Microsoft.CrmSdk.CoreAssemblies:

Add:

using Microsoft.Xrm.Sdk;

We will call our class UpdateAccount, IPlugin with an Execute method:

Add the code. We will update the fax number of the account:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;

namespace Carl.PluginPreValidationSample
{
    public class UpdateAccount : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity entity = (Entity)context.InputParameters["Target"];

                if (entity.LogicalName == "account")
                {
                    if (entity.Attributes.Contains("fax") == false)
                    {
                        entity.Attributes.Add("fax", "1112221111");
                    }
                }
            }
        }
    }
}

Sign the assembly:

Now open the Plugin Registration Tool and register:

Click Register Selected Plugins:

Click Register New Step. We make this an update to account, pre-validation:

Note as this is pre-validation, it needs to be synchronous and cannot be asynchronous.

Now we can test this. On an account record, insert a fax number:

After saving and refreshing, we can see the fax has been overwritten by our plugin:

We can see if we run this in the debugger, the Execution Pipeline stage is 10 (pre-event, pre-validation). As this executes outside the database, the IsInTransaction is false:

 

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

2 Responses to Dynamics 365 – Pre-validation Plugin

  1. This won’t work with your sample. The if statement to check for fax number will fail as the fax attribute will be there. Thoughts?

Leave a Reply

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