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?
Regardless of if you register this in a pre-stage or a post-stage it should be reflected when the form reloads. The only thing that would delay the update is if you registered your plugin to run asynchronously, in which case it runs in the background Async Service.
Which mode are you registering the plugin step as? (1=Asynchronous, 0=Synchronous).
In addition, if you register your plugin to run in the pre-stage and synchronously you can get rid of a lot of the code by just updating the target input parameter.
Hope this helps!