| Ijlal Monawwar
How to refresh roll-up fields in Dynamics 365
Dealing with roll-up fields can be a bit challenging as they have a fixed cadence of updating their values (can be set to at least 1 hour for each update). There are a few ways of dealing with this if we want to update the value instantaneously.
- Manually selecting the re-calculate button after pressing the calculator icon on the field.
- Using HTTP action to call the Dataverse' Web API. There are a few steps to get this going to setup the service principal to allow connection, authenticate then call the CalculateRollupField function.
- Using a plugin
To refresh roll-up fields using a plugin in Dynamics 365, you will need to follow these steps:
Create a new plugin project in your development environment. You can use tools like Visual Studio or the Dynamics 365 Developer Toolkit to do this.
In the plugin code, use the IOrganizationService interface to retrieve the entity record that you want to update.
Use the Update method of the IOrganizationService interface to update the entity record and set the roll-up field to the desired value.
Deploy the plugin to your Dynamics 365 instance.
Here is an example of how this might look in code:
using Microsoft.Xrm.Sdk;
namespace MyPluginNamespace
{
public class MyPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// Retrieve the entity record that you want to update.
Entity targetEntity = (Entity)context.InputParameters["Target"];
// Set the roll-up field to the desired value.
targetEntity["rollupfield"] = 123;
// Update the entity record.
service.Update(targetEntity);
}
}
}
Join us next time, as we continue our journey of learning canvas apps.Click here to learn more about Imperium's Power Apps Services. We hope this information was useful, and we look forward to sharing more insights into the Power Platform world.