web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

Plugin SharedVariable works onpremise but does not work in Dynamics 365 cloud

(5) ShareShare
ReportReport
Posted on by 40
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....
Categories:
I have the same question (0)
  • Suggested answer
    Fedorov Profile Picture
    40 on at
    SOLVED: In Dynamics 365 cloud a plugin shared variable can ONLY be shared in the execution context for the same table and messag. I.e: it works to set a shared variable in pre-create of tableA and read it in post-create of tableA in the same execution.
     
    This means it will not work in my scenario.
     
    The solution was to create a new field of type "auto number", set it to the appropriate "seed", and copy the value from this field to my "original" field in the pre-create plugin. It seems the number exists, as well as the id for the new record, even before it has been saved to the database.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 76

#3
Martin Dráb Profile Picture

Martin Dráb 64 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans