Oct 31, 2012

Sample Plug-in: Compare Pre and Post images on Update

We often use Update message to execute something after identifying whether a particular value is changed from A to B. Typically, we might need to handle some logic to be run upon state change which is defined as a custom field (actually an optionsetvalue). In this case, we need to implement a post update plug-in with two images. Here images are snapshots of the entity in pre/ post execution which enables us to compare some values.

Scenario I am explaining got a custom field called new_officestatus which holds the status of office (new_office entity). It says whether a given office is a main, regional or sub office.


Suppose we need to do something when an office is converted from Sub to Regional.  What we going to do is compare the values of the status in each update action. Below are the optionsetvalues;

Regional - 100000001
Sub – 100000002
Main – 100000003

If we find Pre image with 100000002 and Post image with 100000001, it is the exact time that a sub office is converted to a regional one, which we need to execute our custom logic. Below code shows how it’s being done programmatically.

using System;
using System.Collections.Generic;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System.ServiceModel;

namespace TestCompany.CRM.Plugin
{
public class officeUpdate : IPlugin
 {
  public void Execute(IServiceProvider serviceProvider)
  {
   IPluginExecutionContext context;
   IOrganizationServiceFactory factory;
   IOrganizationService service;
   Entity PreImage;
   Entity PostImage;

   try
   {
     context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
     factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
     service = factory.CreateOrganizationService(context.UserId);

     PreImage = (Entity)context.PreEntityImages["PreUpdateImage"];
     PostImage = (Entity)context.PostEntityImages["PostUpdateImage"];

     int _preOfficestatusVal = 0;
     int _postOfficestatusVal = 0;

     if (PreImage.Contains("new_officestatus"))
     { _preOfficestatusVal = ((OptionSetValue)PreImage["new_officestatus"]).Value; }
     if (PostImage.Contains("new_officestatus"))
     { _postOfficestatusVal = ((OptionSetValue)PostImage["new_officestatus"]).Value; }

     if ((_preOfficestatusVal == 100000002) && (_postOfficestatusVal == 100000001))
     {
        // Logic for - Sub converted to Regional
     }

    }
    catch (FaultException<OrganizationServiceFault> e)
    {
      throw e;
    }
    finally
    {
      service = null;
      factory = null;
      context = null;
      PreImage = null;
      PostImage = null;
    }
  }
 }
}

Now we will see plug-in registration. Here, we register two images for Pre and Post stages. They have the same name as images we read from the context in the code.


Related posts;
Sample Plug-in: Delete
Sample Plug-in: State change
Sample Plug-in code: Create and Update
Retrieve attributes of entity object

9 comments:

  1. I'm attempting do something very similar in 2013. I'm attempting to copy a picklist value before it is saved. Essentially a previous value.
    When Scribe updates a picklist I want the original value saved to a picklist called previous..... any thoughts?

    ReplyDelete
  2. Hmm..I am afraid, I havent done this in 2013, but this shouldnt be with much difference. What is your observation on your attempt.. Isnt it working or getting an error? Could you pl ellaborate on your situation.. (by the way dont forget to register the step for images..sometimes we forget and use the same name, because this is an extra step)

    ReplyDelete
  3. Thank you so much I really appreciate your work on explainig in detail the concepts of how the plugins operate. A lot of the times it can be very confusing if we are not clear about the certain concepts around the different stages, input vs output parameters and so on. Keep it up man !

    ReplyDelete
  4. Stanza, I am happy if this post helped you. Thanks for the kind words.

    ReplyDelete
  5. Well explained about the pre and post images in CRM , its really helpful.

    ReplyDelete
  6. Thanks Shekhar for the kind comment.. I am glad its helpful

    ReplyDelete