How to get the new owner of a record in a plugin (dynamics crm 4)

Hi,

I want to share a situation that I have faced and my solution so far.

 

[Problem]

I have developed my plugin which is registered for Create and Update messages on the Account entity. The purpose of the plugin is to update internal ERP system.

Everything is working fine. But…

Problem 1: Update is triggered automatically when the user changes the owner of an account or in other words, when an account is reassigned. No clicking on ‘Save’ or ‘Save and close’ is necessary neither. Just changing the owner triggers the Update when page is being refreshed.

Problem 2: Owner change is still not reflected, so queries like <myaccount>.user_accounts as well as <myaccount>.ownerid always return the old owner.

 

[Solution]

After asking some colleagues and some research, I found out that I could register my plugin or another plugin (I chose the latter approach) to the Assign message.

image

Fine, this allows to us to capture the right event, but now, how to get the new owner inside my plugin?

The following reference code pretends to answer this question:

    public class UpdateOwnerPlugin : IPlugin
    {

        #region IPlugin Members

        public void Execute(IPluginExecutionContext context)
        {
            // This is just to check that the change was made from the App and not the SDK
            if (!context.CallerOrigin.ToString().Equals("Microsoft.Crm.Sdk.ApplicationOrigin"))
                return;

            Moniker entity = null; // what you get as Target in an Assign message is a moniker

            if (context.InputParameters.Properties.Contains("Target") &&
                       context.InputParameters.Properties["Target"] is Moniker )
            {
                // Obtain the target business entity from the input parmameters.
                entity = (Moniker)context.InputParameters.Properties["Target"];
                
                // Verify that the entity represents an account.);
                if (entity.Name != "account") { return; }
            }
            else
            {
                return;
            }

            Guid id = new Guid();
            try
            {
                id = ((Microsoft.Crm.Sdk.Moniker)context.InputParameters.Properties["Target"]).Id; // this is the id of the account whose owner has been changed

                // I get the source account with that id -> for doing so I am using a helper class developed by me
                account sourceAccount = XrmHelpers.GetAccount(id, context);

               // I get the new owner -> also using a helper class developed by me
                systemuser owner = null;
                owner = XrmHelpers.GetUser(sourceAccount.ownerid.Value, context);
                .....
                .....
         }

 

… reference code for my XrmHelpers class..

public static class XrmHelpers
{
         public static account GetAccount(Guid id, string crmConnectionStringKeyName)
        {
            var crm = new MyEntityModelDataContext(crmConnectionStringKeyName);

            var returnedAccounts = crm.accounts.Where(c => c.accountid == id);

            account accountToreturn = null;

            if (returnedAccounts != null && returnedAccounts.Count() > 0)
                accountToreturn = returnedAccounts.First();

            return accountToreturn;
        }

        public static systemuser GetUser(Guid id, IPluginExecutionContext context)
        {
            var crm = new MyEntityModelDataContext("Default", context, true);

            var returnedUser = crm.systemusers.Where(u => u.systemuserid == id).First();

            return returnedUser;
        }
}

Note that MyEntityModelDataContext refers to the Entity Data Model generated from CRM through the CrmSvcUtil.exe tool. If you do not know how to generate this, I suggest you go over this post.

 

So, this way when a user changes the owner or what is the same, the user reassigns a record (in my case, an account) the Assign plug-in is triggered and you can have access to the new owner to perform the operation you need (i.e. in my case, update the owner within the internal ERP).

Bear in mind that if you have another pluggin or the same one also registered for the Update message, it’s triggered too before Assign. Just be aware of that in order to avoid duplicating logic or getting misbehaved results.

 

Hope you find this useful,

PP [twitter: @pabloperalta]

No Comments