Dynamics 365 Plugins – Pre and Post Images

2 Comments

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:

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);
                }
            }
        }
    }
}

 

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”

 

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 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 *