Home › Dynamics 365 Customer Engagement (CRM) › Dynamics 365 Plugins – Pre and Post Images
Dynamics 365 Customer Engagement (CRM) · Apr 17, 2017

Dynamics 365 Plugins – Pre and Post Images

When using Dynamics 365 Plugins, we have the ability to view the record data before and after changes have been made. Here we will go through an example.

First, create a new class library in Visual Studio:

Add code:

[sourcecode language=”csharp”]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace Carl.Crm.PrePostImage
{
public class CheckAccount : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

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

if (entity.LogicalName == "account")
{
Entity account = (Entity)context.InputParameters["Target"];
Entity preImageAccount = (Entity)context.PreEntityImages["Image"];
Entity postImageAccount = (Entity)context.PostEntityImages["Image"];

string preImagePhoneNumber = preImageAccount.GetAttributeValue<string>("telephone1");
string postImagePhoneNumber = postImageAccount.GetAttributeValue<string>("telephone1");

tracingService.Trace("Pre-image phone number: {0}, Post-image phone number: {1}", preImagePhoneNumber, postImagePhoneNumber);
}
}
}
}
}

[/sourcecode]

 

Now, register a step:

Register on Post Operation:

We will filter this to run on the telephone1 change:

Now, register an image. Select the Update step we created above:

We will register this on Pre Image and Post Image:

First ensure tracing is enabled in System Settings:

Now we can run the code.

Go to an account and note the phone number:

Change the phone number:

Go to the Plugin Trace Log:

Open the record:

You will see the line we added in the message block:

“Pre-image phone number: 425-488-7759, Post-image phone number: 425-488-7758”

 

2 Responses to Dynamics 365 Plugins – Pre and Post Images

  1. I have been exploring for a little for any high-quality articles or blog posts
    in this kind of area . Exploring in Yahoo I at last stumbled upon this website.
    Studying this information So i’m satisfied to express that I’ve a very good uncanny feeling I came upon just what
    I needed. I most for sure will make certain to
    do not disregard this web site and give it a look on a continuing basis.

Leave a Reply

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

Carl de Souza
KEEP LEARNING. KEEP BUILDING.

Explore AI, agents & Microsoft technology.

I share practical ideas, tutorials, and videos about AI, AI agents, Microsoft technologies, and the Power Platform.