Update total time in Case

  4.create total timespan field in Case, update this field value based on timespan update in child records.


Step1: Create Total timespan field in Case entity.

Step2: Create the fetch xml to retrieve the records which having  total time in timespan

Step3: Get the all the records using fetch xml

Step4: Using foreach get the total time in timespan and add the total time using foreach

Step5: Update the that time in case entity 


C# code For  above 

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Activities;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Week3rdtasks
{
    public class Totaltimeforcasesolving : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = (IOrganizationService)factory.CreateOrganizationService(context.UserId);
            ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            if (context.Depth == 1)
            {
                try
                {
                    tracing.Trace("entity update");
                    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                    {
                        Entity Caseobject = (Entity)context.InputParameters["Target"];
                        string query = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
      "<entity name='cr8d7_timespan'>" +
        "<attribute name='cr8d7_timespanid' />" +
        "<attribute name='cr8d7_name' />" +
         "<attribute name='cr8d7_totaltimeoncase' />" +
        "<attribute name='createdon' />" +
        "<order attribute='cr8d7_name' descending='false' />" +
        "<filter type='and'>" +
          "<condition attribute='cr8d7_casestatus' operator='eq' uitype='incident' value='"+Caseobject.Id+"' />" +
          "<condition attribute='cr8d7_totaltimeoncase' operator='not-null' />" +
        "</filter>" +
      "</entity>" +
    "</fetch>";
                        EntityCollection AllTimespandata = service.RetrieveMultiple(new FetchExpression(query));
                        int Totaltimetaken = 0;
                        foreach (Entity Singeltimedaat in AllTimespandata.Entities)
                        {                           
                            tracing.Trace("total entitys" + AllTimespandata.Entities.Count);                        
                            int singelcasetime = Singeltimedaat.GetAttributeValue<int>("cr8d7_totaltimeoncase");
                            Totaltimetaken = Totaltimetaken + singelcasetime;
                            tracing.Trace("time taken " + Totaltimetaken);
                        }
                        tracing.Trace("time taken to complte" + Totaltimetaken);
                        Caseobject["cr8d7_timetocomplte"] = Totaltimetaken;

                        service.Update(Caseobject);
                        return;

                    }
                }
                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException(ex.Message);
                }
            }
        }
    }
}

Trigger the plugin on case status update and post operation pipeline






Comments

Popular posts from this blog

create HTML page show all contact related to specific account, add this webresource as a subgrid in the form

Connect MS Crm To Postman For Webapi Calls.

create one PCF control and embed into crm