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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Help finding a task's ...
Power Apps
Unanswered

Help finding a task's parent from the regarding field to recalculate roll-up

(0) ShareShare
ReportReport
Posted on by 6

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

Categories:

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 248 Super User 2026 Season 2

#2
WarrenBelz Profile Picture

WarrenBelz 192 Most Valuable Professional

#3
Mohsin Ali Profile Picture

Mohsin Ali 135

Last 30 days Overall leaderboard