I wrote a plugin for Dynamics 4.0 to change the value of a picklist when an opportunity entity is reopened. The picklist value is updated correctly, but the form doesn't show this until the form is closed and reopened.
Here is my plugin:
public void Execute(IPluginExecutionContext context)
{
if (context.InputParameters.Properties.Contains("Target") &&
context.InputParameters.Properties["Target"] is DynamicEntity)
{
ICrmService service = context.CreateCrmService(false);
DynamicEntity entity = (DynamicEntity)context.InputParameters.Properties["Target"];
Key key = (Key)entity.Properties["opportunityid"];
DynamicEntity DynamicOpportunity = GetOpportunity(service, key.Value);
Picklist StageCodePicklist = (Picklist)DynamicOpportunity.Properties["salesstagecode"];
StageCodePicklist.IsNull = false;
StageCodePicklist.IsNullSpecified = false;
StageCodePicklist.name = "Advocating - Advanced (90%)";
StageCodePicklist.Value = 200004;
DynamicOpportunity.Properties["salesstagecode"] = StageCodePicklist;
service.Update(DynamicOpportunity);
}
}
The information I assign to properties on StageCodePicklist was derived from the following query run against the database:
select * from stringmap where attributename='salesstagecode'
To reiterate, I reopen an opportunity and the salesstagecode is correctly updated, but the form displays the old value. Closing the form and reopening it for the same opportunity shows that the new value was indeed in the database.
I almost wonder if this is a bug with CRM - surely the form should display the updated value without having to close and reopen the form. But that aside, is there anything I can do to accommodate so users don't have to do this to see the updated value rather than the old value?