Reputation: 615
We have the classic scenario of generating a unique number for each record created of a certain table (custom table).
The unique number is stored i another custom table and on "pre-create" of the record the plugin updates a "fake column" in the custom table and the post-update of that custom table is then increasing the value with 1, adding it to a "shared variable" and once execution is done of the post-update the pre-create continues and pickes up the number from the shared variable and assigns it to a column.
This works splendidly for our Dynamics On Premise which is running the latest version (almost), Version 2104 (9.1.23.10) (DB 9.1.23.10).
We are now about to move Dynamics to cloud and have migrated a lot of functionality successfully. However, in cloud, the shared variable does not exist in the executionContext. Or, at least not in a way that is available in the "pre create" plugin of the record.
My question is obviously: why doesn't it work in cloud? Has Microsoft changed the functionality? Can I adjust my code in some way? I just can't figure out how...
Plugin Pre-Create code:
public int GetIncrementConfigurationValue(Guid configId, IPluginExecutionContext context)
{
Entity updConfig = new Entity(new_configuration.EntityLogicalName.ToString());
updConfig.Attributes.Add("new_configurationid", configId);
updConfig.Attributes.Add("new_aivalue", true);
_service.Update(updConfig); // <- triggers the post-update plugin which increments the value and adds it to the shared variable
if (context.SharedVariables.Contains("NEW_AIV"))
{
return (int)context.SharedVariables["NEW_AIV"];
}
else
{
IPluginExecutionContext nContext = context;
while (nContext.ParentContext != null)
{
if (nContext.ParentContext.SharedVariables.Contains("NEW_AIV"))
{
return (int)nContext.ParentContext.SharedVariables["NEW_AIV"];
}
nContext = nContext.ParentContext;
}
}
throw new Exception("Variable not found!"); // <- code always reaches this :(
}
Post-update plugin that adds the value to "shared variables":
protected override void ExecuteCrmPlugin(LocalPluginContext localContext)
{
IPluginExecutionContext context = localContext.PluginExecutionContext;
Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;
//Logic for auto-increment values: increase counter by 1 when triggered by update of new_aivalue attribute, used together with the plugin on entity where the number belongs
if (localContext.TargetEntity.Contains("new_aivalue")) // FIRE PLUGIN
{
new_configuration currentConfig = postImageEntity.ToEntity<new_configuration>();
int value = Convert.ToInt32(currentConfig.new_value);
string strvalue = string.Empty;
var ct = context;
ct.SharedVariables.Add("NEW_AIV", (Object)value);
while (ct.ParentContext != null) // Try adding value to all "shared variables" in parent contexts...
{
ct = ct.ParentContext;
ct.SharedVariables.Add("NEW_AIV", (Object)value);
}
strvalue = Convert.ToString(value + 1);
new_configuration updConfig = new new_configuration() { new_configurationId = localContext.TargetEntity.Id, new_value = strvalue };
localContext.OrganizationService.Update(updConfig);
}
}
By doing some "ugly logging" I can verify that the value is added to the shared variables and exists there in the post-update plugin but it does not exists later when the pre-create plugin continues its execution.
On-premise however the value DOES exists when the pre-create plugin continues its execution... So here I am....
Upvotes: 0
Views: 8