As per the Event Execution Pipeline for plugins in Dynamics 365, here we will look at a post-operation plugin.
These plugins execute after the main system operation and within the database transaction. They can be synchronous and asynchronous. We have seen how to use pre-validation and pre-operation to update attribute values. In post-operation, the record is committed to the database, and the database transaction has been completed. This stage is used for what to do “after”, e.g. create an activity utilizing the new record GUID.
Create a new class library in Visual Studio:
Add Microsoft.CrmSdk.CoreAssemblies through NuGet:
Add IPlugin with Execute method.
We will create a task attached to a new account we will create. We will be able to access the created entity through the “Target” of Input Parameters. We will also get the guid of the new account through context.PrimaryEntityId and user id through context.UserId:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Xrm.Sdk; namespace Carl.PluginPostOperationSample { public class CreateAccount : IPlugin { public void Execute(IServiceProvider serviceProvider) { IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { // Obtain the target entity from the input parameters. Entity entity = (Entity)context.InputParameters["Target"]; var Task = new Entity("task"); Task.Attributes["ownerid"] = new EntityReference("systemuser", context.UserId); Task.Attributes["regardingobjectid"] = new EntityReference("account", context.PrimaryEntityId); Task.Attributes["subject"] = "Visit the website at " + entity["websiteurl"]; Task.Attributes["description"] = "This is a new description"; // Obtain the organization service reference. IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); service.Create(Task); } } } }
Register the plugin:
Now, create a new account, and add a website:
Save the record. A task will be created:
We can see the Stage is 40 and the IsInTransaction is true:
The Input Parameters from the context contain all the entity attributes:
With the input values:
The Output Parameters of the Context contain the Id, which is the guid of the new record:
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
[…] Post-operation […]
Ciao a tutti vengo dall’italia