Account entity not being updated in plugin

This question is answered

We are using CRM 2011 Online. I have a plugin that is attempting to update the account name from the company name in the regarding lead. It executes without an error but the account name doesn't get updated.  The plugin is registered "Post Operation" and has a post image that includes just the account name field. One question I have is, if there is no data in the account name field, is the field part of the attribute list? Here is the code...

   if (context.InputParameters.Contains("Target") &&

        context.InputParameters["Target"] is Entity)

   {

        Entity account = (Entity)context.InputParameters["Target"];

        if (account.LogicalName == "account")

        {

             Entity member = service.Retrieve("lead",

                   ((EntityReference)account["originatingleadid"]).Id, new ColumnSet(true));

             if (member.Attributes.Contains("companyname"))

             {

                  companyName = member.Attributes["companyname"].ToString();

              }

              if (context.PostEntityImages.Contains("AccountPostImage") &&

                   context.PostEntityImages["AccountPostImage"] is Entity)

              {

                    accountPostImage = (Entity) context.PostEntityImages["AccountPostImage"];

                    companyName = "This is a test";

                    if (companyName != String.Empty)

                    {

                        accountPostImage.Attributes["name"] = companyName;

                        service.Update(account);

                    }

               }

         }

Thank you for the help!!

Verified Answer
  • Gary,

    In regards to your question:

    "if there is no data in the account name field, is the field part of the attribute list?"

    You could identify which fields are available by debugging the plugin.  But to answer your question, the POST images will not contain fields that do not contain data.  

    Thank you for using Microsoft Dynamics CRM Communities,

    Dynamics CRM Support Blog

    Josh Wells
    Support Engineer
    Microsoft Dynamics CRM

All Replies
  • You're changing the attribute on the accountPostImage object and then calling Update on the account object. I don't think you actually need the post image for this.

    Try this:

    if (context.InputParameters.Contains("Target") &&

           context.InputParameters["Target"] is Entity)

      {

           Entity account = (Entity)context.InputParameters["Target"];

           if (account.LogicalName == "account")

           {

                Entity member = service.Retrieve("lead",

                      ((EntityReference)account["originatingleadid"]).Id, new ColumnSet(true));

                if (member.Attributes.Contains("companyname"))

                {

                     companyName = member.Attributes["companyname"].ToString();

                     var accountUpdate = new Entity {

                         Id = account.Id,

                         LogicalName = "account",

                     };

                     accountUpdate.Attributes.Add(new System.Collections.Generic.KeyValuePair<string, object>("name", companyName));

                     service.Update(accountUpdate);

                 }

         }

    }

  • Thanks for the reply!

    I receive the following error...

    'LogicalName' missing for field 'Target'

  • Ah, oops. Try replacing

    var accountUpdate = new Entity {

        Id = account.Id,

    };

    with

    var accountUpdate = new Entity {

       Id = account.Id,

       LogicalName = "account",

    };

    I edited my first post to reflect the change too.

  • This works if there isn't any data in the nme field but it doesn't change it if there is already data there. I would like it to work both ways.

    Thanks

  • That's strange, it should work for either case.

    You could try updating the account object itself and saving that. I'm not sure it'll work though.

    Something like this:

    account["name"] = companyName;

    service.Update(account);

  • If i understand correct - you need to update certain attribute (WIth or without Data ie Null) using plugin.

    1st, Target doesnt include attributes with null data. if you want to update those attributes, check the PreImage for that entity.

    If attribute contains data, there shouldnt be any issue in updating as per suggested code by  MBRAMELD.

    BR/ Vishal

  • Thanks for the reply. Since this is on a create of an account, there is no preimage. Here is my current code....

    When I execute this code, I get a message saying "unsaved updates". When I click on save, the account name is not updated.

  • Gary,

    In regards to your question:

    "if there is no data in the account name field, is the field part of the attribute list?"

    You could identify which fields are available by debugging the plugin.  But to answer your question, the POST images will not contain fields that do not contain data.  

    Thank you for using Microsoft Dynamics CRM Communities,

    Dynamics CRM Support Blog

    Josh Wells
    Support Engineer
    Microsoft Dynamics CRM