locked
Entity owner change tracking RRS feed

  • Question

  • Hi All,

    I have to write a custom plugin that will track the owner changes. I.e., when ever the owner of that particular entity changes I need to add a Note to that entity mentioning the previous Owner name, Present owner name, and the user who changed the owner. Please let me know the code for this.

    Thanke
    Wednesday, December 16, 2009 9:18 AM

Answers

  • Wednesday, December 16, 2009 9:21 AM
    Moderator
  • Yes, after creating the Post and Pre entity images every thing worked fine. Below is the consolidated code for this task. Thanks Andriy.


    public

     

    class AddNotesIfOpportunityOwnerChanged : IPlugin

    {

    #region

     

    IPlugin Members

     

    public void Execute(IPluginExecutionContext context)

    {

     

    if (context.InputParameters.Properties.Contains("Target") && context.InputParameters.Properties["Target"] is Moniker)

    {

     

    Moniker moniker = (Moniker)context.InputParameters.Properties["Target"];

     

    if (string.Compare(moniker.Name, EntityName.opportunity.ToString(), true) == 0)

    {

     

    Guid id = moniker.Id;

     

    ICrmService crmService = context.CreateCrmService(true);

     

    DynamicEntity PreImage = null;

     

    DynamicEntity PostImage = null;

     

    //Get the Pre and Post Images

     

    if (context.PreEntityImages.Properties.Contains("Images") &&

    context.PreEntityImages.Properties[

    "Images"] is DynamicEntity)

    {

    PreImage = (

    DynamicEntity)context.PreEntityImages.Properties["Images"];

    }

     

    if (context.PostEntityImages.Properties.Contains("Images")

    && context.PostEntityImages.Properties[

    "Images"] is DynamicEntity)

    {

    PostImage = (

    DynamicEntity)context.PostEntityImages.Properties["Images"];

    }

     

    if (PreImage != null && PostImage != null)

    {

     

    Guid? userPreUpdate = AddNotesIfOpportunityOwnerChanged.GetOwnerId(PreImage, "ownerid");

     

    Guid? userPostUpdate = AddNotesIfOpportunityOwnerChanged.GetOwnerId(PostImage, "ownerid");

     

    if (userPreUpdate != null && userPostUpdate != null)

    {

     

    if (userPreUpdate != userPostUpdate)

    {

     

    string previousOwner = GetUserNameFromGUID(userPreUpdate, crmService);

     

    string newOwner = GetUserNameFromGUID(userPostUpdate, crmService);

     

    string currentUser = GetUserNameFromGUID(context.UserId, crmService);

     

    Lookup lookupOpportunity = new Lookup();

    lookupOpportunity.type =

    EntityName.opportunity.ToString();

    lookupOpportunity.Value = id;

     

    annotation noteOwnerChange = new annotation();

    noteOwnerChange.subject =

    "Owner Changed";

     

    string noteText = "Previous Owner: " + previousOwner + Environment.NewLine +

     

    "New Owner: " + newOwner + Environment.NewLine +

     

    "Owner Changed By: " + currentUser;

    noteOwnerChange.notetext = noteText;

    noteOwnerChange.objectid = lookupOpportunity;

    noteOwnerChange.objecttypecode =

    new EntityNameReference();

    noteOwnerChange.objecttypecode.Value =

    EntityName.opportunity.ToString();

    crmService.Create(noteOwnerChange);

    }

    }

     

    else

    {

     

    return;

    }

    }

    }

    }

     

    else

    {

     

    return;

    }

    }

     

    public static Guid? GetOwnerId(DynamicEntity entity, string attributeName)

    {

    attributeName = attributeName.ToLower();

     

    if (entity.Properties.Contains(attributeName))

    {

     

    return ((Owner)entity.Properties[attributeName]).Value;

    }

     

    else

    {

     

    return null;

    }

    }

     

    public static string GetUserNameFromGUID(Guid? userGUIDNull, ICrmService crmService)

    {

     

    BusinessEntity entity = crmService.Retrieve(EntityName.systemuser.ToString(), new Guid(Convert.ToString(userGUIDNull)), new ColumnSet(new string[] { "fullname" }));

     

    if (entity != null)

    {

     

    return ((systemuser)entity).fullname;

    }

     

    else

    {

     

    return null;

    }

    }

    #endregion

     

    IPlugin Members

    }

    Thursday, December 17, 2009 10:09 AM

All replies

  • Wednesday, December 16, 2009 9:21 AM
    Moderator
  • I am not sure about the below code in the above article

     

    //Close previous involvement records for this user
    if (keyWorkerIdPre.HasValue)
    {
       CloseKeyWorkerRecord(id, keyWorkerIdPre.Value, crmService);
    }
    if (keyWorkerIdPost.HasValue)
    {
       CreateKeyWorkerHistoryRecord(id, keyWorkerIdPost.Value, crmService);
    }

    I think this is some thing specific to logic. Repalcing this code i need to place my Logic, where i need to add a not to the entity mentioning the required values. Am i correct? And, one more thing how to get the user ID who is exceuting this request?

    Wednesday, December 16, 2009 9:31 AM
  • It's only the sample.

    Identifier of user which executes action you can receive from context.CallerId property. Now to add note to an entity you can see here - without part which attaches file.

    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Wednesday, December 16, 2009 9:39 AM
    Moderator
  • Thanks Andriy. Now I have all what i need, I will build a plugin now. Can you calrify one of my doubt? What is the difference between Moniker and Dynamic Entity.

    Thanks
    Wednesday, December 16, 2009 9:48 AM
  • Check following urls:

    Moniker - http://msdn.microsoft.com/en-us/library/bb959632.aspx
    DynamicEntity - http://msdn.microsoft.com/en-us/library/bb930292.aspx

    Moniker has only entity type and identifier Dynamic Entity - has properties inside it.
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Wednesday, December 16, 2009 9:56 AM
    Moderator
  • Yes, i have gone though them and the difference is clear now.

    Reagrding the user name we only seem to have the pre and post owner identifiers, should we hit the service to get the owner names or is there any way around?
    Wednesday, December 16, 2009 10:08 AM
  • You have to use Retrieve request to retrieve Name of user.

    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Wednesday, December 16, 2009 10:11 AM
    Moderator
  • Yes Andriy, thanks for the help. Will work on this and will post the complete code.
    Wednesday, December 16, 2009 10:19 AM
  • Hi,

    I have some problem getting the Pre and Post images of entity. Both of them are returned as NULL. I am registered the step as

    Message: Assign
    Primary Entity: Opportunity
    Mode: Synchronous
    Invocation: Parent Pipeline
    Stage: Post Stage

    And the code to get them is as below

    Moniker

     

    moniker = (Moniker)context.InputParameters.Properties["Target"];
    Guid id = moniker.Id;
    Lookup lookupOpportunity = new Lookup();
    lookupOpportunity.type =
    EntityName.opportunity.ToString();
    lookupOpportunity.Value = id;
    ICrmService crmService = context.CreateCrmService(true);
    DynamicEntity PreImage = null;
    DynamicEntity PostImage = null;
    //Get the Pre and Post Images
    if (context.PreEntityImages.Properties.Contains("Images") &&context.PreEntityImages.Properties["Images"] is DynamicEntity)
    {
    PreImage = (
    DynamicEntity)context.PreEntityImages.Properties["Images"];
    }
    if (context.PostEntityImages.Properties.Contains("Images")&& context.PostEntityImages.Properties["Images"] is DynamicEntity)
    {
    PostImage = (
    DynamicEntity)context.PostEntityImages.Properties["Images"];
    }

    Please let me know where I am going wrong. I dont have the environment where we can debug the plugin :(

    Many Thanks

    Thursday, December 17, 2009 7:56 AM
  • I have further digged in and found that

    context.PreEntityImages.Properties.Contains(

    "Images")

    is returning NULL and is similar case with

    context.PostEntityImages.Properties.Contains(

    "Images")

    Please suggest.

    Thursday, December 17, 2009 9:15 AM
  • I think i have to create pre and post entity images....can any one explain how this can be done
    Thursday, December 17, 2009 9:26 AM
  • Yes, after creating the Post and Pre entity images every thing worked fine. Below is the consolidated code for this task. Thanks Andriy.


    public

     

    class AddNotesIfOpportunityOwnerChanged : IPlugin

    {

    #region

     

    IPlugin Members

     

    public void Execute(IPluginExecutionContext context)

    {

     

    if (context.InputParameters.Properties.Contains("Target") && context.InputParameters.Properties["Target"] is Moniker)

    {

     

    Moniker moniker = (Moniker)context.InputParameters.Properties["Target"];

     

    if (string.Compare(moniker.Name, EntityName.opportunity.ToString(), true) == 0)

    {

     

    Guid id = moniker.Id;

     

    ICrmService crmService = context.CreateCrmService(true);

     

    DynamicEntity PreImage = null;

     

    DynamicEntity PostImage = null;

     

    //Get the Pre and Post Images

     

    if (context.PreEntityImages.Properties.Contains("Images") &&

    context.PreEntityImages.Properties[

    "Images"] is DynamicEntity)

    {

    PreImage = (

    DynamicEntity)context.PreEntityImages.Properties["Images"];

    }

     

    if (context.PostEntityImages.Properties.Contains("Images")

    && context.PostEntityImages.Properties[

    "Images"] is DynamicEntity)

    {

    PostImage = (

    DynamicEntity)context.PostEntityImages.Properties["Images"];

    }

     

    if (PreImage != null && PostImage != null)

    {

     

    Guid? userPreUpdate = AddNotesIfOpportunityOwnerChanged.GetOwnerId(PreImage, "ownerid");

     

    Guid? userPostUpdate = AddNotesIfOpportunityOwnerChanged.GetOwnerId(PostImage, "ownerid");

     

    if (userPreUpdate != null && userPostUpdate != null)

    {

     

    if (userPreUpdate != userPostUpdate)

    {

     

    string previousOwner = GetUserNameFromGUID(userPreUpdate, crmService);

     

    string newOwner = GetUserNameFromGUID(userPostUpdate, crmService);

     

    string currentUser = GetUserNameFromGUID(context.UserId, crmService);

     

    Lookup lookupOpportunity = new Lookup();

    lookupOpportunity.type =

    EntityName.opportunity.ToString();

    lookupOpportunity.Value = id;

     

    annotation noteOwnerChange = new annotation();

    noteOwnerChange.subject =

    "Owner Changed";

     

    string noteText = "Previous Owner: " + previousOwner + Environment.NewLine +

     

    "New Owner: " + newOwner + Environment.NewLine +

     

    "Owner Changed By: " + currentUser;

    noteOwnerChange.notetext = noteText;

    noteOwnerChange.objectid = lookupOpportunity;

    noteOwnerChange.objecttypecode =

    new EntityNameReference();

    noteOwnerChange.objecttypecode.Value =

    EntityName.opportunity.ToString();

    crmService.Create(noteOwnerChange);

    }

    }

     

    else

    {

     

    return;

    }

    }

    }

    }

     

    else

    {

     

    return;

    }

    }

     

    public static Guid? GetOwnerId(DynamicEntity entity, string attributeName)

    {

    attributeName = attributeName.ToLower();

     

    if (entity.Properties.Contains(attributeName))

    {

     

    return ((Owner)entity.Properties[attributeName]).Value;

    }

     

    else

    {

     

    return null;

    }

    }

     

    public static string GetUserNameFromGUID(Guid? userGUIDNull, ICrmService crmService)

    {

     

    BusinessEntity entity = crmService.Retrieve(EntityName.systemuser.ToString(), new Guid(Convert.ToString(userGUIDNull)), new ColumnSet(new string[] { "fullname" }));

     

    if (entity != null)

    {

     

    return ((systemuser)entity).fullname;

    }

     

    else

    {

     

    return null;

    }

    }

    #endregion

     

    IPlugin Members

    }

    Thursday, December 17, 2009 10:09 AM