Hi I am trying to write a plugin that will auto-recalculate a custom entity's roll-up field every time a task's status is updated.
I have been following a tutorial where both entities are custom entities, but I think it works differently for a task.
When I change the code to try and refer to a task, I think I am either using the wrong field/ID to try and tell the plugin which parent record to run the recalculation on, or I need a different approach due to the child entity being a task and it having to use the 'regarding' / regardingobjectid
In a different tutorial, I have seen a plugin involving tasks, and they used:
if (context.OutputParameters.Contains("id"))
{
Guid regardingobjectid = new Guid(context.OutputParameters["Id"].ToString());
string regardingobjectidtype = "tp_productionrequest";
How can I get the plugin to work in the desired way? Is there anything obvious I'm missing?
My code, such that it is, is below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
namespace RollupRefresherPlugin
{
public class RefreshRollupPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//Common Code for every Plugin
//ExecutionContext Object
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
//OrganizationServiceFactory Object
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
//OrganizationService Object
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
Entity task = null;
try
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
task = (Entity)context.InputParameters["Target"];
Entity pretask = (Entity)context.PreEntityImages["preimage"];
var regarding = pretask.GetAttributeValue<EntityReference>("regarding").Id;
CalculateRollupFieldRequest request = new CalculateRollupFieldRequest
{
Target = new EntityReference("tp_productionrequest", regarding),
FieldName = "tp_CompleteTasks" // Rollup Field Name
};
CalculateRollupFieldResponse response = (CalculateRollupFieldResponse)service.Execute(request);
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}
Many thanks in advance
User | Count |
---|---|
252 | |
101 | |
94 | |
47 | |
38 |