cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Arshavince
Frequent Visitor

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

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

0 REPLIES 0

Helpful resources

Announcements
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Community Call Conversations

Introducing the Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (3,925)