Last couple of weeks I have been spent most of my time in working with Plugins. I would like to share some of my issues I came across and the solution I found.
*
One of the issues that I found was I need to deactivate or re-activate a record in my Plugin. As I already done some Naming Plugins, and some other Plugins that require changing the value of certain fields. I thought this one will be the same, so I did something like this :
Entity entity = new Entity(target.LogicalName);
entity["id"] = target.Get<Guid>(keyField);
entity["state"] = "1";
sdk.Update(entity);
“target” is the entity I like to update the state, here I created a new entity with the same type, and assign the id to it, so that CRM knows which one to update. And then I change the “state” attribute, and manually update it using the “Update” method of IOrganizationService instance.
In Dynamics CRM 2011, you can’t use the standard way to change the value for “status” and “state” attributes, Dynamics CRM 2011 provided a special “message/method” for you to change the values of those two fields, here is the code:
_sdk.Execute(new SetStateRequest
{
EntityMoniker = new EntityReference("contact", contactId),
State = new OptionSetValue(1),
Status = new OptionSetValue(-1)
});
The example above will set the state of a Contact record, which ID is contactId, to “inactive”. Using this method, you can also keep the same “state”, and just change the “status” attribute.
Hopefully you will find it useful in your next project.
*Image from http://www.eb-5center.com/files/icon_check.png