Wednesday, August 23, 2023

Plugin Images: Pre-Image and Post-Image - Dynamics 365 CE

Title: Dynamics 365 Plugin Images: Pre-Image and Post-Image 

  

In the world of Dynamics 365 Customer Engagement (CE) plugin development, the terms "Pre-Image" and "Post-Image" are the most useful components. In this blog, we'll be enchanting concepts of Pre-Image and Post-Image, and their significance and how they reshape your plugin. 

 


To Learn more about Plugin Development: Follow:   

 


What are Pre-Image and Post-Image? 

  

Pre-Image and Post-Image are essentially snapshots of data associated with a record before and after an event occurs in Dynamics 365 CE. Think of them as windows into the past and present, allowing you to peer into the record's state during different stages of a process. 

  


What is Pre-Image? 

Pre-Image - captures the snapshot of a record just before an event triggers your plugin, before it undergoes change – that's Pre-Image. 

The Pre-Image snapshot presents a record detail before any updates take place. You can compare this to the updated data to identify changes. 

  

Usage Scenarios: 

1. Validation: Compare Pre-Image data to ensure that certain conditions are met before proceeding. 

2. Data Transformation: Modify data based on the original state captured by the Pre-Image. 

  


What is Post-Image? 

  

Post-Image, on the other hand, presents a snapshot of the record's state after the event has occurred. Means it holds data after changes –that's the Post-Image.  

The Post-Image snapshot reveals a record detail after the updates. You can use this to log changes or trigger subsequent processes. 

  

Usage Scenarios:  

1. Logging Changes: Capture Post-Image data to log the modifications made by the event. 

2. Secondary Operations: Trigger additional actions or processes based on the changes reflected in the Post-Image. 

  


Image Availability based Message and Event Pipeline Execution: 

  

Message 

Stage 

Pre-Image 

Post-Image 

Create 

PRE 

No 

No 

Create 

POST 

No 

Yes 

Update 

PRE 

Yes 

No 

Update 

POST 

Yes 

Yes 

Delete 

PRE 

Yes 

No 

Delete 

POST 

Yes 

No 

 

 


Register Images in Plugin Steps: 

 

In order to register images in the Plugin step, first you need to register the plugin assembly and register a Step.  

Let’s assume that you have the Plugin step registered on Update of account and filtering attribute telephone1.  


Register New Step: 

 


Register New Image 


Open Plugin Registration Tool > Select the Step to add New Images > Right Click on the Step Name > “Register New Image 


 

 

In the Register New Image dialog:  

Image Type = Pre Image 

Entity Alias: PreImage 

Parameters: telephone1 (click on three dots to select columns rather all attributes) 


Hit Register Image 


Similarly Register Post Image- Alias- PostImage.

 

Both Pre-Image and Post Image Registered.  

 


Let’s Take an Example- If Phone Number for an Account is modified, you need to capture before and after update information. We’ll add a snapshot of the Phone Number in Notes.  


Plugin Code:  


using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Text; 

using System.Threading.Tasks; 

 
 

using System.ServiceModel; 

using Microsoft.Xrm.Sdk; 

 
 

namespace MyFirstDemoPlugin 

{ 

    public class PostAccountUpdate : IPlugin 

    { 

        private readonly string preImageAlias = "PreImage"; 

        private readonly string postImageAlias = "PostImage"; 

        public void Execute(IServiceProvider serviceProvider) 

        { 

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); 

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

            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); 

            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); 

 
 

            //Pre-Image snapshot 

            Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null; 

 
 

            //Post-Image snapshot 

            Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null; 

 
 

            try 

            { 

                tracingService.Trace("PostAccountUpdate Started"); 

 
 

                if (context.InputParameters["Target"] != null && context.InputParameters["Target"] is Entity entityAccount) 

                { 

                    //Entity entityAccount = (Entity)context.InputParameters["Target"]; //pattern matching 

 
 

                    string prePhoneNumber = string.Empty; 

                    string postPhoneNumber = string.Empty; 

 
 

                    if (preImageEntity != null) 

                    { 

                        prePhoneNumber = preImageEntity.Attributes.ContainsKey("telephone1") ? preImageEntity.GetAttributeValue<string>("telephone1") : string.Empty; 

                        tracingService.Trace("Pre Update Phone Number: " + prePhoneNumber); 

                    } 

 
 

                    if (postImageEntity != null) 

                    { 

                        postPhoneNumber = postImageEntity.Attributes.ContainsKey("telephone1") ? postImageEntity.GetAttributeValue<string>("telephone1") : string.Empty; 

                        tracingService.Trace("Post Update Phone Number: " + postPhoneNumber); 

                    } 

 
 

                    string snapshotDescription = "Before Update: " + prePhoneNumber + " After Update: " + postPhoneNumber; 

 
 

                    //create Notes record 

                    Entity notesObj = new Entity("annotation"); 

                    notesObj.Attributes.Add("subject", "Phone Number Update Snapshot"); 

                    notesObj.Attributes.Add("notetext", "Account Phone Number Modified: " + snapshotDescription); 

                    notesObj.Attributes.Add("objectid", new EntityReference("account", entityAccount.Id)); 

                    service.Create(notesObj); 

 
 

                    tracingService.Trace("PostAccountUpdate Finished"); 

                } 

            } 

            catch (Exception ex) 

            { 

                tracingService.Trace(ex.ToString()); 

            } 

        } 

    } 

} 


 

 

 

Let's take a look at Tracing:  

Navigate: Settings > Customization > Plug-In Trace Log    

 


 

Best Practices for Working with Pre-Image and Post-Image 

  

1. Limit Fields: Capture only the necessary fields in Pre-Image and Post-Image to optimize performance. 

2. Effective Comparison: Use Pre-Image to compare and validate before changes and Post-Image to evaluate the aftermath. 

3. Use Efficiently: Avoid excessive manipulation within Pre-Image or Post-Image to ensure system performance. 

  


**Conclusion** 

  

Pre-Image and Post-Image, in the realm of Dynamics 365 CE plugins, transform your customization capabilities. These snapshots of time offer invaluable insights that allow you to craft custom logic and respond effectively to changes within your Dynamics 365 CE environment. 

   

Stay tuned for more in-depth explorations into the Dynamics 365 CE Platform. 

 


No comments:

Post a Comment