Question Status

Verified
Gary Monk asked a question on 1 Mar 2013 10:30 AM

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!!

Reply
Verified Answer
Josh Wells responded on 22 Mar 2013 1:44 PM

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

Reply
mbrameld responded on 1 Mar 2013 2:45 PM

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);

             }

     }

}

Reply
Gary Monk responded on 1 Mar 2013 3:00 PM

Thanks for the reply!

I receive the following error...

'LogicalName' missing for field 'Target'

Reply
mbrameld responded on 1 Mar 2013 3:25 PM

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.

Reply
Gary Monk responded on 1 Mar 2013 3:36 PM

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

Reply
mbrameld responded on 1 Mar 2013 5:47 PM

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);

Reply
Vishal510Sharma responded on 2 Mar 2013 12:02 AM

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

Reply
Gary Monk responded on 4 Mar 2013 2:00 PM

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.

Reply
Verified Answer
Josh Wells responded on 22 Mar 2013 1:44 PM

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

Reply